pinvoke and callbacks

J

Jeff Lederer

I have created a simple test C# console program that calls an unmanaged C
subroutine in a DLL where one of the arguments is a callback to the C# code.
I noticed that when the callback has no paramaters, it works fine. But when
there are parameters, I get the exception: "Runtime failure check #0: The
value of ESP not properly saved..."

Here are the facts: The delegate declaration:

The signature of the Pinvoke method:
[DllImport("Clips.DLL", CharSet=CharSet.Ansi)]
public static extern bool EnvAddRouter(querydlg queryFunction);

The callback method:

public static int FindTrace(IntPtr pEnv, string logicalName)
{
return logicalName.CompareTo("wtrace")==0 ? 1 : 0;
}


the function call within the C# program:

The declaration of the the function call setting up the callback:

And the unmanaged call to the callback function inside the c routine:

where:
void *theEnv;
char *logicalName;

Using the debugger, I saw that the arguments are passed from the unmanaged
code to the callback routine correctly.

I am using version 1.0 of the IDE. Is this a bug in the CLR?

PS This is not my only example, I have other calls with similar problems.
 
B

BMermuys

Hi,

<snip>

Probely a problem with calling convention, try :

extern BOOLEAN EnvAddRouter( int (__stdcall *queryFunction)(void *,char
*) );

-or- change project properties to use stdcall as default.

HTH,
greetings
 
J

Jeff Lederer

By making the compiler switch /Gz (__stdcall calling convention), the
callback now returns.

Thanks!
 

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

Top