Archived

This topic is now archived and is closed to further replies.

Evil-Prey

WNDCLASS question

Recommended Posts

wndclass.lpfnWndProc =WinProc; Ok from my understanding here you are passing the function WinProc as a pointer to the windows procedure. The problem Im having is that now, I''ve wrapped my basecode, rather someone else''s base code into a class. But when I try to pass the WinProc function into wndclass.lpfnWndProc I get an error error C2440: ''='' : cannot convert from ''long (__stdcall NarC::*)(struct HWND__ *,unsigned int,unsigned int,long)'' to ''long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)'' There is no context in which this conversion is possible How would I pass the function? Thanks in advance. I hope I''ve provided enough information.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I''m not a 100% sure about this one but i''m pretty sure. Try something like this:

wndclass.lpfnWndProc = (WNDPROC)WinProc;

or maybe it was

wndclass.lpfnWndProc = (WINPROC)WinProc;

anyways try those and if they don''t work i don''t know what happened. Later.

Share this post


Link to post
Share on other sites
>>

'long (__stdcall NarC::*)(struct HWND__ *,unsigned int,unsigned int,long)

<<

The issue is that you can not pass a WIN API function a class pointer........... This means by default C++ ... When calling a member function with being declared like so

myClass {int myFunction();};

when you call

myClass *X = new myClass;
X->myFunction();

C++ in the background really sends this

X->myFunction(myClass *obj);

Kinda odd..... But the only way to get around the issue is that you must declare the Callback function or API * to function as
"static"
IE

myClass {
static int myFunction();
};

that should resolve that issue. One other issue you will run into is that the static functions can be called from non-static. However not the reverse...... Or variables........
That is almost impossible to get around....................

Hope that helps......................





[edited by - afterburn on March 20, 2002 7:36:11 PM]

[edited by - afterburn on March 20, 2002 7:37:27 PM]

Share this post


Link to post
Share on other sites
There is no simple fix. You can't pass a pointer to a class member function unless it's declared as static. A fix would involve thunking, something beyond what I'd like to explain, look at how ATL handles wndprocs for an example. A simple fix invloves setting the data for the window to be the pointer to the class and calling the function from the wndproc e.g.,

WhatEverClass *global_window = NULL;

void WhatEverClass::CreateWindow()
{
global_window = this;
hwnd = CreateWindow((pass whateverWinProc)...)
SetWindowLong(hwnd,0,(LONG)this);
}

LONG WhatEverClass::WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return 0;
}



static LONG WINAPI whateverWinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
WhatEverClass *window = (WhatEverClass*) GetWindowLong(hWnd,0);

if(!window)
window=global_window;

window->WinProc(hWnd, uMsg, wParam, lParam);

}

EDIT: Added global_window, window will be NULL while messages from CreateWindow are processed.

[edited by - milou212 on March 20, 2002 8:01:07 PM]

Share this post


Link to post
Share on other sites
This question has been answered a million times already. Why don''t you use the search?

As milou212 said, except you should call SetWindowLong in your WM_NCCREATE handler. This will also eliminate global_window.

Share this post


Link to post
Share on other sites
Where do you get the class pointer for use in WM_NCCREATE to use for SetWindowLong? As far as I know, you still need a global or static. If you know another way, I''d love to hear it, I''ve never been able to figure it out.

Share this post


Link to post
Share on other sites
this exact question has been anwserd at least twice on THIS forum.. search for it (topic was something like WinProc in class or something like that)

There are more worlds than the one that you hold in your hand...

Share this post


Link to post
Share on other sites
Or, look in MSDN. CreateWindow takes lpParam that you set to "this", and WM_NCCREATE''s lParam is an LPCREATESTRUCT which contains lpCreateParams that you set to this.

Also, MSJ has an article by Paul DiLascia that explains how MFC hooks up WM_CREATE using CBT hooks for stock windows like edit boxes for which you can''t change WndProc. If you understand what I''m talking about, you can search for it.

Share this post


Link to post
Share on other sites