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

C#:FindWindowEx未按预期工作

  •  0
  • Gazza732  · 技术社区  · 6 年前

    好的,首先,我是C语言的新手,所以这可能很简单,我只是在Google/so中找不到答案。

    我有一个试图使用FindWindowEx的类,但是Visual Studio不允许我使用“null”参数,我不确定为什么。

    到目前为止,该类具有以下代码:

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr handleParent, IntPtr handleChild, string className, string WindowName);
    
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    
        public void SomeWindow()
        {
    
            String someHwnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "SomeWindowClass", NULL);
    
        }
    

    正如所写的,它告诉我“NULL”在当前上下文中不存在。我也尝试过这样写:

    FindWindowEx(null, null, "SomeWindowClass", null)
    

    这会在前两个“null”表示“Argument#:无法从“null”转换为“IntPtr”时出现错误(“null”实际上有<和>围绕着它,尽管它没有显示出来)

    Windows开发中心表示,我应该能够按自己的想法使用它,如下所示: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633500(v=vs.85).aspx

    就像我说的,这可能非常简单,我只是没有足够的C语言经验来理解它。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Cinchoo    6 年前

    FindWindow返回类型是IntPtr,而您正试图将其分配给string。

    试试这个。

    IntPtr wnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "SomeWindowClass", null);