Winmain()

This is a discussion on Winmain() within the Windows Programming forums, part of the Platform Specific Boards category; Can anyone please explain me the parameters passed to Winmain() ?...

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278

    Winmain()

    Can anyone please explain me the parameters passed to Winmain() ?

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdParam, int nCmdShow)
    The first parameter hInstance is a unique identifier for your program. It's like HWND. HWND is a unique identifier for a window. hInstance is the identifier for a program whether or not it has a window. hPrevInstance is the identifier of any instances of your program that are already running. If there are no other instances of your program then it is 0. I don't know what cmdParam is. I've never used it. nCmdShow tells how window should be shown. It could be SW_SHOWMAXIMIZED, SW_SHOWNORMAL, SW_SHOWMINIMIZED, etc... It's just a suggestion but most programs follow it when they call ShowWindow().
    Don't quote me on that... ...seriously

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    cmdParam is the command line arguments as strings (much like argv) except they don't include the program name as the first argument.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Actually, cmdParam is the command line, period. What exactly that contains is up to several factors, including the way the program has been launched.

    hPrevInstance is always NULL. It's a relic from Win16, where all programs had to share a memory space. In Win32, the processes are separated and hPrevInstance is always NULL.

    In fact, even the name HINSTANCE is a relic: it designated a handle to this specific instance of the program. However, in Win32, HINSTANCEs are process-specific and have no meaning outside this particular process, so they no longer identify the instance.
    In fact, for most programs and most concurrently running instances, the hInstance WinMain parameter will almost always be 0x00400000. (Well, not entirely sure, but it's a hex number with a single 4 digit.)

    Win32 has an alias for HINSTANCE which much better describes its modern meaning: HMODULE. HINSTANCE/HMODULE doesn't identify a process, it identifies one particular code file. The hInstance passed to WinMain identifies the .exe file (by the address it has been loaded to, which is why it's almost always the same). The HMODULE/HINSTANCE returned by LoadLibrary, as well as that passed to DllMain, identifies the DLL.

    Window classes are associated with hInstances, as far as I know, for the sole purpose of knowing where to load the menu resource (if provided) from, nowadays. Historically, classes were also automatically unregistered based on their instance handle.
    Windows themselves are also associated with hInstances, but I somewhat suspect that this has now historical reasons only. Possibly it is also used to catch bugs.

    To get a handle that really identifies a process, you need the type HANDLE and a handle obtained by functions such as GetCurrentProcess() or OpenProcess().

    Finally, nCmdShow is intended to be passed straight on to ShowWindow. You can modify the value passed there e.g. by going to the right tab in shortcut properties and selecting a window mode there.
    Many programs override this value, choosing instead to remember how they were displayed on the previous run. In my opinion, that's preferable. Of course, you can also use hybrid schemes, like using nCmdShow unless it's SW_SHOWNORMAL (the default), in which case you load the previous position of the window.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WinMain entry point in .lib
    By cloudy in forum Windows Programming
    Replies: 8
    Last Post: 10-28-2006, 01:16 PM
  2. WinMain Paramaters.
    By C+noob in forum Windows Programming
    Replies: 8
    Last Post: 07-11-2005, 12:44 PM
  3. Understanding the Window WinMain Cycle
    By BillBoeBaggins in forum Windows Programming
    Replies: 4
    Last Post: 12-06-2003, 12:49 PM
  4. Silly WinMain question
    By Magos in forum Windows Programming
    Replies: 2
    Last Post: 09-07-2002, 10:38 AM
  5. Is this necessary: int APIENTRY WinMain
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 06-26-2002, 08:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21