Win32 conflict

Started by
4 comments, last by Staffan E 18 years, 10 months ago
I have a question. I have created a window class that creates that will create a window's class, register it, create the window, setup pixel format, and handle messages. The problem is it needs the instance for the program, and I've designed this engine I'm working on to not have a main.cpp with the main function, so the developer using the engine could do that. So I need to get the hinstance to the calss, but how? I know I could make an extra parameter, but there's only one instance. Is that just the only option then?
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Advertisement
You can use GetModuleHandle( NULL ) to get the handle to your program instance.
Hack my projects! Oh Yeah! Use an SVN client to check them out.BlockStacker
Quote:Original post by dbzprogrammer
I have a question. I have created a window class that creates that will create a window's class, register it, create the window


If you're creating the window, you have the HWND, correct?

HINSTANCE hinstance = (HINSTANCE)::GetWindowLong(hwnd, GWL_HINSTANCE);


HTH


my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
Oh.......so I can create a another HINSTANCE ans assign it GetModuleHandle(NULL)?

Or would i have to create it by doing the second one?
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Quote:Original post by dbzprogrammer
Oh.......so I can create a another HINSTANCE ans assign it GetModuleHandle(NULL)?

Or would i have to create it by doing the second one?


You can grab your programs HINSTANCE by doing either of those. If you have the HWND for the window, use the second one, otherwise use GetModuleHandle(NULL)

moe.ron
Yes, or rather, you can get a copy of the handle that was supplied to WinMain by argument. It's not really a new instance, since GetModuleHandle does not increase the reference count of the module. In other words you don't have to do any housekeeping. You can discard the handle when you don't need it anymore without having to release/free it in any way.

Take a look at this example at MSDN. It shows how to create a simple dialog box that uses GDI brushes to draw a pattern. That part is not related to you but if you scroll down to the bottom of the page and look at the last piece of code you have:

DialogBox((HANDLE)GetModuleHandle(NULL), (LPTSTR)"CustBrush", hWnd, (DLGPROC) BrushDlgProc);

This is the general way to create a dialog box. The first parameter of DialogBox( ) takes the HINSTANCE of the executable where the dialog box resource is located, in this case the program executable itself; the one supplied by WinMain.

So, this is an example of how to use the GetModuleHandle( ) function. In your case you could do something like this:

HINSTANCE hAppInst;hAppInst = (HANDLE)GetModuleHandle( NULL );

I hope this answers your question.

Good luck.
Hack my projects! Oh Yeah! Use an SVN client to check them out.BlockStacker

This topic is closed to new replies.

Advertisement