Home > Mobile >  Is there a scoping problem with assigning my class window handle to a global in InitInstance() versu
Is there a scoping problem with assigning my class window handle to a global in InitInstance() versu

Time:12-13

In the code below as indicated the global assignment of the handle returned from the call to CreateWindowW() seemed to go out of scope. I could not access it outside of InitInstance(). When I moved the assignment into WinProc(), it remained in scope in other functions that I accessed it in.

HWND hWndTop;                                   // Handle to top window
       .
       .

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
       .
       .

   HWND hWnd = CreateWindowW(szWindowClass,....

   hWndTop = hWnd;   // This did not work.
       .
       .
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

   hWndTop = hWnd;    // This did work.

      .
      .
}

CodePudding user response:

...It's just that your WndProc is trying to use hWndTop before InitInstance can set its value. In other words, the issue is not "my assignment to hWndTop is being undone". The issue is "my assignment to hWndTop hasn't happened yet." – Raymond Chen

CreateWindow() will send a few messages to WndProc(), such as WM_(NC)CREATE, WM_GETMINMAXINFO, etc, before exiting to InitInstance(). So, hWndTop won't have been set yet for every message that WndProc() receives – Remy Lebeau

  • Related