Reference an unmanaged C++ DLL from Managed C++

霸气de小男生 提交于 2019-12-04 17:18:03

You cannot add the unmanaged DLL as a reference to your managed C++ project. You can only do that with managed DLLs. What you do instead is link to the unmanaged DLL in the same way as you link to an unmanaged DLL in an unmanaged C++ project:

  • Use the header file for compilation.
  • Supply the unmanaged DLL's .lib file to the linker, for example by adding it to the list of Additional Dependencies list in the linker configuration pages.
  • Put the DLL in the same directory as the executable, so that it can be located by the loader.

Apparently, the only solution is to pass the unmanaged dll path in method decorated with DllImport Attribute.

What you can do to keep things in a neat way is to create a lib folder to put the unmanaged dll's and mark their property ''Copy to output directory'' as ''Copy Always'' (Right-Click over the unmanaged dll->Properties->Copy to output directory: Copy Always).

In your method DllImport attribute, you must specify the parameters as "lib/unmanaged.dll". In C#, you would have something like this.

[DllImport("lib/testLib.dll")]
private static extern int DisplayHelloFromDLL();

This approach has no difference from previous answers but it will help you to keep code a little bit more neat.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!