是否可以直接从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.
更多推荐
使用托管代码中的非托管代码
发布评论