Hi All,
I have a C dll exporting the following:
`````````````````````````````````````````````````` ``
typedef void (__cdecl *pCallBack)(int i);
int __cdecl Foo(pCallBack pFunc);
I have written an interop dll:
````````````````````````````````````````
public class FooInterop
{
public delegate void pCallBack(int i);
[DllImport("Foo.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern int
Foo([MarshalAs(UnmanagedType.FunctionPtr)]pCallBack pFunc);
}
And I'm trying to call it this way:
```````````````````````````````````````````````
class Class1
{
[STAThread]
static void Main(string[] args)
{
int res = FooInterop.Foo(new FooInterop.pCallBack(Class1.FooCB));
Console.WriteLine(res);
}
static public void FooCB(int i)
{
Console.WriteLine(i);
}
}
For some reason when leaving FooCB I get the following error:
"Run-Time Check Failure #0 - The value of ESP was not properly saved
across a function call. This is usually a result of calling a
function declared with one calling convention with a function pointer
declared with a different calling convention."
I'm assuming this is because 'Foo' (in the C dll) is defined as
__cdecl and 'FooCB' (in .NET assembly) is an __stdcall. Am I right?
How do I tell the marsheler that C# method should behave as __cdecl
in .NET 1.1?
How do I solve this problem?
BTW defining the typedef as stdcall (I.e. typedef void (__stdcall
*pCallBack)(int i); )
works OK, but I'm not surethis is the solution I need.
Any ideas?
Thanks!