使用托管代码中的非托管代码

编程入门 行业动态 更新时间:2024-10-27 18:27:01
本文介绍了使用托管代码中的非托管代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有我的项目是在MFC中开发的,这是一个非常好的代码。现在我需要在C#中创建一个类似的应用程序,通过重用大多数MFC类。

是否可以直接从MFC DLL导出class / struct / enum我可以使用dllimport在C#中导入它,并使用它。

解决方案

是的,这是很可能的。你只需要注意类型。许多翻译非常好,但有些是古怪的。

您要搜索的概念的名称是COM互操作。看到这里有一个入门教程当然,MFC DLL必须支持COM可以从.NET访问。您需要重建您支持的适当COM接口的MFC DLL。

这是一个 COM Automation的COMDN概述,其中包含示例项目的链接。

而且这里是一个简单但随便点的CodeProject示例如何在.NET程序集中使用COM DLL。

很好

编辑:另一个想法

如果您无法重建MFC DLL(您没有IDE的源代码或正确版本),您可以在MFC或原始C / C ++中创建一个COMwrapperDLL,以标准的COM方式导入MFC DLL,然后暴露objec ts和方法。

I have my project developed in MFC which is unmnaged code. Now i need to create a similar application in C#, by reusing most of the MFC classes.

Is it possible to directly export class/struct/enum from MFC dll, so that i can import it in my C# using dllimport and use it.?

解决方案

Yes, it is quite possible. You just need to be careful with the types. Many translate very nicely but some are quirky.

The name of the concept you're searching for is COM interop. See here for a getting started tutorial. Of course, the MFC DLL has to support COM to be accessible from .NET. You need to rebuild your MFC DLLs with the proper COM interfaces supported.

Here is an MSDN overview of COM Automation complete with links to sample projects.

And here is a simple but to-the-point CodeProject sample that demonstrates exactly how COM DLLs can be used from within .NET assemblies.

Great pinvoke reference here. For accessing native Win32 APIs as well.

Edit: Another Idea

In case you cannot rebuild your MFC DLLs (you don't have the source or the right version of the IDE) you can create a COM "wrapper" DLL in MFC or raw C/C++ which would import the MFC DLLs in the standard, pre-COM manner and then expose the objects and methods that you need.

更多推荐

使用托管代码中的非托管代码

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

发布评论

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

>www.elefans.com

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