From: Jef D. <jef...@ho...> - 2008-11-20 10:46:07
|
Frank Hrebabetzky wrote: > Posted a similar question on the Codeguru site; as things still don't > work I am suspecting an error on the native dll side. > > Problem: I wasn't capable to call a native library (GSL) from a C# > program running on .NET. So I wrote a simple C program with similar > prototypes, compiled it with MinGW/gcc into a native dll, wrote a test > program in the same way which calls the dll, and wrote another test > program in C# under .NET which calls it too. Result: calling the dll > from the C test program running under MSys works, calling it from .NET > doesn't; it compiles, but I get a null reference error message at runtime. I did run into the same problem some time ago, see here: http://www.nabble.com/Using-a-mingw-DLL-from-C--td18586582.html The problem is caused by the difference in calling convention between the C DLL (cdecl) and the C# callback function (stdcall). You have to change the calling convention of the callback function to cdecl (.NET 2.x or higher only): [UnmanagedFunctionPointer (CallingConvention.Cdecl)] delegate double Func (double x, ref Parameters p); Or change the C DLL to expect a stdcall callback function of course. |