Jump to content
  • Advertisement

Archived

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

Bagpuss

Win32 Function Pointers

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Not sure if it will help, but try declaring the variable lptrWindProc first.

VOID* lptrWindProc = NULL;



I should probably be working now...

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Your Code should be...

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 this post


Link to post
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 this post


Link to post
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...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!