473,657 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,LPVOI D)' 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 1722
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******@nester ovsky-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******@nester ovsky-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******* *************** ************@mi crosoft.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,LPVOI D)' 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
2392
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
6521
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. When it completes, it can call a success, or a failure function. The names of these success, or failure functions will differ, and I'd like to know how I can pass the name of a function to my tool, and how my tool can call the function, using that...
2
9579
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 averages (they're actually processing a sequence of pairs of variants and the sequence is of unknown length). I want something like these two functions:
4
5020
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 *b, double *c), double *a, double *b, double *c, int numRowsA, int numColsA, int numColsB) { ...../*other code*/ func(a, b, c); ..... }
1
1519
denizvb
by: denizvb | last post by:
Hai programmers, Do you know How to write a function inside another function in c
1
1501
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 no problem in retrieving. At the same I want to send the Imagedata to the another function and write the same which cause the error. Here I given that code,
3
4135
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 perpendicular. I called that function distance. My issue in moving forward is taking the distance i am given from the above function and bringing it into another function i call function wp. I would like to say if the distance is less than 300 then calculate...
7
8420
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' program and the file containing the user function included. 3. When the program is invoked, the user is asked how his function is named (f.e. function1). 4. The 'main' program calls and executes the user function. The problem is, how can I cast the...
4
15777
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
2689
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 tabulate the selected function for all integers within this interval. I need to use a C++ function with a function pointer Thanks a lot in advance Need answers in next 4 days
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7354
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.