gpt4 book ai didi

.net - 如何在c#中调用GetGUIThreadInfo

转载 作者:行者123 更新时间:2023-12-04 06:51:25 25 4
gpt4 key购买 nike

我有一个窗口句柄,并试图通过传入窗口的进程 ID 来调用 GetGUIThreadInfo。我总是在调用 GetGUIThreadInfo 时收到错误“参数不正确”,我可以找出原因。有没有人让这个工作?

[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetGUIThreadInfo(unit hTreadID, ref GUITHREADINFO lpgui);

[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(unit hwnd, out uint lpdwProcessId);

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int iLeft;
public int iTop;
public int iRight;
public int iBottom;
}

[StructLayout(LayoutKind.Sequential)]
public struct GUITHREADINFO
{
public int cbSize;
public int flags;
public IntPtr hwndActive;
public IntPtr hwndFocus;
public IntPtr hwndCapture;
public IntPtr hwndMenuOwner;
public IntPtr hwndMoveSize;
public IntPtr hwndCaret;
public RECT rectCaret;
}

public static bool GetInfo(unit hwnd, out GUITHREADINFO lpgui)
{
uint lpdwProcessId;
GetWindowThreadProcessId(hwnd, out lpdwProcessId);

lpgui = new GUITHREADINFO();
lpgui.cbSize = Marshal.SizeOf(lpgui);

return GetGUIThreadInfo(lpdwProcessId, ref lpgui); //<!- error here, returns false
}

最佳答案

我认为您在调用 GetWindowThreadProcessId 时使用了错误的值, 如果您查看文档 here ,您将看到第二个参数是进程 ID(正如您命名的那样),但线程 ID 位于返回值中。

所以换句话说,我认为你的代码应该是这样的(未经测试):

public static bool GetInfo(unit hwnd, out GUITHREADINFO lpgui)
{
uint lpdwProcessId;
uint threadId = GetWindowThreadProcessId(hwnd, out lpdwProcessId);

lpgui = new GUITHREADINFO();
lpgui.cbSize = Marshal.SizeOf(lpgui);

return GetGUIThreadInfo(threadId, ref lpgui);
}

关于.net - 如何在c#中调用GetGUIThreadInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3072974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com