Jump to content
  • Advertisement

Archived

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

Sand_Hawk

Function pointers

This topic is 5657 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, I understand function pointers, but I got a little problem. I am making a standard class for handling dialogboxes with MDI. I am up to the part of making it universal. I created a struct to create a new MDI window containing the dialogbox template and the pointer to the DlgProc. It looks likes this:
typedef struct tagCHILDCREATESTRUCT
{
    LPCTSTR lpDialogTemplate;
    BOOL (* CALLBACK  DlgProc) (HWND, UINT, WPARAM, LPARAM);
} CHILDCREATESTRUCT, * PCHILDCREATESTRUCT;
 
To create a new MDI window I have to do this:
CHILDCREATESTRUCT ccs;
ccs.lpDialogTemplate = MAKEINTRESOURCE(IDD_DIALOG1);
ccs.DlgProc          = MainDlgProc;
MDIForm->CreateChild(&ccs);
 
However, on the function pointer assignment, I get this error:
rror C2440: ''='' : cannot convert from ''int (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)'' to ''int (__cdecl *)(struct HWND__ *,unsigned int,unsigned int,long)''
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
 
How can I fix this? I don''t really understand what I am doing wrong. Sand Hawk

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster

  

typedef BOOL (*CALLBACK DLGPROC) (HWND, UINT, WPARAM, LPARAM);

typedef struct tagCHILDCREATESTRUCT
{
LPCTSTR lpDialogTemplate;
DLGPROC DlgProc;
} CHILDCREATESTRUCT, * PCHILDCREATESTRUCT;


CHILDCREATESTRUCT ccs;
ccs.lpDialogTemplate = MAKEINTRESOURCE(IDD_DIALOG1);
ccs.DlgProc = (DLGPROC)MainDlgProc;MDIForm->CreateChild(&ccs);


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Heterogenous calling conventions.

MainDlgProc needs to be declared as __stdcall or CALLBACK or a host of other synonyms.

Thus, the prototype:

int __stdcall MainDlgProc(HWND hWnd,WPARAM wParam,LPARAM lParam);

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
No. Don''t go casting __cdecl into __stdcalls like that, or you''ll die an untimely death.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Just a calling convention. It determines the order in which parameters are pushed onto the stack and whose responsibility it is to clean up. __stdcall is equivalent to the CALLBACK macro. But __cdecl (the default calling convention assigned to non-OOP functions) is an altogether different calling convention from __stdcall. You can''t just go casting between different calling conventions willy nilly or else your functions will get their parameters mixed up. So declare your MainDlgProc as __stdcall, or else it''ll be set to __cdecl. And you can''t just cast the function pointer from __cdecl to __stdcall. It''s apples and oranges.

Share this post


Link to post
Share on other sites
why should i set my default dlgproc to that? what about the others. I use some funktion pointers in my gui for my game and im not using non of that, default i guess __cdecl.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Because that''s what the API wants. Hence the error message when you don''t give it what it wants.

Share this post


Link to post
Share on other sites
IF you need to typecast functions you are in trouble because the compiler has a set of rules to manage the stack,heap, and paramters depending on the type of call.

your WndProc (DlgProc) callback function MUST exist, and according to your structure it may or may not. Basically if you cannot put the static keyword in front of a call back function, your implementation design is wrong.

your WndProc must be defined in such a way that it has global accessibility where an instance of itself exists, but in a structure, it may or may not exist...

if you want, make a seaparate function pointer to a non-callback method, then forward your call back function to the specific structure method. This of course will require another layer of function overhead, but realisticly you will never notive a perfromance drop...

I hope this explanation helps.

www.cppnow.com

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!