简体   繁体   English

C#:FindWindowEx无法按预期工作

[英]C#: FindWindowEx not working as anticipated

Ok first off I'm new to C# so this may be something really simple, I've just not found the answer throughout Google/SO. 好的,首先我是C#的新手,所以这可能真的很简单,我只是在整个Google / SO中都找不到答案。

I have a class where I'm trying to use FindWindowEx, however, Visual Studio isn't allowing me to use 'null' arguments and I'm unsure why. 我有一个尝试使用FindWindowEx的类,但是Visual Studio不允许我使用“空”参数,我不确定为什么。

So far the class has this code: 到目前为止,该类具有以下代码:

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

    }

As it is written, it is telling me that 'NULL' doesn't exist in the current context. 在撰写本文时,它告诉我在当前上下文中不存在“ NULL”。 I've also tried writing as: 我也尝试写成:

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

This gives errors under the first two 'null's saying "Argument #: Cannot convert from 'null' to 'IntPtr'" (The 'null' actually has < and > surrounding it although SO isn't displaying it with them) 这会在前两个“ null”说“参数#:无法从'null'转换为'IntPtr'”时产生错误(“ null”实际上包含<和>,尽管SO并未与它们一起显示)

The Windows Dev Center says I should be able to use it as I am though which is here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633500(v=vs.85).aspx Windows开发人员中心说我应该能够按原样使用它: https//msdn.microsoft.com/en-us/library/windows/desktop/ms633500(v = vs.85).aspx

Like I said it may be something incredibly simple, I just don't have enough experience in C# to figure it out. 就像我说的那样,这可能非常简单,我只是没有足够的C#经验来弄清楚。

FindWindow return type is IntPtr, whereas you are trying to assign it to string. FindWindow返回类型为IntPtr,而您正在尝试将其分配给字符串。

Try this. 尝试这个。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM