如何使用pinvoke使用cdecl回调

pm1*_*100 9 c# pinvoke callback

我有一个具有cdecl回调的ac库.我如何从c#中使用这些.

一切似乎都说他们必须是stdcall回调

要清楚:

delegate int del();
[dllimport("mylib.dll",CallingConvention=CallingConvention.Cdecl)]
public static extern int funcwithcallback(del foo);
Run Code Online (Sandbox Code Playgroud)

其中del必须被称为cdecl-wise

Mat*_*att 15

看看这个.该功能自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);
Run Code Online (Sandbox Code Playgroud)

然后在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);
Run Code Online (Sandbox Code Playgroud)