4

I am trying to create a popup window when I click a button on my main window, and I am using the CreateWindowEX function for that purpose. However, the CreateWindowEx function recieves the HINSTANCE type variable as its second last parameter. Given that I am making this new window from inside the WndProcedure when a button is being clicked, the HINSTANCE variable in the WinMain function will not be available to me because it is not a global variable.

The following code was obtained from the WndProcedure callback function:

            case IDC_BUTTON PRESSED:
            {

                const char g_szClassName2[] = "POPWIND";
                const char WndName2[] = "POPUP WINDOW";

                HWND invisWindowHandle = CreateWindowEx(0,
                      g_szClassName2,
                      WndName2,
                      WS_OVERLAPPEDWINDOW,
                      200,
                      200,
                      800,
                      500,
                      NULL,
                      NULL,
                      hInstance,//THIS IS THE VARIABLE I AM HAVING TROUBLE GETTING
                      NULL);

                ShowWindow(invisWindowHandle, 3);// SW_SHOWNORMAL);
                UpdateWindow(invisWindowHandle);
             }

How would I go about getting the hInstance variable I need so that the CreateWindowEx function works from inside the WndProcedure callback function?

Note: I considered making the hInstance variable global, but I am not sure if the same hInstance has to be used for every single window my application makes.

2
  • It's an optional parameter (as stated in the documentation), but if you really want to, GetModuleHandle(nullptr)
    – chris
    Aug 16, 2014 at 15:19
  • Oh ok, and do you know of any benefits by including it? Aug 16, 2014 at 15:22

1 Answer 1

14

The winapi carries some baggage that it inherited from Windows versions 1 through 3. They used co-operative multi-tasking, running the same "task" was implemented by using the same program and passing it different hInstance values. Important tricks to get a multi-tasking GUI operating system crammed into 640 kilobytes of RAM.

That stopped being relevant 21 years ago with the release of Windows NT, shortly followed by Windows 95. Versions of Windows that first supported the notion of a process. An important concept for security and system stability, there is a very strong isolation between running programs. Accordingly, the relevance of the HINSTANCE disappeared, every process gets the same value. A pseudo handle. Much the same for a HMODULE, it is simply the address in memory where the module was loaded.

Accordingly, passing NULL for the hInstance argument is fine. It means "the current process" and you can't create a window for another process. And storing the hInstance you get from WinMain() in a global variable is fine as well, there is only ever one.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.