• ### Popular Now

• 12
• 12
• 9
• 10
• 13

#### Archived

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

# Win32 Function Pointers

This topic is 5448 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

OK, this is my second attempt at this ! I have declaread a global function pointer to my windows message loop. Ihave done this so that I can pass this as a parameter to my windows Objects as they are created. I am getting errors when I try to compile (MSVC 6) The declaration is as below;

LRESULT CALLBACK WndProc(	HWND	hWnd,					// Handle For This Window

UINT	uMsg,					// Message For This Window

WPARAM	wParam,					// Additional Message Information

LPARAM	lParam);					// Additional Message Information

LRESULT CALLBACK (lptrWindProc*) (HWND,UINT,WPARAM,LPARAM);	//Function Pointer to the windows Message Lopp


Then in my applications main loop I have lptrWindProc = (&WndProc); Is there a better way of passing the WndProc to a windows class as I make it, and why if I am declared as above do I get the following errors ? e:\CODING_PROJECTS\Win32_OGLBase\Win32_OGLBase.cpp(35) : error C2059: syntax error : ''('' 119) : error C2065: ''lptrWindProc'' : undeclared identifier e:\CODING_PROJECTS\Win32_OGLBase\Win32_OGLBase.cpp(119) : error C2440: ''='' : cannot convert from ''long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)'' to ''int'' This conversion requires a reinterpret_cast, a C-style cast or function-style cast I have tried putting the CALLBACK in brackets but that doesn''t help. Can anyone shed some light onto this, I am going mad slowly trying to resolve what should be a simple problem ! Thanks, Bp. , when I want a new window I am

##### Share on other sites
Not sure if it will help, but try declaring the variable lptrWindProc first.

VOID* lptrWindProc = NULL;

I should probably be working now...

##### Share on other sites
Nope, just get a different error message..

e:\CODING_PROJECTS\Win32_OGLBase\Win32_OGLBase.cpp(37) : error C2165: ''left-side modifier'' : cannot modify pointers to data

##### Share on other sites
I''m not sure, but try it without the & sign. I think functions are already pointers. I''m not sure about this. I think i read it in a C++ book a while back when I was in school.

Try it and see.

##### Share on other sites

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
///HANDLE MESSAGE
}

Then when you create your object from WNDCLASSEX.

WNDCLASSEX winclass;

Just refer to the name of the CALLBACK function.. like this

winclass.lpfnWndProc = WindowProc;

That''s it..

##### Share on other sites
Thanks AP, but I was doing that.
I was trying to create a fnction pointer so that I could pass it as a parameter to my CWndClass:CWndClass(...) and create an instance of any window that referred to the Main Message Function.

I have it fixed now, I needed the CALLBACK inside the bracket as well. Sometimes Trial and Error will save the day

If anyone has any comments on how I am doing this, then please tell me. I don''t know if it''s the best way, but it is the way I think it should be done.

Bp.

##### Share on other sites
A generalised, global function pointer looks like this:

return-type (*pointer-name)(argument-type, argument-type);

You can have the argument names in there too, if you want, but it doesn''t make any difference.

Then, a function name on it''s own is a pointer. So when you do MyFunction(), you''re saying ''jump to the code pointed to by MyFunction.'' In the same way, you can create a function pointer of your own (as shown above) and do MyFunctionPointer() - although stylistically, you should probably do (MyFunctionPointer)().

Later, you get into Functors.. and then the party really gets fun...