且构网

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

无法将MFC dll添加到C#代码中的引用

更新时间:2023-11-10 16:52:04

萨蒂亚,

要将本机代码(非托管)dll与托管dll(C#dll/程序集等)一起使用,我们应该进行pInvoke或使用COM可调用包装器(CCW).

现在,当您创建MFC dll时,您应该已经有了一些GUI.然后,您应该创建MFC Activex控件,而不是MFC dll.然后只有您可以注册它并在C#中使用它

您的DLL无法注册,因为它没有导出regsvr32.dll进行注册所必需的DllRegisterServer方法.
Hi Satya,

To use a native code (un-managed) dll with managed dlls (C# dlls/assemblies etc..), we should go for pInvoke or use COM callable wrapper (CCW).

Now as you have created a MFC dll, you should be having some GUI. Then you should be creating MFC Activex control, rather than MFC dll. Then only you can register this and use it in C#

Your DLL can not be registered as it is not exporting DllRegisterServer method, which is required by regsvr32.dll to perform registration.


添加到.NET项目中的引用不仅仅是DLL.您只能引用任何可执行文件(顺便说一下,可以是DLL,EXE-没什么区别),前提是该文件是某个有效的.NET程序集的主要可执行模块.

您可以在.NET程序集中使用本机代码,但这到目前为止还不是那么简单.这样做的方法称为P/Invoke.如果只阅读这篇文章,您可以学到很多:
http://en.wikipedia.org/wiki/P/Invoke [ http://msdn.microsoft.com/en-us/library/Aa712982 [ ^ ].

这是有关以下主题的出色CodeProject文章:基本P/调用 [
最后,我真的怀疑您是否可以在.NET应用程序中使用MCF.使他们生活在一起并不容易,所以我认为这没有道理.自己看看吧.

—SA
The references you add to a .NET projects are not just DLLs. You can reference any executable file (by the way, it can be DLL, EXE — no difference) only if this is a main executable module of some valid .NET assembly.

You can use native code in your .NET assembly, but this by far not so simple. The method of doing it is called P/Invoke. You can learn a good deal of it if you read just this article:
http://en.wikipedia.org/wiki/P/Invoke[^].

For further information, please see: http://msdn.microsoft.com/en-us/library/Aa712982[^].

This is a good CodeProject article on the topic: Essential P/Invoke[^].

An alternative to P/Invoke is using C++/CLI language in combination with C++ in one project; which could be a "mixed-mode" project (managed+unmanaged). You can bind unmanaged API in C++, wrap it in C++/CLI "ref" classes and make them public. The output executable module of such project can be referenced as a regular .NET assembly by your .NET assemblies.

Finally, I really doubt you can use MCF in a .NET application. It is not easy to make them live together, so I don''t believe it makes sense. See by yourself though.

—SA