Strange problem about register window class
I want to register two window class in winmain.But when registering
the second window class,VC7.0 reports an error.The error code is 87 -
parameter error.But the parameters of both WINCLASSEX are the same
except the wndproc and wndclass name.
When compiling with bcc55,it's OK.When compiling with cygwin-g++-4.1.2
it's the same as VC7.0.
If assign wc.hInstance with
reinterpret_cast<HINSTANCE>(&hInstance) ,it's OK.Why?What does
&hInstance mean?
Below is the code:
// cl register.cpp -link gdi32.lib user32.lib
// g++ -o register.exe register.cpp -mwindows
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ) ;
LRESULT CALLBACK ProcSplitter(HWND hwnd, UINT Msg, WPARAM wParam,
LPARAM lParam);
void outError( UINT num )
{
TCHAR buf[10] ;
_stprintf( buf, TEXT("%u"), num ) ;
MessageBox( NULL, buf, NULL, 0 ) ;
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE,
PSTR, int )
{
WNDCLASSEX wc ;
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_GLOBALCLASS ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.hIconSm = LoadIcon(NULL, (LPCTSTR)IDI_APPLICATION) ;
wndclass.lpszClassName = "First" ;
if( ! RegisterClassEx( &wndclass ) )
{
MessageBox( NULL, TEXT( "Can not register window class First" ),
"Error", MB_ICONERROR ) ;
outError( GetLastError() ) ;
return 0 ;
}
wc.cbSize = sizeof(wc);
wc.style = CS_GLOBALCLASS ;
wc.lpfnWndProc = ProcSplitter ;
wc.cbClsExtra = 0 ;
wc.cbWndExtra = 0 ;
wc.hInstance = hInstance ;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION) ;
wc.hCursor = LoadCursor(NULL, IDC_ARROW) ;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH) ;
wc.lpszMenuName = NULL ;
wndclass.hIconSm = LoadIcon(NULL, (LPCTSTR)IDI_APPLICATION) ;
wc.lpszClassName = "Second" ;
if( ! RegisterClassEx( &wc ) )
{
outError( GetLastError() ) ;
return 0 ;
}
return 0 ;
}
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam,
LPARAM lParam )
{
switch( message )
{
case WM_DESTROY :
PostQuitMessage( 0 ) ;
return 0 ;
}
return DefWindowProc( hwnd, message, wParam, lParam) ;
}
LRESULT CALLBACK ProcSplitter(HWND hwnd, UINT Msg, WPARAM wParam,
LPARAM lParam){
switch (Msg)
{
default:
return DefWindowProc(hwnd, Msg, wParam, lParam);
}
}