I made some Win32 API test program.
#include <Windows.h>
#define IDC_ACQUIRE (WM_APP + 103)
#define IDC_UPDATE_RESULTS (WM_APP + 107)
#define IDC_MSG_PREPARING (WM_APP + 200)
LONG WINAPI WndProc (HWND, UINT, WPARAM, LPARAM);
DWORD ThreadTakingProc (LPVOID lpdwThreadParam );
HWND m_hWnd; //Parent Window Handle
HWND m_hWndMsg;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
WNDCLASS wc;
HWND hwnd;
MSG msg;
wc.style = 0;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = L"MyWndClass";
RegisterClass (&wc);
hwnd = CreateWindow (
L"MyWndClass",
L"SDK Application",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
HWND_DESKTOP,
NULL,
hInstance,
NULL
);
ShowWindow (hwnd, nCmdShow);
UpdateWindow (hwnd);
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
static HWND hwndButton[5];
WCHAR wbuf[100];
switch (message) {
case WM_CREATE:
hwndButton[0] = CreateWindow (L"button", L"Create",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
80, 370, 100, 30,
hwnd, (HMENU)IDC_ACQUIRE,
((LPCREATESTRUCT) lParam)->hInstance, NULL);
m_hWnd = hwndButton[0];
return 0;
case WM_COMMAND:
switch(wParam)
{
case IDC_ACQUIRE:
CreateThread(NULL, //Choose default security
0, //Default stack size
(LPTHREAD_START_ROUTINE)&ThreadTakingProc,//Routine to execute
(LPVOID)"", //Thread parameter
NULL, //Immediately run the thread
NULL); //Thread Id
break;
case IDC_UPDATE_RESULTS:
unsigned int* l = reinterpret_cast<unsigned int *>(lParam);
memset(wbuf, 0x00, sizeof(wbuf));
swprintf(&wbuf[0], L"lParam : ");
MessageBox(hwnd, wbuf, L"MessageBox",MB_OK);
break;
}
break;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps);
EndPaint (hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage (0);
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam);
}
DWORD ThreadTakingProc (LPVOID lpdwThreadParam )
{
unsigned int val;
unsigned int* pval;
pval = &val;
*pval = IDC_MSG_PREPARING;
PostMessage(m_hWnd, WM_COMMAND, IDC_UPDATE_RESULTS, reinterpret_cast< LPARAM >(pval));
return 0;
}
My intention was to create thread by clicking button.
Thread should PostMessage to the WndProc and WndProc should grab the message and display the value(IDC_MSG_PREPARING = 32968)
However this code doesn't work correctly.
First, compile error occurs in swprintf(I can't find correct UNICODE version of sprintf)
Can anybody make this code work correctly?