MFC Dll与COM接口 [英] MFC Dll with COM Interface

查看:181
本文介绍了MFC Dll与COM接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常喜欢管理/非管理互操作性和COM概念。



我收到了一个使用COM Interop的建议,用于在C#中使用我现有的MFC代码。但是我的问题是,我有一个不是一个有效的COM组件的MFC Dll。如何使这个MFC DLL具有可以在.NET中使用的COM可访问接口?

解决方案

从线程在C#Windows应用程序中加载MFC DLL


要从C#访问本地代码,您有几个选择。最直接的是,您可以使用DllImportAttribute以C#方式描述DLL的入口点,以便通过P / Invoke调用它们。他们将看起来像静态方法到您的C#程序。



直接减少,您可以创建一个托管的C ++程序集,将您的DLL包装在一个或多个托管对象中。可以通过添加引用从C#访问Managed C ++ DLL(因为它是具有.dll扩展名的托管程序集),并且还可以使用#include来访问MFC DLL,以包含MFC DLL的头文件。



第三个选项是将你的dll变成一个COM对象,以便你的C#程序可以这样访问。



I am pretty new to managed/unmanaged interoperability and COM concepts.

I received a suggestion of using COM Interop, for using my existing MFC code in C#. But the problem for me is, i have a MFC Dll which is not a valid COM component. How can I make this MFC DLLs to have COM-accessible interfaces ready for use in .NET?

解决方案

From thread Loading MFC DLL in C# Windows Application

To access native code from C# you have a few choices.

Most directly, you can use DllImportAttribute to describe your DLL's entry points in C# terms so that they can be called via P/Invoke. They'll look like static methods to your C# program.

Less directly, you can create a Managed C++ assembly that wraps your DLL in one or more managed objects. The Managed C++ DLL can be accessed from C# via Add Reference (because it is a managed assembly with a .dll extension) and should also be able to access your MFC dll by using #include to include the MFC dll's header file.

A third option would be to turn your dll into a COM object so that your C# program can access it that way.

这篇关于MFC Dll与COM接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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