Reputation: 347
I use CreateWindow
to create the main window. hInstance
in WNDCLASSEX
specifies the instance under which the class is registered. So I need hInstance
in CreateWindow
function to find it.
I use CreateWindow
to create a button. No user-defined WNDCLASSEX
now. The BUTTON's WNDCLASSEX
is global. But I still need to pass an hInstance
to the function? Why and what is the right value to pass?
In Microsoft's example it's (HINSTANCE)GetWindowLong(hMainWnd, GWL_HINSTANCE)
, but I have no idea of what it is.
Please also tell me if I had anything wrong.
Thanks.
Upvotes: 5
Views: 20120
Reputation: 596
When you create a window, you specify the class of which an instance is created with a string (lpClassName
) in the first parameter of CreateWindow
.
hInstance
is used for identification in case more than one DLL referenced contains a class with the same name.
For more information see https://devblogs.microsoft.com/oldnewthing/20050418-59/?p=35873
Upvotes: 3
Reputation: 51
I found in MSDN dated from 2001 for CreateWindow
and CreateWindowEx
hInstance
Windows 95/98/Me: [in] Handle to the instance of the module to be associated with the window.
Windows NT/2000/XP: This value is ignored.
Upvotes: 5
Reputation: 61970
But I still need to pass an hInstance to the function? Why and what is the right value to pass?
As for the why, it would be a bit pointless (at least from a winapi perspective) to make a whole new function that takes everything but that and just uses the parent's or something when all other parameters still have a use.
I personally don't know for sure what the right value to pass in is, but I use GetModuleHandle(nullptr)
, which should be equivalent to the instance passed into WinMain
. It would also be the same as the one Microsoft's example uses (which gets the instance used to create the parent window) if that's what you use to create the parent window. The difference would come in if using a different application's window as the parent. The other main option I see used is nullptr
/NULL
, which has worked the same way as the aforementioned options every time I have used it.
If there's a subtle difference today between using NULL
and the application's HINSTANCE
, I'd like to know, but either of those should work fine for creating child controls on your windows.
Upvotes: 2