Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine the current HINSTANCE?

The HINSTANCE of a win32 application is passed to WinMain, but is there any other way of determining the current HINSTANCE (in case you couldn't tell, I'm very new to win32 programming!)? I need to create a window inside of a library and (since the library is cross platform), id prefer not to have to pass it in.

like image 509
dicroce Avatar asked Nov 17 '09 16:11

dicroce


People also ask

How do I get hInstance?

Just make hInstance = NULL when registering the class and pass NULL to CreateWindow() and you're good to go.

What is hInstance?

hInstance is something called a "handle to an instance" or "handle to a module." The operating system uses this value to identify the executable (EXE) when it is loaded in memory. The instance handle is needed for certain Windows functions—for example, to load icons or bitmaps.

What is WinMain function?

Rather, WinMain is the conventional name for the user-provided entry point to a Windows program. The real entry point is in the C runtime library, which initializes the runtime, runs global constructors, and then calls your WinMain function (or wWinMain if you prefer a Unicode entry point).


2 Answers

If memory serves, GetModuleHandle(NULL); returns the instance handle.

like image 125
Jerry Coffin Avatar answered Oct 19 '22 00:10

Jerry Coffin


__ImageBase is your friend, especially in the case of libraries.

Note that the linked blog post (by R. Chen, although not the same post as the one linked by Brian Bondy) is worth reading (including the comments!)

like image 20
Serge Wautier Avatar answered Oct 19 '22 01:10

Serge Wautier