从Managed C ++引用非托管C ++ DLL [英] Reference an unmanaged C++ DLL from Managed C++

查看:93
本文介绍了从Managed C ++引用非托管C ++ DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是从我以前的问题:从非托管C ++创建DLL 您不需要阅读它来了解这个新问题。

This question follows from my previous question: Create DLL from unmanaged C++, but you would not have to read it to understand this new question.

我现在有一个DLL,其中包含由几个函数组成的非托管C ++代码,其中只导出一个供外部使用。接下来,我需要在托管C ++项目(使用公共语言运行时支持构建)中使用此DLL。到目前为止,我通过在Visual Studio 2010中设置 Additional Include Directories 添加了对现有非托管项目头文件的引用。

I now have a DLL that contains unmanaged C++ code consisting of a few functions, of which I only export one for outside use. Next, I need to use this DLL in a Managed C++ project (built with Common Language Runtime support). So far, I have added a reference to the existing unmanaged project's header file by setting the Additional Include Directories in Visual Studio 2010.

如果我现在尝试添加对DLL文件的引用: MyManagedProject>属性>共同属性>添加新参考>浏览> MyUnmanagedDLL.dll ,我收到错误无法添加对文件MyUnmanagedDLL.dll的引用,因为它不是.NET程序集或注册的ActiveX控件。但是,如果我使用前面提到的DLL,并将其复制到与我的Managed C ++的可执行文件相同的文件夹,一切都正常。

If I now try to add a reference to the DLL file: MyManagedProject > Properties > Common Properties > Add New Reference > Browse > MyUnmanagedDLL.dll, I get an error Could not add a reference to file MyUnmanagedDLL.dll because it is neither a .NET assembly nor a registered ActiveX control. However, if I take the previously mentioned DLL and I copy it to the same folder as my Managed C++'s executable, everything works fine.

我不知道这是否是正确的方法,因为它似乎是一个黑客的解决方案。此外,这意味着我必须在每次更改其源代码时复制DLL。

I am not sure if this is the correct way to do it, as it seems to be a hackish solution. Also, it means that I have to copy the DLL across every time I make a change to its source code.

总结我的问题:


  1. 我创建了一个包含非托管C ++代码的DLL,让我们称之为 MyUnmanagedDLL.dll 。我也为这个DLL创建了头文件。

  2. 我有一个托管C ++项目(CLR),让我们称之为 MyManagedProject.vxproj

  3. 如何在 MyManagedProject.vxproj 中使用 MyUnmanagedDLL.dll ?有没有正确的方法来引用它,或者我应该将其复制到与$ code> MyManagedProject.exe 相同的目录? (这个工作,但我不知道它的良好做法...)

  1. I have created a DLL consisting of unmanaged C++ code, let's call it MyUnmanagedDLL.dll. I have also created a header file for this DLL.
  2. I have a Managed C++ project (CLR), let's call it MyManagedProject.vxproj
  3. How can I use MyUnmanagedDLL.dll in MyManagedProject.vxproj? Is there a correct way to reference it, or should I just copy it to the same directory as MyManagedProject.exe? (This works but I'm not sure if its good practise...)

额外的信息:Windows 7,Visual Studio 2010 Ultimate ,CMake 2.8.10.2

Extra information: Windows 7, Visual Studio 2010 Ultimate, CMake 2.8.10.2

推荐答案

您不能添加非托管DLL作为托管C ++项目的引用。您只能使用受管理的DLL进行操作。您所做的是链接到非托管DLL的方式与在非托管C ++项目中链接到非托管DLL的方式相同:

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:


  • 使用用于编译的头文件。

  • 将非托管DLL的.lib文件提供给链接器,例如将其添加到链接器中的附加依赖关系列表中配置页面。

  • 将DLL放在与可执行文件相同的目录中,以便可以由加载程序找到。

  • 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.

这篇关于从Managed C ++引用非托管C ++ DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆