【问题标题】:C#: FindWindowEx not working as anticipatedC#:FindWindowEx 没有按预期工作
【发布时间】:2018-01-20 16:26:42
【问题描述】:

好的,首先我是 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 #: Cannot convert from 'null' to 'IntPtr'”下给出了错误('null' 实际上有 包围它,尽管 SO 没有与它们一起显示)

Windows 开发中心说我应该可以照原样使用它,但这里是: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633500(v=vs.85).aspx

就像我说的那样,这可能非常简单,我只是没有足够的 C# 经验来弄清楚。

【问题讨论】:

  • 你为什么不试试FindWindowEx(IntPtr.Zero, IntPtr.Zero, "SomeWindowClass", null);
  • sTrenat,因为它在整个调用中用红色下划线表示“无法将类型 'System.IntPtr' 隐式转换为 'string'。不过感谢您的建议。
  • 哦等等——你问的是两个不同的问题。您的标题是“FindWindowEx 不接受空参数”,但您已经在问题正文中解决了这个问题,您自己显示IntPtr.Zero。返回类型问题与此不同。
  • 不抱歉,我不知道是导致问题的类型。我认为这是空参数,对不起。编辑:我刚刚更新了问题标题以反映这一点。但是,由于只是将变量分配给不正确的类型这样一个简单的例子,我不确定这是否是一个有价值的问题,也可以帮助其他人。

标签: c# visual-studio pinvoke dllimport


【解决方案1】:

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

试试这个。

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

【讨论】:

  • 完美,解决了错误。有一种感觉,这将是一件简单的事情。我正在使用一个字符串,以便我可以在消息框中写入来测试我正在尝试的工作。非常感谢:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多