Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2005

Tip: Looking for answers? Try searching our database.

using a function pointer in another function

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AK - 27 Jul 2005 15:19 GMT
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]

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.