带有 COM 接口的 MFC Dll

5

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

问题描述

限时送ChatGPT账号..

我对托管/非托管互操作性和 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14