Wrong calling convention? .NET V1.X expect stdcall callbacks.
Willy.
Willy,
Here's the situation: I have the dll, the lib and the C++ .h file. The
declaration of the method looks like:
ERROR Log(void (*CallBack)(LOG_LEVEL LogLevel, const char *src, const char
*dest));
In the C# code:
[DllImport("mydll.dll",CharSet=CharSet.Auto, SetLastError=true,
CallingConvention=CallingConvention.StdCall)]
private static extern void Log([MarshalAs(UnmanagedType.FunctionPtr)]
CallBack callback);
Where the delegate is declared as:
private delegate int CallBack(
[MarshalAs(UnmanagedType.U4)] uint logLevel,
[MarshalAs(UnmanagedType.LPStr)] string src,
[MarshalAs(UnmanagedType.LPStr)] string dest);
The callback method looks like:
private static int Log(uint logLevel, string src, string dest)
{
Console.WriteLine(src + " :: " + dest);
int hh = Marshal.GetLastWin32Error();
//error returned here:: 126 The specified module could not be found.
ERROR_MOD_NOT_FOUND
return 0;
}
So, I get the callback from the dll, with the right data passed as
parameters. The only problem is when the execution exists the scope of the
Log method it shows that error with buffer overrun. GetLastWinError returns
an ERROR_MOD_NOT_FOUND (126) error.
I'll check if the dll uses stdcall. Any other ideas ?
> Wrong calling convention? .NET V1.X expect stdcall callbacks.
>
[quoted text clipped - 11 lines]
> > idea where this comes from and why ? is it something in the dll I'm
> > calling ?
I haven't been able to find the documentation, is there a way to change the
delegate calling convention in .NET 2.0? I don't like having to use the IL
hack.
> Wrong calling convention? .NET V1.X expect stdcall callbacks.
>
[quoted text clipped - 11 lines]
> > idea where this comes from and why ? is it something in the dll I'm
> > calling ?