Jump to content
  • Advertisement
Sign in to follow this  

A trivial window hello world program

This topic is 4210 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

it only depend 2 lib. user32.lib and gdi32.lib ,I even curious it doesn't depend kernel.lib
#include <windows.h>

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

Share this post

Link to post
Share on other sites
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?

Share this post

Link to post
Share on other sites
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.

RegisterClass <-- Not sure if this gets turned into RegisterClassEx
CreateWindow <-- Gets turned into CreateWindowEx

Share this post

Link to post
Share on other sites
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?

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!