Thread: Why is szAppName static?

  1. #1
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99

    Why is szAppName static?

    Hello,

    I am unsure why szAppName in the following program (and in virtually every other windows application I have seen) is declared static:

    Code:
    #include <windows.h>
    
    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,
                      TEXT ("Application"),
                      WS_OVERLAPPEDWINDOW,
                      CW_USEDEFAULT,
                      CW_USEDEFAULT,
                      CW_USEDEFAULT,
                      CW_USEDEFAULT,
                      NULL,
                      NULL,
                      hInstance,
                      NULL) ;
    
         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 ;
    
         switch (message)
         {
         case WM_PAINT:
              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) ;
    }
    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.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by DL1 View Post
    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.
    I don't see much advantage to starting a program that way to begin with...

    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...)

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    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.

    http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx
    Last edited by novacain; 09-18-2010 at 10:07 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C#] Intercept SysListView32 item added
    By Devils Child in forum Windows Programming
    Replies: 9
    Last Post: 03-26-2010, 07:29 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  5. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM