problem with callback

M

Mark

I'm trying to write a callback function, I've done it before,
but this time I seem to be having problems...

I define the following...

typedef int (* ENUMINTERFACE)(char* szIP);
void enumInterfaces(ENUMINTERFACE pFn);
int InterfaceEnumProc(char* szIP);

then at some point in my code I call
enumInterfaces(InterfaceEnumProc);

I get the compilation error
'cannot convert parameter 1 from int(char*) to int(_cdecl*)(char*)

whats the correct form for declaring callbacks.

Mark
 
V

Victor Bazarov

Mark said:
I'm trying to write a callback function, I've done it before,
but this time I seem to be having problems...

I define the following...

typedef int (* ENUMINTERFACE)(char* szIP);
void enumInterfaces(ENUMINTERFACE pFn);
int InterfaceEnumProc(char* szIP);

then at some point in my code I call
enumInterfaces(InterfaceEnumProc);

I get the compilation error
'cannot convert parameter 1 from int(char*) to int(_cdecl*)(char*)

This "_cdecl" business is apparently added by your compiler. You might
want to consider asking in a newsgroup that deals with your compiler...
whats the correct form for declaring callbacks.

This is explained in the FAQ 5.8.

V
 
G

Gianni Mariani

Mark said:
I'm trying to write a callback function, I've done it before,
but this time I seem to be having problems...

I define the following...

typedef int (* ENUMINTERFACE)(char* szIP);
void enumInterfaces(ENUMINTERFACE pFn);
int InterfaceEnumProc(char* szIP);

then at some point in my code I call
enumInterfaces(InterfaceEnumProc);

try:
enumInterfaces( & InterfaceEnumProc )
 
L

Larry I Smith

Mark said:
I'm trying to write a callback function, I've done it before,
but this time I seem to be having problems...

I define the following...

typedef int (* ENUMINTERFACE)(char* szIP);
void enumInterfaces(ENUMINTERFACE pFn);
int InterfaceEnumProc(char* szIP);

then at some point in my code I call
enumInterfaces(InterfaceEnumProc);

I get the compilation error
'cannot convert parameter 1 from int(char*) to int(_cdecl*)(char*)

The compiler expects the callback function to be a 'C' function, rather
than a C++ function. You've either defined it (or the typedef) in a 'C'
code block -or- the enumInterfaces() expects its param to be a 'C'
function (this is the case with most 3rd-party and Windows libs that
use callback functions - they must be 'C' functions).

This would be a valid callback function defined within a C++ module:

extern "C" int InterfaceEnumProc(char * szIP) { /* code here */ }
.
.
enumInterfaces(InterfaceEnumProc);
whats the correct form for declaring callbacks.

Mark

Larry
 
A

Aaron W. LaFramboise

I define the following...

For what it's worth, your testcase compiles with no errors or warnings
on all C++ compilers I have access to, and appears to be correct code
otherwise.

Are you sure you aren't omitting important details?

As part of the debugging process, and before asking questions, it is
helpful to create a so-called "minimal complete testcase." This way
you can have a clear idea in your mind of exactly what is failing, and
be sure that the problem actually is where you think it is, before
proceding further with debugging (by asking questions on Usenet).


Aaron W. LaFramboise
 
A

Alf P. Steinbach

* Mark:
I'm trying to write a callback function, I've done it before,
but this time I seem to be having problems...

I define the following...

typedef int (* ENUMINTERFACE)(char* szIP);
void enumInterfaces(ENUMINTERFACE pFn);
int InterfaceEnumProc(char* szIP);

then at some point in my code I call
enumInterfaces(InterfaceEnumProc);

I get the compilation error
'cannot convert parameter 1 from int(char*) to int(_cdecl*)(char*)

whats the correct form for declaring callbacks.

"__cdecl" is a Microsoft extension that denotes a machine code level calling
convention.

You have somehow managed to declare ENUMINTERFACE using that calling
convention.

I suspect you have fixed the code a bit before presenting it here, omitting
something relevant.

Btw. it's not a good idea to use all uppercase for non-macro names.

All uppercase should be reserved for macro names.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top