我有一个窗口句柄,我试图通过传入窗口的进程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
}
答案 0 :(得分:3)
我认为您在调用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);
}