Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

c++ - How to receive messages using a message-only window in a console application?

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;
}

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.

Am I doing something wrong here? What is the most minimal application that can receive window messages?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...