Message-Only Windows DOES NOT receive broadcast messages:
A message-only window enables you to send and receive messages. It is
not visible, has no z-order, cannot be enumerated, and does not
receive broadcast messages. The window simply dispatches messages.
Instead, you should create a top-level window and do not call showWindow
.
In addition, you don't need to call CreateWindow
/CreateWindowEx
through DLL, try to use WinAPI by importing the module win32api, win32con, win32gui. Here is an sample.
UPDATE:
C++ sample that cannot receive WM_DEVICECHANGE
with Message-only window.
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
MessageBox(NULL, "WM_CREATE", "Message", 0);
break;
case WM_DEVICECHANGE:
MessageBox(NULL, "WM_DEVICECHANGE", "Message", 0);
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
int main()
{
static const char* class_name = "NAME_CLASS";
WNDCLASSEX wx = {};
HWND hwnd;
HINSTANCE hInstance = GetModuleHandleA(NULL);
wx.cbSize = sizeof(WNDCLASSEX);
wx.lpfnWndProc = WndProc; // function which will handle messages
wx.hInstance = hInstance;
wx.lpszClassName = class_name;
if (RegisterClassEx(&wx)) {
hwnd = CreateWindowEx(0, class_name, "Title", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
// hwnd = CreateWindowEx(0, class_name, "Title", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
// Create a normal top-level window which can receive the broadcast messages.
}
HACCEL hAccelTable = LoadAccelerators(hInstance, class_name);
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
EDIT:
Pump messages is needed after create window.