nappaji
January 22nd, 2008, 06:36 PM
CreateWindow fails
--------------------------------------------------------------------------------
I have the following non-threaded code that works fine i.e Window creation is successful.
<code>
INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
WNDCLASSEX m_wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
L"thread", NULL };
RegisterClassEx( &m_wc );
HWND m_hWnd = CreateWindow( L"thread", L"threads",
WS_OVERLAPPEDWINDOW, 100, 100, 800, 800,
NULL, NULL, m_wc.hInstance, NULL );
ShowWindow( m_hWnd, SW_SHOWDEFAULT );
UpdateWindow( m_hWnd );
for ( ;; )
return 0;
}
<\code>
I am trying to convert this to a threaded application (I am new to Windows threading);
When I call the CreateWindow in the worker thread, it crashes at the CreateWindow call; Below is the code
<code>
DWORD WINAPI ThreadFunc( LPVOID lpParam )
{
WNDCLASSEX m_wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
L"thread", NULL };
RegisterClassEx( &m_wc );
HWND m_hWnd = CreateWindow( L"thread", L"threads",
WS_OVERLAPPEDWINDOW, 100, 100, 800, 800,
NULL, NULL, m_wc.hInstance, NULL );
ShowWindow( m_hWnd, SW_SHOWDEFAULT );
UpdateWindow( m_hWnd );
return 0;
}
//-----------------------------------------------------------------------------
// Name: wWinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
DWORD dwThreadId, dwThrdParam = 1;
HANDLE hThread;
hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadFunc,
&dwThrdParam, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
// Check the return value for success.
if (hThread == NULL)
{
printf( "CreateThread failed (%d)\n", GetLastError() );
}
else
{
CloseHandle( hThread );
}
}
<\code>
--------------------------------------------------------------------------------
I have the following non-threaded code that works fine i.e Window creation is successful.
<code>
INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
WNDCLASSEX m_wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
L"thread", NULL };
RegisterClassEx( &m_wc );
HWND m_hWnd = CreateWindow( L"thread", L"threads",
WS_OVERLAPPEDWINDOW, 100, 100, 800, 800,
NULL, NULL, m_wc.hInstance, NULL );
ShowWindow( m_hWnd, SW_SHOWDEFAULT );
UpdateWindow( m_hWnd );
for ( ;; )
return 0;
}
<\code>
I am trying to convert this to a threaded application (I am new to Windows threading);
When I call the CreateWindow in the worker thread, it crashes at the CreateWindow call; Below is the code
<code>
DWORD WINAPI ThreadFunc( LPVOID lpParam )
{
WNDCLASSEX m_wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
L"thread", NULL };
RegisterClassEx( &m_wc );
HWND m_hWnd = CreateWindow( L"thread", L"threads",
WS_OVERLAPPEDWINDOW, 100, 100, 800, 800,
NULL, NULL, m_wc.hInstance, NULL );
ShowWindow( m_hWnd, SW_SHOWDEFAULT );
UpdateWindow( m_hWnd );
return 0;
}
//-----------------------------------------------------------------------------
// Name: wWinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
DWORD dwThreadId, dwThrdParam = 1;
HANDLE hThread;
hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadFunc,
&dwThrdParam, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
// Check the return value for success.
if (hThread == NULL)
{
printf( "CreateThread failed (%d)\n", GetLastError() );
}
else
{
CloseHandle( hThread );
}
}
<\code>