使用托管代码中的非托管代码 [英] Using unmanaged code from managed code

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

问题描述

我有我的项目是在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.

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

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