更改C#委托的调用约定CDECL [英] Changing a C# delegate's calling convention to CDECL

查看:2718
本文介绍了更改C#委托的调用约定CDECL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我我是用DotNet1.1的时候有这个问题,C#

I have had this problem with C# when I was using DotNet1.1

问题是这样的。我有一个非托管的DLL,它有一个函数,它接受一个函数指针(其它参数中)。当我宣布在C#代码中的DllImport,我通过一个委托。但在C#中的代表有 STDCALL 调用约定而非托管函数需要一个 CDECL 函数指针。因此,我的幼稚的做法导致崩溃。后来我发现: http://www.codeproject.com/KB/cs/cdeclcallback的.aspx 有些家伙写了能够通过改变委托的调用约定一个很好的书房,我的理解,MSIL黑客。事情进展得很顺利,直到...

The problem is this. I have an unmanaged dll, which has a function which takes a function pointer (among other arguments). When I declare the DLLImport in C# code, I pass a delegate. But the delegates in C# have stdcall calling convention whereas the unmanaged function expects a cdecl function pointer. Thus my naive approach resulted in crashes. Then I found the following: http://www.codeproject.com/KB/cs/cdeclcallback.aspx Some guy wrote an excellent library that enables changing calling convention of the delegate by, as I understood, MSIL-hacking. Things went well, until...

我迁移到VS2008和.NET的新版本。根据这个版本的上述文库不起作用。我不是一个真正的C#或.NET的专家,而且,说实话,我不太了解他的书房做了什么(虽然它是开源的),所以我甚至不希望尝试,以使其适应新的.NET。但是我希望C#的新版本有我的问题提供一些更好的解决方案。

I migrated to VS2008 and the new version of .NET. Under this version the abovementioned library doesn't work. I am not really a C# or .NET expert, and, to tell the truth, I barely understand what his library does (although it's open-source), so I don't even want to try to adapt it to new .NET. However I am hoping that newer version of C# has some better solution available for my problem.

如此,所以专家,请帮我在臀部疼痛:)

So, SO experts, please help me with my pain in the buttocks :)

推荐答案

默认情况下的p / Invoke包装系统将委托一个STDCALL函数。您可以通过使用 UnmanagedFunctionPointer 属性:

By default the p/invoke system wraps your delegate in a stdcall function. You can change the generated wrapper's calling convention by using the UnmanagedFunctionPointer attribute:

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void MyDelegate();

这篇关于更改C#委托的调用约定CDECL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆