Link to home
Start Free TrialLog in
Avatar of cdesigner
cdesignerFlag for Russian Federation

asked on

Pointer to function

I have function

void Function (char* string);

I need get pointer (void*) to this function
how?
Avatar of gothick
gothick

Why do you need void*?

Just "Function" should evaluate to a pointer to the function, e.g.

void Function(char* string);
....
void (*fptr)(char*); /* Declare fptr as a pointer to a function returning void */
                          /* and taking a char* parameter */
fptr = Function;

If you want it void*, then just cast it.
Avatar of cdesigner

ASKER

but I have definition in header

typedef void (WINAPI *pFunction)(char *string);

and you method not compile....

error C2440: '=' : cannot convert from 'void (__stdcall CMyClassDlg::*)(char *)' to 'void (__stdcall *)(char *)'
How about you say what you're actually trying to do.  It seems like you actually want a pointer to a _member_ function, which is different.  You'll also have a problem passing a pointer to a member function to something which doesn't expect it, as the implied first parameter (this) will not be expected.
how I can do?

function to need pointer :

void WINAPI Set (UINT Code, void *pFunc);

help please
send me request for answer
I send you points
gothick is right.  The problem is that the function is a non-static member of a class.  A pointer to a non-static member function is different than a pointer to a non-member function and there is no posibility to convert between them.  You must make the member function a static member function or you must use a special type of pointer called a "pointer-to-member"  This type of pointer is very different than other pointers and cannot be converted to  a "regular" pointer.  

It would help if we knew what you are trying to do.
You 're using Borland with non-borland extensions maybe . Try casting using (*pfunction) . For example , some borland modes require the cast of (DLGPROC) , (WNDPROC) etc in "special" function pointers.


Now I understood my source , this other problem
>void Function (char* string);
 > I need get pointer (void*) to this function how?

You can do this simply by
void (*fp) (char* string);

fp getpointer(void*)
{
   fp = Function;
   return fp;
}

If it is a C function and not a functon of C++ Class.

>how I can do?
>      function to need pointer :
>    void WINAPI Set (UINT Code, void *pFunc);
>      help please

for this you should do like this
void WINAPI Set(UINT Code, (void (*fp) (char* string)) )
ramshakal,  have you read the question history?
question answered by gothick
Should I repost my answer for you to grade?
yes
send me answer
I send you points
ASKER CERTIFIED SOLUTION
Avatar of gothick
gothick

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial