using a function pointer in another function

G

Guest

I'm writing a Windows Forms application in C++.NET.
I've defined function F which takes 2 arguments a & b.
I need to use the pointer of F inside another function G & I can't
figure out how to do it properly.

Here's what I have :

U32 (*pf) (HANDLE, LPVOID); //declaring the function pointer

U32 __stdcall F (HANDLE a, LPVOID b) //defining F
{
......

return 0;
}


pf = F; //assigning the address of F to pf

//....(inside a Windows Form)...

G(...,pf(a,b),...); //using the function pointer inside of G


The compiler tells me at the pf = F line :
error C2501: 'pf' : missing storage-class or type specifiers
error C2373: 'PixelinkE::pf' : redefinition; different type modifiers
error C2440: 'initializing' : cannot convert from 'U32 (__stdcall
*)(HANDLE,LPVOID)' to 'int'

It seems that it doesn't like the way that pf is defined..
Is this the correct way to declare & use a function pointer or are there
better ways ?

Thanks,
ak
 
T

Tamas Demjen

AK said:
U32 (*pf) (HANDLE, LPVOID); //declaring the function pointer

U32 __stdcall F (HANDLE a, LPVOID b) //defining F

pf is not compatible with F, because they're using different calling
conventions. pf is __cdecl, F is __stdcall. Try

U32 (__stdcall *pf) (HANDLE, LPVOID);

Tom
 
G

Guest

Using __stdcall for both pointer & function doesn't change the errors
removing __stdcall from both doesn't help either..
 
V

Vladimir Nesterovsky

Here's what I have :
U32 (*pf) (HANDLE, LPVOID); //declaring the function pointer

U32 __stdcall F (HANDLE a, LPVOID b) //defining F
{
......

return 0;
}

pf = F; //assigning the address of F to pf

I believe last expression is in the some function's body, otherwise it's
invalid.
If you want to declare and initialize static variable you have to write

U32 (__stdcall *pf) (HANDLE, LPVOID) = F;

or

typedef U32 (__stdcall * func_pointer) (HANDLE, LPVOID);
func_pointer pf = F;
 
G

Guest

That does the job - I needed to define pf as a static variable.

Thanks for the help,
ak
 
A

Alon Fliess

=?Utf-8?B?QUs=?= said:
I'm writing a Windows Forms application in C++.NET.
I've defined function F which takes 2 arguments a & b.
I need to use the pointer of F inside another function G & I can't
figure out how to do it properly.

Here's what I have :

U32 (*pf) (HANDLE, LPVOID); //declaring the function pointer

U32 __stdcall F (HANDLE a, LPVOID b) //defining F
{
......

return 0;
}


pf = F; //assigning the address of F to pf

//....(inside a Windows Form)...

G(...,pf(a,b),...); //using the function pointer inside of G


The compiler tells me at the pf = F line :
error C2501: 'pf' : missing storage-class or type specifiers
error C2373: 'PixelinkE::pf' : redefinition; different type modifiers
error C2440: 'initializing' : cannot convert from 'U32 (__stdcall
*)(HANDLE,LPVOID)' to 'int'

It seems that it doesn't like the way that pf is defined..
Is this the correct way to declare & use a function pointer or are there
better ways ?

Thanks,
ak

Hi

There are two different problems:
The function pointer should be forward declared in the Form header file:
extern U32 (__stdcall *pf) (HANDLE, LPVOID);
Notice the "extern" keyword and the "__stdcall" keyword.
The assigment in the cpp file should be:
U32 (__stdcall *pf) (HANDLE, LPVOID) = F;

Alon Fliess
[VC++ MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top