如何在控制台应用程序中使用仅消息窗口接收消息? [英] How to receive messages using a message-only window in a console application?
问题描述
我创建了一个简单的Win32控制台应用程序,创建一个隐藏的消息窗口并等待消息,完整代码如下。
I've created a simple Win32 console application that creates a hidden message-only window and waits for messages, the full code is below.
#include <iostream>
#include <Windows.h>
namespace {
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_COPYDATA)
std::cout << "Got a message!" << std::endl;
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
int main()
{
WNDCLASS windowClass = {};
windowClass.lpfnWndProc = WindowProcedure;
LPCWSTR windowClassName = L"FoobarMessageOnlyWindow";
windowClass.lpszClassName = windowClassName;
if (!RegisterClass(&windowClass)) {
std::cout << "Failed to register window class" << std::endl;
return 1;
}
HWND messageWindow = CreateWindow(windowClassName, 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, 0, 0);
if (!messageWindow) {
std::cout << "Failed to create message-only window" << std::endl;
return 1;
}
MSG msg;
while (GetMessage(&msg, 0, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
但是,我没有从其他应用程序接收任何消息。 GetMessage()
只是阻止,从不返回。我在发送消息的应用程序中使用具有相同类名的 FindWindowEx()
,它会找到该窗口。只是消息显然从未收到。
However, I'm not receiving any messages from another application. GetMessage()
just blocks and never returns. I use FindWindowEx()
with the same class name in the application that sends a message, and it finds the window. Just the message is apparently never being received.
我在这里做错了吗?
推荐答案
您的邮件可能会被用户界面权限隔离。在这种情况下,您可以使用 ChangeWindowMessageFilterEx()
函数允许WM_COPYDATA消息通过。
Your messages may be blocked by User Interface Privilege Isolation. In that case you can use the ChangeWindowMessageFilterEx()
function to allow the WM_COPYDATA message through.
这篇关于如何在控制台应用程序中使用仅消息窗口接收消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!