WndProc in a class

Started by
6 comments, last by Ilthigore 21 years, 5 months ago
I am writing an OOP version of the first NeHe tutorial so I can call CApp::WinMain(...) in my WinMain statement and then just write InitGL and DrawGLScene. However I am having some problems with WndProc as when I am including it in my window class, it is type "long(__stdcall CApp::*)" instead of the usual "long(__stdcall*)". How can I change my code to make this work. Here is the offending line of code and the error message:
quote: wc.lpfnWndProc = (WNDPROC)WndProc;
quote: C2440: ''type cast'' : cannot convert from '''' to ''long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)'' [\quote] and if I take out the (WNDPROC) bit the error is:
C2440: ''='' : cannot convert from ''long (__stdcall CTLGLApp::*)(struct HWND__ *,unsigned int,unsigned int,long)'' to ''long (__stdcall *)(struct HWN
The error message ends at "HWN" so the line obviously ran out. Any help would be cool >>>>>>>>>>>>>>>>> Ilthigore <<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>>Ilthigore<<<<<<<<<<<<<<<<<
Advertisement
The problem is that it''s a member function. There is a way round it, though.

Although it''s hidden for us, C++ member functions have an extra parameter - the first parameter is the ''this'' pointer. So, when you create a member function which takes (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam), it actually takes (CTLGLApp *this, HWND hWnd...) and so on. That''s not what windows is asking you for in the WNDCLASS structure.

The way to get around it is to use a static member function (add the ''static'' keyword in front of the line in the class declaration).

The problem with *that* is that you lose all access to the specific window object itself. There''s ways of getting round that too, and they use GetWindowLong and SetWindowLong. I''ll leave the rest to you.

Superpig
- saving pigs from untimely fates
- sleeps in a ham-mock at www.thebinaryrefinery.cjb.net

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Go to baskuenen.cfxweb.net and download the "ooengine".
It too uses GetWindowLong for its window class. (Or engine class, I forgot).
Thanks for the note on declaring it static but how can I access the non-static members of my class with a static function. I now get about 10 errors saying this and it is probably because I lack a pointer to the class. Is another static data member containing a pointer to each instance of the class (using an array) and another static member with a counter of each of the classes an answer? If there is a neater way (that is a little cumbersome) can someone tell me.

>>>>>>>>>>>>>>>>>
Ilthigore
<<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>>Ilthigore<<<<<<<<<<<<<<<<<
Found out how to use GetWindowLong() thanks to the second reply. I didn''t actually notice it until I saw that I had 3 replies. Thank you to all those who replied and helped.

>>>>>>>>>>>>>>>>>
Ilthigore
<<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>>Ilthigore<<<<<<<<<<<<<<<<<
Sorry to bring this post up again but I have run into more difficulties. I am now having access violations. It compiles OK but doesn''t work. Click HERE to download the source zip for my lib project. Any MORE help would be great again.

>>>>>>>>>>>>>>>>>
Ilthigore
<<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>>Ilthigore<<<<<<<<<<<<<<<<<
Try passing your class object''s "this" pointer as the last parameter of the CreatWindwoEx function. This is what is supposed to get stored as USERDATA via the WM_CREATE processing code you added and restored for every other msg passed to your WndProc.

It would also help with future problems if you had some error checking in your code. Like an assert(pCApp) in your WM_CREATE processing. Or at least an if test to cause the create to fail so that the app gracefully abends.

You really should never use a pointer without first checking is viability.
look here

and here



[edited by - Crazemanx on November 4, 2002 2:38:31 PM]

This topic is closed to new replies.

Advertisement