我有一个带有 cdecl 回调的 C 库。我如何在 c# 中使用这些。
一切似乎都在说它们一定是 stdcall 回调
需要明确的是:
delegate int del();
[dllimport("mylib.dll",CallingConvention=CallingConvention.Cdecl)]
public static extern int funcwithcallback(del foo);
其中 del 必须按 cdecl 方式调用
看看这个。该功能自 1.1 起就已存在,因此它应该涵盖您正在使用的任何 .NET 版本。您只需指定 CallingConvention。
MSDN 上的 CallingConvention 文档 https://msdn.microsoft.com/en-us/library/z6f9ef9d(v=vs.110).aspx
您还可以看看代码项目上的这篇文章:
在 C# 中使用 _CDECL 调用约定 http://www.codeproject.com/KB/cs/cdeclcallback.aspx
编辑:另外,这是来自 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)