2010-06-18 77 views
2

我有一个窗口句柄,并试图通过传入窗口的进程ID来调用GetGUIThreadInfo。我在GetGUIThreadInfo调用中总是收到错误“参数不正确”,我可以找出原因。有没有人得到这个工作?如何在c中调用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 
} 

回答

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); 
} 
+0

感谢发现!现在事实证明,GetGUIThreadInfo不会为不同的进程中的窗口返回任何内容。嗯... – Jeremy 2010-06-18 21:57:57