A trivial window hello world program

Started by
5 comments, last by LessBread 17 years ago
it only depend 2 lib. user32.lib and gdi32.lib ,I even curious it doesn't depend kernel.lib

#include <windows.h>


LRESULT CALLBACK  WndProc(HWND , UINT , WPARAM , LPARAM);
WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
     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))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     hwnd = CreateWindow (szAppName,                  // window class name
                          TEXT ("The Hello Program"), // window caption
                          WS_OVERLAPPEDWINDOW,        // window style
                          CW_USEDEFAULT,              // initial x position
                          CW_USEDEFAULT,              // initial y position
                          CW_USEDEFAULT,              // initial x size
                          CW_USEDEFAULT,              // initial y size
                          NULL,                       // parent window handle
                          NULL,                       // window menu handle
                          hInstance,                  // program instance handle
                          NULL) ;                     // creation parameters
     
     ShowWindow (hwnd, nShowCmd) ;
     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_CREATE:
         // PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
          return 0 ;

     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 ;
          
     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}


Advertisement
Everything seems o.k. at a glance. Is there something wrong with it?
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
I mean it get run without kernel.lib(window kernel dll?) support. but it must need user32.lib(user32.dll?) gdi32.lib to get compiled and run.right?
I believe that it would only require user32.lib as your calling no GDI function calls or any kernal calls.

You only need to link in the lib that corresponds to the dll if you call a function that resides inside the dll.

You can check for yourself in MSDN these functions. It will tell you somewhere down the bottom of the page which library it uses

The functions you are calling all reside inside user32.dll (I think, I looked up a few function which I thought were in gdi32.dll but actually were in user32.dll but I didn't check them all)

The actual functions would have an W or and A on the end depending if you are using unicode or not but the macro that the windows header makes for you takes care of this for you.

User32.dll
----------
RegisterClass <-- Not sure if this gets turned into RegisterClassEx
MessageBox
CreateWindow <-- Gets turned into CreateWindowEx
ShowWindow
UpdateWindow
GetMessage
TranslateMessage
DispatchMessage
BeginPaint
GetClientRect
EndPaint
DrawText
PostQuitMessage
DefWindowProc
I'm pretty certain that it'll need to be linked with kernel32.dll to run. Is your make file not listing kernel.lib along with the other .lib files? Or are you just trying to reduce the dependency of the resulting .exe file? Does it compile? Does it run? Have you used Dependency Walker to examine the resulting .exe to see if kernel32.dll is linked?

"This program requires Windows NT!"

"Hello, Windows 98!"

Which operating system are you running? Which are you targeting?

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
You are probably right LessBread that you may need link kernel32.lib aswell as the startup code may need to call upon it (Didn't think of that)


user32.dll and gdi32.dll are both dependent on kernel32.dll (and ntdll.dll on nt/2k/xp). The various crt dlls are probably dependent on it as well. msvcrt.dll is. The start up code probably calls GetModuleHandle for the hinstance argument and probably GetCommandLine for the command line argument and maybe GetStartupInfo for the show/hide argument. And at the other end of the execution cycle there's ExitProcess. And in the middle, it's likely that new/delete/malloc/free from the crt wrap calls to HeapAlloc and friends. And there's probably a need for RtlUnwind in there too as part of the exception frame in the start up code. There don't appear to be any explicit calls to crt but it could still be part of the assemblage.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement