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

查看:171
本文介绍了从托管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 ++项目(使用Common Language Runtime支持构建)中使用此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. 我有一个Managed C ++项目(CLR),我们称之为 MyManagedProject.vxproj c> c> c> c> 有没有正确的方法来引用它,或者我应该把它复制到相同的目录 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放在与可执行文件相同的目录中,以便加载器可以找到它。

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

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