HINSTANCE from main()

vince koch vkoch@nvsmedia.com
Tue, 24 Apr 2001 17:18:00 -0400

Axel -

While browising FAQs and message boards I found a question you had
posted a few months ago.  I decided to write
you in case you had not learned the answer yet.  (I remember this answer
was pretty tough to find when I needed it!)


Hi all,

I tried to write a Win32 program in with GHC 4.05 but I am stumbeling
the definition

 (ClassStyle,  -- style
  HINSTANCE,   -- hInstance
  MbHICON,     -- hIcon
  MbHCURSOR,   -- hCursor
  MbHBRUSH,    -- hbrBackground
  MbLPCSTR,    -- lpszMenuName
  ClassName)   -- lpszClassName

which looks fine if I'd knew where to get hInstance. main doesn't give
to me. Can specify something like

winMain hInstance ... =

as my main program entry? Or a more sensible question: Is there any
userguide on how to use the Win32 library?



You can get a handle to the current instance of your app in one of 2
ways that I am aware of...
1) You can replace your main() with WinMain (defined below) in which
case windows will pass you the instance,
and you can either pass it into other functions or use it directly

int WINAPI WinMain(
  HINSTANCE hInstance,  // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,      // pointer to command line
  int nCmdShow          // show state of window


2) you can call GetModuleHandle (defined below) and pass in NULL for the
lpModuleName and cast the resulting
HMODULE into a HINSTANCE as shown below

HMODULE GetModuleHandle(
  LPCTSTR lpModuleName   // address of module name to return handle for

To Use:
    HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);

Good luck!