473,216 Members | 1,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 software developers and data experts.

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 1698
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Satish Chimakurthi | last post by:
Hi all, My question is surely a basic one, but somehow, I am not able to figure it out. I have a python file "satish.py" as follows: *satish.py* def main(): y()
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
2
by: Edlueze | last post by:
Greetings: I have two functions and I would like to pass the ParamArray gathered from one function to the other function. For the purposes of this post, let's say that they are calculating...
4
by: Pushkar Pradhan | last post by:
I want a function to execute another function, which I pass to it, sometimes it may be mm_6r6c_6r6c, mm_2r2c_2r2c, ... etc. thus this style. double exec_basecase(void (*func)(double *a, double...
1
denizvb
by: denizvb | last post by:
Hai programmers, Do you know How to write a function inside another function in c
1
by: krishnasamy | last post by:
Hi, I am retrieving the image from DLL through vc++ dll. Actually Image is retrieving in Callback function of main function. When I writing that image file inside of the Callback function then...
3
by: jbanas | last post by:
In MS Access i have a function that is calculating the distance between two locations using the latitude and longitudes for two points assuming the earth is flat and the lat and long are...
7
by: dries | last post by:
Hello lads, I'd like to do the following in C: 1. The user writes a function with predefined arguments and return value in a separate file. 2. The program is compiled as my written 'main'...
4
by: soni2926 | last post by:
hi, is it possible to pass a function into another function as a parameter? Say i have these: function SaveMe(text) {...} function SaveMeNow(text) {...}
8
by: HighlightReel44 | last post by:
I need help writing a program which will display a menu with options Square, Cube, Fourth Power and Quit. Accept S,C,F,Q from the user non case sensitive. Use integers between 0 and 100 and...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.