为什么c#中的CreateWindowEx()函数总是返回0

本文关键字:返回 函数 中的 CreateWindowEx 为什么 | 更新日期: 2023-09-27 17:51:08

我正试图在c# 中实现Windows消息传递,以便与常规的exe通信HTML页面。我需要做的是创建一个具有特定类名和窗口名的新窗口,以便其他进程可以向我的Activex应用程序发送Windows消息。

[DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr CreateWindowEx(
           WindowStylesEx dwExStyle,
           string lpClassName,
           string lpWindowName,
           WindowStyles dwStyle,
           int x,
           int y,
           int nWidth,
           int nHeight,
           IntPtr hWndParent,
           IntPtr hMenu,
           IntPtr hInstance,
           IntPtr lpParam);

            IntPtr temp = CreateWindowEx(0, "privateclassname", "privatewindowname",
                WindowStyles.WS_CHILD | WindowStyles.WS_VISIBLE, 0, 0, 1, 1,
                IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

这是我一直在尝试,但temp总是得到0而不是一个适当的窗口句柄。这段代码有什么问题?我怀疑hWndParent参数。给它0,因为我不知道父进程的句柄,或者它甚至不存在。提前感谢

为什么c#中的CreateWindowEx()函数总是返回0

您正在传递WS_CHILD窗口样式标志,但您没有传递父窗口句柄(您正在传递0/NULL)。尝试删除WS_CHILD样式。

另外,请参阅我对上面调用RegisterClass的评论(如果适用)。