且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MFC Dll与COM接口

更新时间:2023-02-07 18:21:28

从线程在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.