如何使用pinvoke使用cdecl回调
我有一个具有cdecl回调的ac库。 我如何从c#中使用这些。
一切似乎都说他们必须是stdcall回调
要清楚:
delegate int del(); [dllimport("mylib.dll",CallingConvention=CallingConvention.Cdecl)] public static extern int funcwithcallback(del foo);
其中del必须被称为cdecl-wise
看看这个。 该function自1.1以来就已存在,因此它应涵盖您正在使用的任何.NET版本。 您只需指定CallingConvention即可。
MSDN上的CallingConvention文档
您还可以在Code Project上查看这篇文章:
在C#中使用_CDECL调用约定
编辑:另外,这是FreeImage.NET的一个例子。
static FreeImage_OutputMessageFunction freeimage_outputmessage_proc = NULL; DLL_API void DLL_CALLCONV FreeImage_SetOutputMessage(FreeImage_OutputMessageFunction omf);
然后在C#方面,简单地说:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void FreeImage_OutputMessageFunction(FREE_IMAGE_FORMAT format, string msg); [DllImport(dllName, EntryPoint="FreeImage_SetOutputMessage")] public static extern void SetOutputMessage(FreeImage_OutputMessageFunction omf);
- 使用.NET 2.0编译,使用2005编译器!!
- 反转参数方向。
它的工作原理是2005编译器添加了一些安全保护代码。
编辑:如果你可以在本机代码中填充垫片,请不要尝试这个。