Using of PostThreadMessage for InterThread Communication..
Question
Wednesday, September 1, 2010 7:26 AM
#include <iostream.h>
#include <process.h>
#include <windows.h>
#include <stdio.h>
HANDLE Threadhandle;
HANDLE Eventhandle;
DWORD Threadid;
DWORD WINAPI Threadproc(LPVOID lpParameter )
{
MSG msg;
while
(::GetMessage( &msg, NULL, 0, 0 ))
{
if
( WM_COMMAND == msg.message )
printf( "WM_COMMAND\n"
);
}
return
0;
}
int
main()
{
MSG msg;
Threadhandle = CreateThread(NULL,0,Threadproc,NULL,0,&Threadid);
// sleep some while thread initialize
Sleep( 1000 );
while
(TRUE)
{
if
(PostThreadMessage(Threadid,WM_COMMAND,(WPARAM)0,(LPARAM)0) == 0)
printf("failure"
);
printf("%d"
,GetLastError());
Sleep(1000);
}
}
1- Question : now msdn states that
BOOL WINAPI PostThreadMessage
(
__in DWORD idThread,
__in UINT Msg,
__in WPARAM wParam,
__in LPARAM lParam
);
Background : I found this example online.. and i am having difficulty understanding and modifying it...
i have the the threadid , now how do i define the wparam and lparam ?
How would i go about passing a string ?? will that be possible using the above example ... any help would be appreciated
A candle loses nothing by lighting another candle.
All replies (12)
Wednesday, September 1, 2010 1:42 PM ✅Answered
The wparam and lparam are parameters you can pass to the thread for any purpose you like. Also note that the message itself is also a parameter that can be meaningful to the thread, like
#define UWM_SEND_MY_STRING (WM_APP + 3) // custom message for thread
For example, to pass a string
CString* p = new CString(_T("data"));
// pass heap object to thread
PostThreadMessage(Threadid,UWM_SEND_MY_STRING,(WPARAM)p,0);
In the thread message handler...
CString* pt = (CString*)wparam;
...use the string
delete pt;
Wednesday, September 1, 2010 6:38 PM ✅Answered
Silver_Gates wrote:
One more question in out post message we used a constant
UWM_SEND_MY_STRING (WM_APP + 3)
what is the point of using this why do we need this in Postmessage as a parameter ??
could you please tell me what __in UINT Msg is about ?? though my example above is 70% functional now apart from the bugs i just
mentioned..
Msg is a more or less arbitrary number that both parties agree on the meaning of.
Igor Tandetnik
Wednesday, September 1, 2010 1:34 PM
Silver_Gates wrote:
1- Question : now msdn states that
BOOL WINAPI PostThreadMessage
(
__in DWORD idThread,
__in UINT Msg,
__in WPARAM wParam,
__in LPARAM lParam
);i have the the threadid , now how do i define the wparam and lparam ?
Any way you want, as long as the two parts of your code agree on their meaning. Whatever values you send here, the thread will receive as msg.wParam and msg.lParam.
How would i go about passing a string??
You pass a pointer to it. Make sure to allocate the string on the heap (and have the recipient deallocate it).
Igor Tandetnik
Wednesday, September 1, 2010 6:09 PM
I readjusted the code
#include <iostream.h>
#include <process.h>
#include <windows.h>
#include <stdio.h>
HANDLE Threadhandle;
HANDLE Eventhandle;
DWORD Threadid;
#define UWM_SEND_MY_STRING (WM_APP + 3) // custom message for thread
DWORD WINAPI Threadproc(LPVOID lpParameter )
{
MSG msg;
while
(::GetMessage( &msg, NULL, 0, 0 ))
{
cout << msg.wParam; //Display what was received in wParam of the message<br/>
}
return
0;
}
int
main()
{
MSG msg;
Threadhandle = CreateThread(NULL,0,Threadproc,NULL,0,&Threadid);
char *p=new char;
p="Hello"; //Created on heap
Sleep( 1000 );
while
(TRUE)
{
//if (PostThreadMessage(Threadid,WM_COMMAND,(WPARAM)0,(LPARAM)0) == 0)
if(PostThreadMessage(Threadid,UWM_SEND_MY_STRING,(WPARAM)p,0)==0);
printf("failure");
printf("%d"
,GetLastError());
Sleep(1000);
}
}
The ouput i am getting is wrong : I get "failure 2002236 failure 2002236 failure 2002236"
What am i doing wrong ??? I was expecting the "Hello" to be printed .....
A candle loses nothing by lighting another candle.
Wednesday, September 1, 2010 6:14 PM
Fixed it... sorry forgot to type cast it
#include <iostream.h>
#include <process.h>
#include <windows.h>
#include <stdio.h>
HANDLE Threadhandle;
HANDLE Eventhandle;
DWORD Threadid;
#define UWM_SEND_MY_STRING (WM_APP + 3) // custom message for thread
DWORD WINAPI Threadproc(LPVOID lpParameter )
{
MSG msg;
char *p = new char;
while
(::GetMessage( &msg, NULL, 0, 0 ))
{
p = (char*)(msg.wParam);
cout << p;
}
return
0;
}
int
main()
{
MSG msg;
Threadhandle = CreateThread(NULL,0,Threadproc,NULL,0,&Threadid);
char *p=new char;
p="Hello"; //Created on heap
Sleep( 1000 );
while
(TRUE)
{
//if (PostThreadMessage(Threadid,WM_COMMAND,(WPARAM)0,(LPARAM)0) == 0)
if(PostThreadMessage(Threadid,UWM_SEND_MY_STRING,(WPARAM)p,0)==0);
printf("failure");
printf("%d"
,GetLastError());
Sleep(1000);
}
}
Now i get failureHello0
Why am i getting a failure ??
and why am i getting a 0 at the end ?? I guess that the LPARAM ?? why is the compiler showing the LPARAM since i explicitly stated i wanted the Wparam only when i typecasted it ???
A candle loses nothing by lighting another candle.
Wednesday, September 1, 2010 6:18 PM
One more question in out post message we used a constant
UWM_SEND_MY_STRING (WM_APP + 3)
what is the point of using this why do we need this in Postmessage as a parameter ??
could you please tell me what __in UINT Msg is about ?? though my example above is 70% functional now apart from the bugs i just mentioned..
A candle loses nothing by lighting another candle.
Wednesday, September 1, 2010 6:36 PM
Silver_Gates wrote:
while
(::GetMessage( &msg, NULL, 0, 0 ))
{
cout << msg.wParam; //Display what was received in wParam of the message<br/>
You missed the part where wParam is cast back to the pointer.
if(PostThreadMessage(Threadid,UWM_SEND_MY_STRING,(WPARAM)p,0)==0);
Remove the semicolon at the end of this line.
Igor Tandetnik
Wednesday, September 1, 2010 6:37 PM
Silver_Gates wrote:
//if (PostThreadMessage(Threadid,WM_COMMAND,(WPARAM)0,(LPARAM)0) == 0)
if(PostThreadMessage(Threadid,UWM_SEND_MY_STRING,(WPARAM)p,0)==0);
printf("failure");
printf("%d"
,GetLastError());Now i get failureHello0
Why am i getting a failure ??
Because in your code, printf("failure") executes unconditionally. As does printf("%d", GetLastError()) (this is where '0' comes from).
Igor Tandetnik
Wednesday, September 1, 2010 6:41 PM
OOPPS Sorry :) ...
A candle loses nothing by lighting another candle.
Wednesday, September 1, 2010 6:43 PM
Fixed it.. works flawlessly
while
(TRUE)
{
//if (PostThreadMessage(Threadid,WM_COMMAND,(WPARAM)0,(LPARAM)0) == 0)
if(PostThreadMessage(Threadid,UWM_SEND_MY_STRING,(WPARAM)p,0)==0)
{
printf("failure");
printf("%d" , GetLastError());}
Sleep(1000);
}
A candle loses nothing by lighting another candle.
Wednesday, September 1, 2010 6:44 PM
My bad... sorry i didnt revise my code before posting...A candle loses nothing by lighting another candle.
Wednesday, September 1, 2010 7:45 PM
>> char *p=new char;
>> p="Hello"; //Created on heap
Awful. It's premature to be playing with passing pointers to a thread with such a poor understanding of pointers. You have allocated only 1 character in the heap. Its address is in p. Then in the next line you write the address of something else into p. Since you have lost the address of the one char in the heap, when the thread attempts to delete the pointer to "Helllo" it will cause a nasty error, since "Hello" is not a heap allocation.
Make life easier on yourself by learning to use one of the string classes.