Pointer to functions

Hi,
Can anyone explain me the concept of Pointer to a function? I have a Visual C++ project in which I need to create a pointer to MAPI functions MAPIAllocateBuffer, MAPIAllocateMore and MAPIFreeBuffer which in turn need to be used in CreateIProp() function. I need to know the way to proceed as I am not very familiar with Pointer to a function.

Any help in this regard is appreciated.

Thanks,
Anitha
[427 byte] By [Anitha B] at [2007-11-16 15:15:50]
# 1 Re: Pointer to functions
You can declare pointer to function in this way:

ret_type (*your_pointer)(type1, type2,.....)

where
ret_type is return type of the function
your_pointer is the name of your variable
type, type2,... - types of the parameters function receives

Example:
int sum(int a, int b)
{
return a + b;
}

void main()
{
int (*fp)(int, int); //declare sum as pointer to function, that return int and receives two int parameters
fp = sum; //name of the function is a pointer to it, but you may use fp = &sum, too
int a = fp(5,6); //call function via pointer
}
Rado Nikolov at 2007-11-10 5:41:25 >
# 2 Re: Pointer to functions
Tbanks for the info.
I need to make pointers to some MAPI functions for example MAPIAllocateBuffer.
I have written the code like this:

1) SCODE (* pFunc) (unsigned long, LPVOID FAR *);

2) pFunc = MAPIAllocateBuffer;

3) if(CreateIProp((struct _GUID*)NULL,pFunc(unsigned long(1028),pBuffer),
pFunc1, //Another pointer to a function
pFunc2, //Another pointer to a function
0, &lpPropData) != S_OK)
{
AfxMessageBox("Error in creating the IPropData object");
}

For the above code, I get a compile error in line 2 as follows:

error C2440: '=' : cannot convert from 'long (__stdcall *)(unsigned long,void ** )' to 'long (__cdecl *)(unsigned long,void ** )'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast

Can you please guide me on how to correct the error?
Anitha B at 2007-11-10 5:42:25 >
# 3 Re: Pointer to functions
Actually your MAPI function must be compiled as _stdcall (functions passed to Windows API must be _stdcall), not _cdecl (this is the default). Try to declare pointer in this way:
SCODE (_stdcall * pFunc) (unsigned long, LPVOID FAR *);
Rado Nikolov at 2007-11-10 5:43:22 >
# 4 Re: Pointer to functions
Try declaring your function pointer as __stdcall:SCODE (__stdcall* pFunc) (unsigned long, LPVOID FAR *);

Dave
dlorde at 2007-11-10 5:44:27 >
# 5 Re: Pointer to functions
Hi,
Thanks for the help.
I could get rid of the errors in the declaration of pointer to function. But I am still held up with 1 error in CreateIProp() function . I have written the function as follows :

if(CreateIProp((struct _GUID*)NULL,pFunc(unsigned long(1028),pBuffer),
pFunc1(....),
pFunc2(....),
0, &lpPropData) != S_OK)
{
AfxMessageBox("Error in creating the IPropData object");
}

The error is :

CreateIProp : Cannot convert parameter 2 from 'long' to 'long(_stdcall*)(unsigned long,void **)
Conversion from Integral type to pointer type requires reinterpret_cast, C-Style cast or function-type cast

Can you please guide me to solve this error?
Anitha B at 2007-11-10 5:45:25 >
# 6 Re: Pointer to functions
I tried using reinterpret_cast when calling the function and it compiles without any errors.

Tbanks for all the help.

Anitha
Anitha B at 2007-11-10 5:46:24 >
# 7 Re: Pointer to functions
Actually the CreateIProp function is declared:
SCODE CreateIProp(
LPCIID lpInterface,
ALLOCATEBUFFER FAR * lpAllocateBuffer,
ALLOCATEMORE FAR * lpAllocateMore,
FREEBUFFER FAR * lpFreeBuffer,
LPVOID lpvReserved,
LPPROPDATA FAR * lppPropData
);
This means that you must pass addresses of MAPIAllocateBuffer, MAPIAllocateMore and MAPIFreeBuffer functions as params to CreateIProp. So call it in this way:
SCODE CreateIProp(
NULL,
pFunc1,
pFunc2,
pFunc3,
0,
&pPropData
);
Rado Nikolov at 2007-11-10 5:47:28 >
# 8 Re: Pointer to functions
Your second argument is a call to the pFunc function, not pFunc itself: pFunc(unsigned long(1028),pBuffer), so you're passing the return value from a call to pFunc, which apparently is a long. Quite rightly, the compiler complains that it can't convert this return value into a function pointer!

Casting with reinterpret_cast will shut the compiler up, but it won't make your code correct.

Dave
dlorde at 2007-11-10 5:48:30 >
# 9 Re: Pointer to functions
No.

Do not use reinterpret cast to solve your problem.

You must know exactly why the compiler complained. The reinterpret_cast is dangerous since you are asking the compiler to "shut up", but there may be a valid reason why it's complaining. As soon as you run your program, you'll find out when the program doesn't work correctly.

As a humorous analogy, the reinterpret_cast allows you to turn cars into elephants, dogs into tables, and trees into stop signs. Are you going to put your cup of coffee on your dog, when you actually expected a table? I hope you get my point.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-10 5:49:27 >