Why is szAppName static?
I am unsure why szAppName in the following program (and in virtually every other windows application I have seen) is declared static:
Since WinMain() is called only once per program execution, I can't see what it is to be gained by making szAppName static. Can anyone explain why this is done? Thanks.
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
static TCHAR szAppName = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
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.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
return 0 ;
hwnd = CreateWindow (szAppName,
ShowWindow (hwnd, iCmdShow) ;
//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)
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
return 0 ;
return DefWindowProc (hwnd, message, wParam, lParam) ;
I don't see much advantage to starting a program that way to begin with...
Originally Posted by DL1
Window creation for anything more complex than a single control can involve many variables and should probably be moved over to a separate function so the variables can be disposed of after the windows are created.
WindMain may only be called once per run of a program, but because of the message dispatcher it is active the entire time the program is running. When WindMain exits, so does your program.
szAppName is optional, you can use string litterals in it's place. It's probably always declared as static because that's the way the guy who wrote the example did it and since then tens of thousands have simply copied the example...
As you become more familiar with the Windows API you will discover a lot of stuff that is purely arbitrary decision making... (I'll spare you the rant about function calls that don't work as described...)
EDIT: My bad.
There is no reason I can see to make that particular variable a static.
I suggest it is to do with the age of the code. ie GetMessage() now returns -1 if it fails (ie gets an invalid message).
And it is Hello Win98....
Have a look at some code from this century.
Yes, the code is pretty old. And it seems that in pre-ANSI C, locally defined character arrays initialized with strings had to be static.