我有一个带有cdecl回调的c库。如何在c#中使用这些。
一切似乎都在说它们必须是stdcall回调。
需要说明的是:
delegate int del();
[dllimport("mylib.dll",CallingConvention=CallingConvention.Cdecl)]
public static extern int funcwithcallback(del foo);其中,del必须被称为cdecl-wise
发布于 2011-02-09 06:58:33
看看这个。该功能从1.1开始就存在了,因此它应该涵盖您正在使用的任何.NET版本。您只需指定CallingConvention。
CallingConvention Documenation at MSDN
您还可以在Code Project上查看这篇文章:
Using the _CDECL calling convention in C#
编辑:另外,这里有一个来自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);发布于 2011-02-09 06:55:23
它的工作归功于2005编译器添加的一些保护代码。
编辑:如果您可以在本机代码中创建填充程序,请不要尝试此操作。
https://stackoverflow.com/questions/4939450
复制相似问题