Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 *************************/

/* Unique IDs for Window messages to exchange between the worker and GUI thread. */
#define WM_YOUR_MESSAGE   ( WM_USER + 3 )

   /* Some data structure intended to store the message content. */
typedef struct
{
    int SomeData1;
    int SomeData2;
    int SomeDataN;
} MessageData;


volatile DWORD ThreadID_GUI;

void __cdecl ThreadProc(void* aArg) 
{
    MessageData* data;

    for (;; )
    {
        Sleep(500);

        /* Allocate memory for a new message data structure */
        data = (MessageData*)malloc(sizeof(*data));

        data->SomeData1 = 1234;
        data->SomeData2 = 4567;
        data->SomeDataN = 7894;

        PostThreadMessage(ThreadID_GUI, WM_YOUR_MESSAGE, 0, (LPARAM)data);
    }
}


/************* Main.c ********************/
#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);

    // TODO: Place code here.

    ThreadID_GUI = GetCurrentThreadId();

    /* Start some background thread */
    _beginthread(ThreadProc, 0, 0);
#ifdef reg
    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_TESTMESSAGEQUEUE, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TESTMESSAGEQUEUE));
#endif
    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
            /* STEP 3: React on the message sent from the foreign thread */
            if (msg.message == WM_YOUR_MESSAGE)
            {
                MessageData* tmp = (MessageData*)msg.lParam;

                    if (tmp->SomeData1 == 1234) {
                    printf("someData\n");
                }
                /* Free the data structure associated to the message */
                free(tmp);
            }
            TranslateMessage(&msg);
            DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}
Posted
Updated 30-Jun-21 23:37pm
v2
Comments
Richard MacCutchan 30-Jun-21 4:06am    
You need to gather more information via the debugger. Check that the thread process gets started and it is posting the message to the correct threadid of the message handler.
Member 14603643 30-Jun-21 4:24am    
hi Richard,
instead of _beginthread , i tried to debug with
DWORD _acquireThreadID;
HANDLE _acquireThread;

_acquireThread = CreateThread(NULL, 0, ThreadProc, 0, 0, &_acquireThreadID);
if (_acquireThread == NULL) {
printf("could not create the thread\n");
return false;
}
result : _acquireThread = 0x000000e0

I debug it , and it seems thread is created,
ret = PostThreadMessage(ThreadID_GUI, WM_YOUR_MESSAGE, 0, (LPARAM)data);
result: ret = 1;
any more suggestion?
Richard MacCutchan 30-Jun-21 5:36am    
I have tried this myself and it works fine. The only thing that is different is that my main routine actually creates a GUI Window. As far as I can tell, if you do not do that then the system does not create a message queue. Again you can test that quite simply be placing a breakpoint at the line: while (GetMessage(&msg, NULL, 0, 0)).
Member 14603643 1-Jul-21 5:41am    
I have one more doubt , as PostMessage is in different thread and GetMessage in different thread. Is it possible to do _beginthread(ThreadProc, 0, 0); in the file where PostMessage is there in my case it is test.c. Because i have several function where data resides so that i can begin the thread and post the message .
Richard MacCutchan 1-Jul-21 5:54am    
It does not matter where the source files exist. The only issue is that you must post the message to the correct thread. But the only way to be sure is to try a few simple tests.

1 solution

Hi
it works fine only if i do extern volatile DWORD ThreadID_GUI in test.h that is included in main.c and test.c
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900