问题描述
我有了CDECL回调的C库。如何使用这些从C#。
I have a c library that has cdecl callbacks. How can I use these from c#.
一切都似乎是说,他们必须STDCALL回调
Everything seems to say that they must be stdcall callbacks
是明确:
delegate int del();
[dllimport("mylib.dll",CallingConvention=CallingConvention.Cdecl)]
public static extern int funcwithcallback(del foo);
在这里德尔必须调用CDECL明智
where del must be called cdecl-wise
推荐答案
看看这个。该功能已经出现自1.1所以它应该包括你使用任何.NET版本。你只需要指定CallingConvention。
Take a look at this. The functionality has been around since 1.1 so it should cover whatever .NET version you are using. You just have to specify the CallingConvention.
的
您也可以看看这篇文章的代码项目:
You can also look at this article on Code Project:
使用_cdecl调用约定
Using the _CDECL calling convention in C#
编辑:另外,下面是一个FreeImage.NET例如
Also, Here is a example from FreeImage.NET.
static FreeImage_OutputMessageFunction freeimage_outputmessage_proc = NULL;
DLL_API void DLL_CALLCONV
FreeImage_SetOutputMessage(FreeImage_OutputMessageFunction omf);
然后在C#的一面,简单地说:
Then on the C# side, simply:
[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);
这篇关于如何使用的PInvoke的cdecl回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!