.Net中的API:托管或非托管代码 [英] API's in .Net: Managed or UnManaged Code

查看:118
本文介绍了.Net中的API:托管或非托管代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用在c#或VB中创建的任何API,那么可以在托管代码还是非托管代码中使用此API?

If I am using any API which is created in c# or VB, can this API be used in either Managed Code or Unmanaged Code?

推荐答案

是. MSDN [
Yes. MSDN[^] to the rescue!


我假设您的意思是C#和VB.NET(即使您是说VB,也请忘记它.)

基本上,只能在托管代码中使用它,方法是在项目中引用或在运行时使用System.Reflection.Assembly加载它.

您可以开发混合模式的程序集,这些程序集既可以用作.NET程序集,也可以用作常规非托管DLL,但是C#和VB.NET不足以做到这一点(但是C ++/CLI和IL可以做到). >
但是,有一个非常巧妙的解决方案,可以以混合模式转换现有的.NET Assembly并将某些托管方法导出为非托管方法.想法是将程序集分解为IL,修改IL代码并重新组装.此过程可以自动化.

以下是提供此类解决方案的CodeProject文章:
非托管代码可以包装托管方法 [如何自动将.NET功能导出到非托管程序 [如何使用在Visual Basic 6.0中的Visual Basic 2008中创建的dll [从Unmanged Code VC ++用C#调用托管DLL [ ^ ].

—SA
I assume you mean C# and VB.NET (even if you mean VB, let''s forget it).

Basically, it can be used only in managed code, by referencing in the project or by loading it during run time using System.Reflection.Assembly.

You can develop mixed-mode assemblies which can be used as both .NET Assembly and a regular unmanaged DLL, but C# and VB.NET are not good enough to do that (but C++/CLI and IL can do it).

However, there is a very cunning solution to convert existing .NET Assembly in mixed mode and exporting some managed method to unmanaged. The idea is to disassemble the assembly to IL, modify IL code and re-assemble. This process can be automated.

Here are the CodeProject articles offering such solutions:
Unmanaged code can wrap managed methods[^],
How to Automate Exporting .NET Function to Unmanaged Programs[^].

See also my past solutions explaining the idea:
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++[^].

—SA


这篇关于.Net中的API:托管或非托管代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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