问题描述
我对托管/非托管互操作性和 COM 概念非常陌生.
I am pretty new to managed/unmanaged interoperability and COM concepts.
我收到了使用 COM Interop 的建议,以便在 C# 中使用我现有的 MFC 代码.但对我来说问题是,我有一个不是有效 COM 组件的 MFC Dll.如何使这个 MFC DLL 具有可在 .NET 中使用的 COM 可访问接口?
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?
推荐答案
从线程 在 C# 中加载 MFC DLLWindows 应用程序
要从 C# 访问本机代码,您有几个选择.
To access native code from C# you have a few choices.
最直接的是,您可以使用 DllImportAttribute 以 C# 术语描述 DLL 的入口点,以便可以通过 P/Invoke 调用它们.它们看起来像 C# 程序的静态方法.
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.
不太直接,您可以创建一个托管 C++ 程序集,将您的 DLL 包装在一个或多个托管对象中.托管 C++ DLL 可以通过添加引用从 C# 访问(因为它是具有 .dll 扩展名的托管程序集),并且还应该能够通过使用 #include 来访问您的 MFC dll 以包含 MFC dll 的头文件.
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.
第三种选择是将您的 dll 转换为 COM 对象,以便您的 C# 程序可以通过这种方式访问它.
A third option would be to turn your dll into a COM object so that your C# program can access it that way.
这篇关于带有 COM 接口的 MFC Dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!