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
Tamas Demjen - 27 Jul 2005 18:25 GMT
> 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
AK - 27 Jul 2005 19:11 GMT
Using __stdcall for both pointer & function doesn't change the errors
removing __stdcall from both doesn't help either..
> > U32 (*pf) (HANDLE, LPVOID); //declaring the function pointer
> >
[quoted text clipped - 6 lines]
>
> Tom
Vladimir Nesterovsky - 27 Jul 2005 20:29 GMT
> Here's what I have :
>
[quoted text clipped - 6 lines]
> 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;

Signature
Vladimir Nesterovsky
e-mail: vladimir@nesterovsky-bros.com
home: http://www.nesterovsky-bros.com
AK - 27 Jul 2005 22:46 GMT
That does the job - I needed to define pf as a static variable.
Thanks for the help,
ak
> > Here's what I have :
> >
[quoted text clipped - 19 lines]
> typedef U32 (__stdcall * func_pointer) (HANDLE, LPVOID);
> func_pointer pf = F;
Alon Fliess - 29 Jul 2005 01:20 GMT
> I'm writing a Windows Forms application in C++.NET.
> I've defined function F which takes 2 arguments a & b.
[quoted text clipped - 30 lines]
> 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]