Hi there,
I recently just about managed to complete a little challenge I set myself by getting a windows class registered and window created from functions in a DLL instead of from the WinMain function.
However... there's one part of it that is more of a C++ question than a Windows question so I thought I'd ask it here.
It concerns a variable not going out of scope when I thought maybe it would. If I try and declare a HWND variable like this:
Code:
HWND hwnd;
createRunWindow(hInstance, hDll, hwnd);
The compiler will complain (C4700 error code I believe) that the variable is used before being initialised. If I however do this:
Code:
HWND hwnd = nullptr;
or HWND hwnd = 0;
createRunWindow(hInstance, hDll, hwnd);
I get some crazy errors with the linker (probably something windows specific). But if I make the createRunWindow function to do this:
Code:
HWND createRunWindow(HINSTANCE hInstance, HMODULE hDll)
{
....some time later
HWND hwnd;
CREATEWINDOW _createWindow = (CREATEWINDOW)GetProcAddress(hDll, "createWindow");
_createWindow(&hInstance, WindowProc, &hwnd);
return hwnd;
}
And have the function called in WinMain like this:
Code:
HWND hwnd = createRunWindow(hInstance, hDll);
All works nicely. But here's where it becomes a general C++ question. The WinMain function continues afterwards needing a valid HWND handle for other functions within it to work.
So my question is, given that the function returns a hwnd before exiting, does that mean the line
Code:
HWND hwnd = createRunWindow(hInstance, hDll);
is ok because it's been initialized to something before the temporary variable in the function went out of scope?
Thanks