.Net中的API:托管或非托管代码

编程入门 行业动态 更新时间:2024-10-26 12:23:03
本文介绍了.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:托管或非托管代码

本文发布于:2023-06-10 16:26:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/615014.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:或非   代码   Net   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!