Using a Direct3D Tutorial, I took the knowledge and encapsulated it into a class. The code builds in MSVC 2010 fine, and I've handled all compiler warnings pertaining to my project with the highest warning levels. When stepping through the code from the entry point, the program fails to RegisterClassEx(&wc). After which, the program is told nicely to exit. Shortly after the unique_ptr's destructor is run (not manually, but after the unique_ptr goes out of scope), the program crashes w/invalid memory access.

My WinMain looks something like this:

std::unique_ptr<Engine> j_engine(new Engine);
j_engine->CreateD3DWindow(title, width, height, fullscreen); //public method of Engine, similar to CreateD3DWindow from the tutorial

while(message looping)
// etc

//if loop is exited, let j_engine fall out of scope (my program has never reached this point)

The start of my CreateD3DWindow method contains VERY standard window creation code, and does not even contain a shred of a Direct3D call prior to the crash (RegisterClassEx()).

It is essentially:

hInstance = GetModuleHandle(NULL);
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); 
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;            
wc.lpszMenuName = NULL;     
wc.lpszClassName = "SomeName";

RegisterClassEx(&wc); // fails. GetLastError() == "Invalid Parameters". Stepping through WNDCLASSEX struct creation doesn't raise any invalid parameter flags.

I've also tried using ZeroMemory() after declaring WNDCLASSEX wc.

Two things register in my mind as I type this:
1. GetModuleHandle() is being called from inside the class, which exists on the heap and is referenced by a pointer from WinMain. Does this matter? Is it providing a valid handle?
2. WndProc declaration and definition is outside the class and outside the namespace of the class. Does this mean WNDCLASSEX.lpfnWndProc cannot see it? As such:



namespace xyz
class Engine

I'm out of ideas and my problem solving capabilities are waning. Any advice? I will certainly provide more information if necessary.

Specifically, I'd like to know why RegisterClassEx() is failing.

Thank you!

***Mentioned below that GetModuleHandle(NULL) will return a global handle for the given process, so the context of this call in this program should not matter.


From the looks of it you forgot to fill in the cbSize member of the WNDCLASSEX structure. Hence the API cannot determine what version of the structure you've got and hence returns the invalid parameter value.

  • \$\begingroup\$ Close Enough :). I set all available members for WNDCLASSEX (I was missing cbSize and hIconsm) and this worked. Commenting out any one of the members cause a failure. I wonder how those tutorials get away with not including all members?? \$\endgroup\$ – Lokked Mar 3 '12 at 1:48

You want to use RegisterClassEx with a WNDCLASSEX, I believe.

Also, GetModuleHandle(NULL) returns a process-global handle which will be valid for as long as your executable is loaded.

  • \$\begingroup\$ Ack sorry, typing this at work. I meant RegisterClassEx(). I've edited it. Thank you! \$\endgroup\$ – Lokked Mar 2 '12 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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