Sign in to follow this  

Win32 API: Difference between handle to an Instance and handle to a Window.

This topic is 3579 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, first of all, this will help: hInstance -> handle to an instance hWindow -> handle to a window the title of the thread is pretty explanatory :) I'm a little bit confused about these concepts, the way I'm thinking is that the hInstance is the loaded program in memory, and the hWindow is the (what would be) visual part of the hInstance, so when we do the CreateWindow function we are "connecting", "linking" the hInstance to the hWindow. :p i dont really know if that's what it is, this is what makes sense to me. Any help would be appreciated Thanks :)

Share this post


Link to post
Share on other sites
Quote:
Original post by niner
hi,

first of all, this will help: hInstance -> handle to an instance
hWindow -> handle to a window

the title of the thread is pretty explanatory :)

I'm a little bit confused about these concepts, the way I'm thinking is that the hInstance is the loaded program in memory, and the hWindow is the (what would be) visual part of the hInstance, so when we do the CreateWindow function we are "connecting", "linking" the hInstance to the hWindow.

:p i dont really know if that's what it is, this is what makes sense to me.

Any help would be appreciated

Thanks :)
An application loaded into memory has a HINSTANCE. Every EXE or DLL has an HINSTANCE.
A window created by an application has a HWND. An EXE or DLL may create zero windows, in which case it won't have a HWND, it may create one window, or it may create hundreds. There's one HWND for each window, and for each control on a window (A control is a window itself).

The reason you need a HINSTANCE when you create a window (or rather when you register the window class) is that Windows needs to know what EXE or DLL your window procedure is in, so it can notify your program when something happens to the window.

This may be relevant.

Share this post


Link to post
Share on other sites
Quote:
Original post by niner
I'm a little bit confused about these concepts, the way I'm thinking is that the hInstance is the loaded program in memory, and the hWindow is the (what would be) visual part of the hInstance, so when we do the CreateWindow function we are "connecting", "linking" the hInstance to the hWindow.

There isn't a "visual part of the hInstance" until you create one. If you never called CreateWindow... well, give it a try. I guarantee you that a boring time will be had by all.

Share this post


Link to post
Share on other sites
the simplest explanation is that an HINSTANCE is just a number, an int that represents your program, so whenever you refer to your program, you use the HINSTANCE

the same with an HWND, it is just a number that represents your window, so when you have to do something that involves a window, you use the HWND

Share this post


Link to post
Share on other sites
This sounds suspisiously like that old newbie standy "how do I find the main window of a process". The reply of course being that there is no such thing as a "main" window. This discussion may be relevant.

To expand a bit on what you actually asked... An HINSTANCE is just a reference to an exe or dll. You need it because the exe or dll can say things like "load my menu, here's the id of the one I want". The OS needs to know who the heck you are before it can figure out where to start looking for "your" menu id. The HINSTANCE gives it that information.

Note in Win32 HINSTANCE values are local to the process - a.exe and b.exe might have the same HINSTANCE value, and foo.dll might have different HINSTANCE values in each process even though it's loaded in both. This was *not* true in Win16 where HINSTANCE's were system-wide. This leaks through in a few places, the most notable being the "hPrevInstance" parameter to WinMain which is completely useless today.

Similiarly HWND's are references to windows. There is no direct connection between HWND's and HINSTANCE's. The only reason you need to pass an HINSTANCE when you call CreateWindow is so CreateWindow can do things like find your menu resource - again, it needs to know who "you" are. Unlike HINSTANCE, HWND's are system-wide (well, session-wide anyway).

Share this post


Link to post
Share on other sites

This topic is 3579 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this