PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Strange CreateWindow behaviour in emulator

I was having some trouble testing some code in the emulator that worked well on my E-200, so I decided to track down the problem. After an hour or so of hunting, I finally got it down to a problem registering the window class.

Here is the scenario: I have a class wrapper for a window. In that class wrapper are funtions to register and create the window, as well as a static wndproc. On creation, I use setwindowlong to put a pointer to the specific class in its windows GWL_USERDATA. Then, in the wndproc, I get the GWL_USERDATA, cast it as a pointer to my class, and send the messages through to another function in the class that isn't shared.

I had two separate projects that I was using to play around with this. The first time I tried it, it worked great on both the emulator and the PPC. The second time I tried it, I found that I had to register the window class with a NULL wndproc and then use setwindowlong to set the GWL_WNDPROC to point to my static wndproc after I had created the window.

Every time I tried to create a window with the wndproc set when I registered the class, the program would kill itself when I called createwindow. It doesn't do this in the first project.

I have no idea what could be happening. Thoughts?
If you do:

Code:

HWND hWnd = CreateWindow (...);
SetWindowLong (hWnd, GWL_USERDATA, (DWORD) this);


And have a window proc
Code:

LRESULT MyWindowProc(HWND hWnd, ...)
{
MyWindowClass * p = GetWindowLong (hWnd, GWL_USERDATA);
// call the instance to handle the message
return p->windowProc();
}


Then you get into trouble because the CreateWindow call will call MyWindowProc with WM_CREATE before SetWindowLong has occured.

The typical way to get around this is to use CreateWindowEx instead of CreateWindow. This way you can send the this pointer of your window class instance using the lpParam parameter of the CreateWindowEx call. This parameter will then be available to you in the MyWindowProc as one of the parameters of WM_CREATE (see documentation for exactly how to get it).

A good idea is then to move the SetWindowLong call to where WM_CREATE is handled in MyWindowProc. I think the hWnd parameter is valid for WM_CREATE...

Sorry for the rough explanation...

The reason it sometimes works is probably just luck. You will call into some random part of memory. Sometimes it crashes other times it doesn't.
Thanks, that works perfectly. I don't know why I didn't realize that CreateWindow would send WM_CREATE to my wndproc. Thanks again.
superman wrote:
Thanks, that works perfectly. I don't know why I didn't realize that CreateWindow would send WM_CREATE to my wndproc. Thanks again.


It's really easy to overlook the WM_CREATE message. Guess how I learned about this... :)