WNDPROC in a class... no idea--PART 2

Started by
15 comments, last by Verg 19 years, 9 months ago
Whenever i get "Invalid Window Handle" from CreateWindowEx, its because in the WndProc, i've been using the m_hWnd variable.
When you call CreateWindowEx, windows sends a few messages to the WndProc, then returns from the call. If you write "m_hWnd = CreateWindowEx(...), then m_hWnd isn't filled in until the call returns. So, if you use the m_hWnd variable in the WndProc during those few calls (I think you get WM_NCCREATE, WM_CREATE, WM_SIZE, and possible one or 2 others), then you'll end up calling DefWindowProc with a NULL hWnd parameter. Windows doesn't like that, and takes it as meaning your WndProc returned failure for the WM_NCCREATE message (the first one the WndProc gets), so CreateWindowEx fails with the Invalid Window Handle error.

My reply in the previous thread shows a way around this problem.
Advertisement
Very simple question: even if you can get the address to the WndProc, what do you intend to do with it? Call it yourself?

You DO realize that means generating all the messages that Windows generates... and dispatching those yourself, to everything with a handle. That means interfacing with hardware like the kernel does... so you may as well rewrite that, too.

To some people, the words "you can't breathe water" is just an annoying challenge, so good luck.
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
Reply to Evil Steve:

Why do you put "this" on the last paramter to CreateWindowEx? I mean what do it do in order to create the static WNDPROC? Or for other purposes? Could you explain that?
When Evil Steve passes the "this" pointer as the last parameter in CreateWindowEx(), it becomes data accessible by the window. You can access it in the struct passed in one of the parameters with the WM_(NC?)CREATE message, and later on you can access it with GetWindowLong( GWL_USERDATA, ... ). So even thought the WndProc is a static function, it can now look up the class instance associated with the window. So now the window is "properly" tied to the WinClzCtrl class you wrote. :)

P.S., please use [ source ] tags around your code.
This looks like an example of when to use C#.
Not giving is not stealing.
Now I got another question.

I believe many people, like me, who try to put the WNDPROC inside a class is mostly for officence (didn't spell right) and security (I believe).

If we put the WNDPROC as a static function how safe is that? And of course we have to do some tricks to asscess our class memeber functions.

Anyway, my question is the advantage and disadvantage (or it's not perferred) of having ONLY 1 static WNDPROC in the class--just like a normal WNDPROC function.


To thedevdan:

Well, I guess C# probably makes it easier, but I'm not just trying to get the job done. I'm looking for more advanced understanding about window programmings.



Thank you
Quote:Original post by psae0001
Now I got another question.

I believe many people, like me, who try to put the WNDPROC inside a class is mostly for officence (didn't spell right) and security (I believe).

If we put the WNDPROC as a static function how safe is that?


As in... other object can see/access it? That's not too big of a deal in this case. What's the harm? Actually, you can set the access specifier for WndProc to private.

Quote:Anyway, my question is the advantage and disadvantage (or it's not perferred) of having ONLY 1 static WNDPROC in the class--just like a normal WNDPROC function.


Biggest advantage is that you only have to code it once. Now, there are hundreds (probably thousands) of messages that Windows generates; all you have to do is pass the values you care about
to another object and process them there.

Those tutorials (esp. the one on www.relisoft.com) show how to add a user parameter to CreateWindowEx... that's where you put the pointer to the object that will retrieve your messages in the WndProc. Simply get the parameter (using GetWindowLong) and do a static_cast... then you can pass all messages in the WndProc to the object.

[Edited by - Verg on July 10, 2004 5:03:08 PM]
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]

This topic is closed to new replies.

Advertisement