Reputation: 2764
I have created a Window and a Thread in my Application. Now I want the thread to process some data and display it on the Main Window. For this purpose, I have used PostMessage() funcion inside my thread. i.e. A window is created, then a Thread is created. The thread applies some processing to data and Posts a Message to Main window that data should be displayed now. But the problem is that for this purpose, I shall have to pass Window's Handle, While for this thread, Main Window Handle is an undeclared IDENTIFIER. Is there any possible way in which I can pass the handle to Main Window to my thread so that using this Handle, the Thread is able to Post the Message. Below is the code snippet for any help:
///Thread Function////
DWORD WINAPI threadFunction(LPVOID param)
{
//do something
PostMessage(hMainWin, WM_thread,0, 0);
return true;
}
////Main Function///
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
const char AH_Glb_ClassName[] = "myWindClass";
WNDCLASSEX wc;
MSG Msg;
//Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = NULL;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = AH_Glb_ClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
HWND hMainWin; //Handle to Main WIndow
///Creating Main Window///
hMainWin = CreateWindowEx( WS_EX_CLIENTEDGE,
AH_Glb_ClassName,
"I am SERVER Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
440,
120,
NULL, NULL, NULL, NULL);
if(hMainWin == NULL)
{
MessageBox(NULL,
"Window Creation Failed!",
"Error!",
MB_ICONEXCLAMATION |
MB_OK);
return 0;
}
ShowWindow(hMainWin,SW_MAXIMIZE);
UpdateWindow(hMainWin);
/////////////////THREAD////////////////
DWORD threadID = 0;
///Creating Thread///
HANDLE threadHandle = CreateThread(NULL,
0,
threadFunction,
0,
0,
&threadID);
return 0;
}
Upvotes: 0
Views: 2551
Reputation: 43331
DWORD WINAPI threadFunction(LPVOID param)
{
HANDLE hwnd = (HANDLE)param;
}
...
HANDLE threadHandle = CreateThread(NULL,
0,
threadFunction,
(LPVOID)hMainWin,
0,
&threadID);
Thread function has LPVOID
type, it matches the HANDLE
type, both in Win32 and x64.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682453%28v=vs.85%29.aspx
BTW, you need to add message loop to WinMain.
Upvotes: 7