Global HINSTANCE?

This is a discussion on Global HINSTANCE? within the Windows Programming forums, part of the Platform Specific Boards category; I'm reading Schildt's Windows 98 Programming from the Ground Up, and this question is from pg 116 for anyone with ...

  1. #1
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916

    Global HINSTANCE?

    I'm reading Schildt's Windows 98 Programming from the Ground Up, and this question is from pg 116 for anyone with a copy.

    When calling DialogBox from within the window function, the handle to the current instance is required as a parameter. So Schildt declares a global HINSTANCE and sets it equal to the HINSTANCE passed to WinMain. I know that global variables should be avoided when possible, and I was wondering if there is a way around doing it like he does. (If it's covered later in the book, just tell me)

    Code:
    HINSTANCE hInst;
    
    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nWinMode)
    {
         ...
         hInst=hThisInst;
         ...
    }
    
    LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
         ...
         DialogBox(hInst, "MyDB, hwnd, (DLGPROC) DialogFunc);
         ...
    }
    Away.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Try GetModuleHandle(0);

    That should get the HINSTANCE of the current module

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Thanks. I knew there had to be something better than globals.
    Away.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Or GetWindowLong() and ask for GWL_HINSTANCE.

    With books and tutorials, (mine included), it is common to use globals and other things which are "frowned upon", because the book/tut is trying to demonstrate routines not really programming technique.

    I tend to use globals rather than functions I have not covered, and don't wish to cover in a particular tut.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well I have to say that despite the displeasing nature of globals the HINSTANCE thing is one that I would typically say is best. I'm sure calling GetModuleHandle(NULL) doesn't add too much overhead to a program, but it does add some.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I agree. Think about it, HInst really is relative to the application as a whole, it is a value of global use, why should it not, therefore, have global scope?

    I think a lot of the arguments used to deprecate globals, (and certain other practices), are a little thin with todays IDE's, and some of the hoops people jump through to avoid them are ridiculous.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    All a HINSTANCE represents it the load address of your exe in the processes address space. Almost always 0x40000000 as that's the address almost all linkers specify by default.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Global objects and exceptions
    By drrngrvy in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2006, 07:37 AM
  3. Global Variables, include files and classes
    By sharpstones in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2005, 10:06 AM
  4. defining and using a global class
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2004, 08:51 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM

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