Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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:

WNDCLASSEX wc;
hInstance = GetModuleHandle(NULL);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
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:

#includes

LRESULT CALLBACK WndProc()
{
}

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.

share|improve this question
1  
+1 for stepping through with a debugger! –  thedaian Mar 2 '12 at 21:58

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
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?? –  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.

share|improve this answer
    
Ack sorry, typing this at work. I meant RegisterClassEx(). I've edited it. Thank you! –  Lokked Mar 2 '12 at 22:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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