Aim:
To send the message in the Queue from one source file and get the message in another source file
test2.cpp - post the message
main.c - get the Message
Testing1: If i execute the same code in single file that get executed and i receive the data
Testing : Same code is written in two separate file "if (msg.message == WM_YOUR_MESSAGE)" these statement does not get executed.
What I have tried:
/*************test.cpp *************************/
#define WM_YOUR_MESSAGE ( WM_USER + 3 )
typedef struct
{
int SomeData1;
int SomeData2;
int SomeDataN;
} MessageData;
volatile DWORD ThreadID_GUI;
void __cdecl ThreadProc(void* aArg)
{
MessageData* data;
for (;; )
{
Sleep(500);
data = (MessageData*)malloc(sizeof(*data));
data->SomeData1 = 1234;
data->SomeData2 = 4567;
data->SomeDataN = 7894;
PostThreadMessage(ThreadID_GUI, WM_YOUR_MESSAGE, 0, (LPARAM)data);
}
}
#include<test.h>
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
ThreadID_GUI = GetCurrentThreadId();
_beginthread(ThreadProc, 0, 0);
#ifdef reg
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_TESTMESSAGEQUEUE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TESTMESSAGEQUEUE));
#endif
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
if (msg.message == WM_YOUR_MESSAGE)
{
MessageData* tmp = (MessageData*)msg.lParam;
if (tmp->SomeData1 == 1234) {
printf("someData\n");
}
free(tmp);
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}