Skip to content
Search

WindowsApi:Handle

lastmod 2018-08-17created 2016-04-131 edits

(Can also be reached via HWND.)

HWND vs HINSTANCE

The HINSTANCE handle usually holds the start address at which a running Win32 program is loaded in memory. It usually has a value like 0x00400000, which means the executable module is loaded at that memory address. Functions that load resources often refer to this handle value, because it specifies that the resource should be read from the executable module loaded at the address hInstance points to, among the executable modules loaded in memory (exe, dll, etc.). Besides this, it is also used to point to a specific module loaded in memory, as with HMODULE (in Win32 you can consider it to serve the same function and hold the same value as HINSTANCE), for example when you need to get a function address from another DLL, as with GetProcAddress(). This variable is sometimes made global, because it is used frequently by many functions; given how Win32 is implemented, many functions need to read that value, and since the value comes in only once in WinMain(), it is stored in a global variable. Of course, there are also ways to avoid making it global, such as using C++ classes like MFC does. Whichever approach you take, the purpose is the same.

HINSTANCE means the program's instance identifier (handle). Simply put, it refers to the actual entity in memory of a program that, as an executable file, is merely a shell. So if you run several copies of a program, each of them is called a program instance, each running program has a unique value, and the instance handle is used as the identifier to distinguish the running programs.

HWND means the program's window identifier (handle), an identifying value used to distinguish the program's windows. Here, there is a difference between a window and a program, because in Windows programming a single program can have many windows. The most representative of a program's window handles is the handle of the parent window. So it may be quickest to understand that most of the work in Windows programming uses window handles obtained through these HWND values. This is because not only windows but also many different controls are all created as windows and own a handle value.

HINSTANCE is not a handle to the program but a handle to the module containing the program code. That is, for a program to run, the file containing the program code must be loaded into a specific area of memory and prepared so that instructions can be read and executed one by one. To manage these chunks of program code loaded into memory, Windows assigns a kind of unique identification number, and this is the instance handle, HINSTANCE. Since there must be at least one module with the code of the executable that runs the process loaded into memory, the OS passes the instance handle for the executable's module as an argument to the application's WindMain. If a process loads several modules to run the program, a single program may be allocated and using several instance handles. (Of course, a single module may also be shared by several processes.) A typical example is IE; simply put, each time a DLL file is used, one instance handle is created for that DLL module. Of course, once a DLL is loaded, it is shared among other programs. Since a program does not have just one HINSTANCE, and an instance handle is not tied to just one program, it is clearly unsuitable for specifying the output target of a program. Of course, the instance handle was never an identifier designed with screen output in mind; it is simply a resource created to manage chunks of program code. The resource that the Windows OS manages for screen output is HWND. As you can see with MSN Messenger and the like, there is no guarantee that a program uses only one window. Rather, 99.99% of programs consist of more than one window. Even if a program's window looks like a single target area, in reality it is split by component into separate windows, and each window handles only the screen output and user input for its own part. Naturally, a single program uses more than one HWND-type object, and in this situation, to perform a specific action at a specific location, you have no choice but to distinguish the target area by HWND.

If you look at the structure contents of HWND and HINSTANCE, their properties are completely different. HWND holds information about the window itself, and HINSTANCE holds information about the currently running instance. Since an instance has a unique number across the whole operating system (think of them as the running windows), you cannot get the instance from an HWND, but if you have the HINSTANCE you can get information about the HWND.

See also