代码之家  ›  专栏  ›  技术社区  ›  Jeremy

如何在c中调用GetGUIThreadInfo#

  •  2
  • Jeremy  · 技术社区  · 14 年前

    [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
    }
    
    1 回复  |  直到 14 年前
        1
  •  3
  •   Hans Olsson    14 年前

    我想你用的是错误的值 GetWindowThreadProcessId ,如果你看一下文档 here

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

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