470,632 Members | 2,554 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,632 developers. It's quick & easy.

using a function pointer in another function

AK
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
Nov 17 '05 #1
5 1592
AK wrote:
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
Nov 17 '05 #2
AK
Using __stdcall for both pointer & function doesn't change the errors
removing __stdcall from both doesn't help either..

"Tamas Demjen" wrote:
AK wrote:
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

Nov 17 '05 #3
> 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;
--
Vladimir Nesterovsky
e-mail: vl******@nesterovsky-bros.com
home: http://www.nesterovsky-bros.com
Nov 17 '05 #4
AK
That does the job - I needed to define pf as a static variable.

Thanks for the help,
ak

"Vladimir Nesterovsky" wrote:
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;
--
Vladimir Nesterovsky
e-mail: vl******@nesterovsky-bros.com
home: http://www.nesterovsky-bros.com

Nov 17 '05 #5
"=?Utf-8?B?QUs=?=" <AK@discussions.microsoft.com> wrote in message news:<C9**********************************@microso ft.com>...
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]
Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Satish Chimakurthi | last post: by
4 posts views Thread by Pushkar Pradhan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.