Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:FindWindowEx未按预期工作_C#_Visual Studio_Pinvoke_Dllimport - Fatal编程技术网

C#:FindWindowEx未按预期工作

C#:FindWindowEx未按预期工作,c#,visual-studio,pinvoke,dllimport,C#,Visual Studio,Pinvoke,Dllimport,好吧,首先我是C#新手,所以这可能很简单,我只是在Google/so中找不到答案 我在一个类中尝试使用FindWindowEx,但是VisualStudio不允许我使用“null”参数,我不确定为什么 到目前为止,该类具有以下代码: [DllImport("user32.dll")] public static extern IntPtr FindWindowEx(IntPtr handleParent, IntPtr handleChild, string className,

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

我在一个类中尝试使用FindWindowEx,但是VisualStudio不允许我使用“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开发中心表示,我应该能够按自己的想法使用它,如下所示:


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

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

试试这个

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

为什么不试试FindWindowEx(IntPtr.Zero,IntPtr.Zero,“SomeWindowClass”,null)Strengat,因为它在整个调用下面加了红色下划线,表示“无法隐式地将类型'System.IntPtr'转换为'string'。不过谢谢你的建议。哦,等等,你问了两个不同的问题。你的标题是“FindWindowEx不接受空参数”“,但您已经基本解决了这个问题,在您的问题体中,您显示了
IntPtr.Zero
您自己。返回类型问题与此不同。不抱歉,我不知道是该类型导致了问题。我以为是空参数,抱歉。编辑:我刚刚更新了问题标题以反映这一点。然而,由于只是将变量赋给了一个不正确的类型,我不确定这是否是一个有价值的问题,对其他人也有帮助。很好,这就解决了错误。我有一种感觉,这将是一件简单的事情。我使用了一个字符串,这样我就可以在消息框中编写代码来测试我所尝试的是否有效。非常感谢:D