GetModuleHandle (NULL) vs hInstance

This is a discussion on GetModuleHandle (NULL) vs hInstance within the Windows Programming forums, part of the Platform Specific Boards category; In creating resources for Windows programs I've noticed a lot of people generally use "GetModuleHandle (NULL)" when an instance argument ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    207

    GetModuleHandle (NULL) vs hInstance

    In creating resources for Windows programs I've noticed a lot of people generally use "GetModuleHandle (NULL)" when an instance argument is required.

    Example:

    Code:
    g_hbmBall = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BALL));
    as opposed to:

    Code:
    g_hbmBall = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BALL));
    I guess this is personal preference, but I was just wondering why people prefer getting a null module handle? Is it a "just the way I was taught" kind of thing or is there some advantage to it?

    mw
    Blucast Corporation

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    GetModuleHandle(NULL) will get the handle to the EXE module. This may not be what you want if in a DLL (if you write reusable code, you should consider that it may be used in a DLL at some point). Ideally, the hInstance should probably be saved in a global variable at startup. However, code posted on this forum is often in the form of standalone functions, so it is easier to use GetModuleHandle(NULL) than explaining that a global variable must be used.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    The instance of your application is not readily availible in your WinProc, for example. So, instead of relying on it being passed as a parameter, you can GetModuleHandle(..) to obtain it. But as, in your example, it's just some global bitmap, it probably is just something you could initialize anywhere. Another thing that you can do, is make a "g_hInstace", (a global variable) which you initialize at the start of your WinMain, or pull the program's instance out of the CREATESTRUCT which is passed in the lParam of a WM_CREATE message, if you want to use it in your WinProc:

    Code:
    	static HINSTANCE hInstance;
    	switch(msg)
    	{
    	case WM_CREATE:
    	{
    		hInstance = (reinterpret_cast<CREATESTRUCT*>(lParam))->hInstance;
    More info here: http://msdn.microsoft.com/library/de.../wm_create.asp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  4. Compiling 3rd party code problem me too
    By siavoshkc in forum C Programming
    Replies: 1
    Last Post: 09-12-2007, 05:55 AM
  5. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM

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