Calling derived classes function from pointer in wndproc

Hi,

I have a number of windows classes, all derived from Windows_Base, which, when created, pass 'this' pointers to the wndproc as the lparam on the WM_CREATE message. I want to recover these pointers and use them to call the On_WM_CREATE function in the derived class. This function is present as a virtual function in Windows_Base.

The problem is that the function called is always that of Windows_Base, not the derived class.

(I am not used to playing around with VOID* and reinterpret cast, and suspect the problem lies somewhere there!)

Any help appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Windows_Base
{
	friend LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam); 
public:
	Windows_Base(WINFO,int);
	virtual ~Windows_Base(){}
	
//...

	virtual bool On_WM_CREATE(HWND hwnd){return false;}

//...
};

class Main_Window:public Windows_Base
{
public:
	Main_Window();
	~Main_Window();
//...
	bool On_WM_CREATE(HWND hwnd);
//...
};

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)   
{   
	Windows_Base* window = NULL;   

	//-----   
	// Process "create" message seperately in order to associate the instance of    
	// the BaseWindow being created with its window handle, such that it can   
	// be retreived when other messages are being processed   

	if( msg == WM_CREATE )   
	{   
		// Get the BaseWindow instance to be associated with the windows handle
		// By accessing the createstruct passed in the lparam as the WM_CREATE message.
		// (LPCREATESTRUCT is simply a pointer to such a structure.)
		LPCREATESTRUCT createStruct = reinterpret_cast<LPCREATESTRUCT>(lparam);   
		// And then retreiving the pointer passed in the Create Window call.
		window = reinterpret_cast<Windows_Base*>(createStruct->lpCreateParams);
		if(!window)   
		{   
			throw Base_Exception(_T("Failed to get pointer to instance of BaseWindow being created"),
				_T("void On_WM_CREATE(HWND hwnd,LPARAM lparam,W*& window)"),_T("Global Window Functions.cpp, Line 29."));
		} 
		
		// Associate the Window instance with the windows handle   
		SetLastError(0);   
		if( !SetWindowLongPtr(hwnd, GWLP_USERDATA,reinterpret_cast<LONG_PTR>(window)))   
		{   
			DWORD err = GetLastError();   
			if( err )   
			{   
				std::basic_string<TCHAR> ErrMsg=_T("Could not associate BaseWindow instance with window handle./r/nGetLastError returned: ")+num2T(err);   
				throw Base_Exception(ErrMsg,_T("void On_WM_CREATE(HWND hwnd,LPARAM lparam,W*& window)"),_T("Global Window Functions.cpp"));
			}   
		}  

		// Pass HWND to window object to undertake creation tasks
		window->On_WM_CREATE(hwnd);

		// Signal that the window is created and ready to go   
		SetEvent(window->w_created);   

		return 0;   
	}

//...

}
My guess is that your window is not fully constructed.

If you are creating the window (ie: calling CreateWindowEx) in your constructor, then that's the problem.

IIRC, Windows will generate and send a WM_CREATE message before CreateWindowEx returns. This means that your WndProc will be running before your Window_Base object is fully created.

And as you should know, you can't call derived functions via a virtual pointer until the object is fully constructed, since the derived class hasn't been constructed yet.
You're on the money. Thanks!
Topic archived. No new replies allowed.