为C#托管代码创建C ++包装器? [英] Creating a C++ wrapper for C# managed code?

查看:86
本文介绍了为C#托管代码创建C ++包装器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图采用与我发现的大多数文章相反的方式。我需要创建一个非托管的C ++ DLL作为C#DLL的入口。原因是我需要将函数暴露给Firebird / Interbase作为UDF。我不知道C ++,我正在寻找帮助为我的托管代码创建一个简单的包装器。我的C#DLL有一个名为SOUNDEX的函数(暂时)接受一个varchar(50)字符串并返回一个char(4)变量。

I am trying to go the opposite way of most articles I have found. I need to create an unmanaged, C++ DLL to serve as an entry into a C# DLL. The reason is that I need to expose functions to Firebird/Interbase as UDFs. I do not know C++ and am looking for assistance in creating a simple wrapper for my managed code. My C# DLL has one function (at the moments) called SOUNDEX that accepts a single varchar(50) string and returns a char(4) variable.

推荐答案

你可以通过使用C ++ / CLI和混合模式(托管+非托管)项目来实现。你可以在C ++ / CLIref类中使用常规C ++类。你可以真正包装一个常规C ++类或结构包围在包装类的实现中使用的一些ref类型。



其他一切都取决于你的问题。



请注意,将某些.NET程序集方法导出为非托管是不可能的,但事实并非如此,因为这种可能性在CIL中是标准化的。在没有C ++ / CLI的情况下,通过将编译成CIL的程序集反汇编成IL,修改代码并将其编译回来,已经知道了这样做的技术。在我过去的答案中,请参阅参考文献(顺便提一下CodeProject的所有文章):

在MFC中加载C#DLL [ ^ ],

如何在Visual Basic 6.0中使用在Visual Basic 2008中创建的dll [ ^ ],

从无人码VC ++中调用用C#编写的托管DLL [ ^ ],

API在.Net:托管代码或非托管代码 [ ^ ]。



正如您所看到的,这种方法是合法的,但不是非常简单,因此C ++ / CLI方法具有明显的好处。另请参阅:

http://en.wikipedia.org/wiki/ C%2B%2B / CLI [ ^ ],

http://www.ecma- international.org/publications/standards/Ecma-372.htm [ ^ ],

http: //msdn.microsoft.com/en-us/library/xey702bw.aspx [ ^ ]。



-SA
You can do it by using C++/CLI and mixed-mode (managed+unmanaged) project. You can really use "regular" C++ classes with C++/CLI "ref" classes. You can really wrap a "regular" C++ class or struct around some "ref" types used in the implementation of the wrapper class.

Everything else depends on your problem.

Note that exporting of some .NET assembly methods as unmanaged is considered impossible, but this is not true, as such possibility is standardized in the CIL. There are know techniques for doing this without C++/CLI, by disassembling of the assembly compiled into CIL into IL, modification of the code and compiling it back. Please see the references (all to the CodeProject articles, by the way) in my past answers:
loading C# DLL in MFC[^],
How can I use a dll created in Visual Basic 2008 in Visual Basic 6.0[^],
Call Managed DLL written in C# from Unmanged Code VC++[^],
API''s in .Net: Managed or UnManaged Code[^].

As you could see, this approach is legitimate, but not very straightforward, so C++/CLI approach has apparent benefits. Please see also:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^].

—SA


这篇关于为C#托管代码创建C ++包装器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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