首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过pinvoke使用cdecl回调

如何通过pinvoke使用cdecl回调
EN

Stack Overflow用户
提问于 2011-02-09 06:52:18
回答 2查看 8.2K关注 0票数 9

我有一个带有cdecl回调的c库。如何在c#中使用这些。

一切似乎都在说它们必须是stdcall回调。

需要说明的是:

代码语言:javascript
复制
delegate int del();
[dllimport("mylib.dll",CallingConvention=CallingConvention.Cdecl)]
public static extern int funcwithcallback(del foo);

其中,del必须被称为cdecl-wise

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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的示例。

代码语言:javascript
复制
static FreeImage_OutputMessageFunction freeimage_outputmessage_proc = NULL;
DLL_API void DLL_CALLCONV
FreeImage_SetOutputMessage(FreeImage_OutputMessageFunction omf);

然后在C#端,简单地:

代码语言:javascript
复制
[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);
票数 15
EN

Stack Overflow用户

发布于 2011-02-09 06:55:23

  1. 编译用.NET 2.0,使用2005 compiler!!
  2. Reverse参数方向。

它的工作归功于2005编译器添加的一些保护代码。

编辑:如果您可以在本机代码中创建填充程序,请不要尝试此操作。

票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4939450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档