简体   繁体   English

在Visual Studio C ++中使用DLL

[英]Using a DLL in Visual Studio C++

I have a DLL that I've been using with no problem in Visual C# (simply adding the reference and using the namespace). 我有一个DLL,我一直在使用Visual C#没有问题(只需添加引用和使用命名空间)。 Now I'm trying to learn C++, and I don't understand how you reference a namespace from a DLL. 现在我正在尝试学习C ++,我不明白你如何从DLL引用命名空间。 I can right-click on a project and select 'references' and from there click 'add new reference', but that just provides me with an empty 'projects' window. 我可以右键单击一个项目并选择“引用”,然后单击“添加新引用”,但这只是为我提供了一个空的“项目”窗口。 What am I missing? 我错过了什么?

C++ is a lot different from C#/VB.Net when it comes to processing DLL references. 在处理DLL引用时,C ++与C#/ VB.Net有很大的不同。 In C# all that is needed to do a reference is a DLL because it contains metadata describing the structures that lay inside. 在C#中,进行引用所需的只是一个DLL,因为它包含描述内部结构的元数据。 The compiler can read this information such that they can be used from another project. 编译器可以读取此信息,以便可以从其他项目中使用它们。

C++ does not have the concept of metadata in the DLL in the sense that C# does. 在C#的意义上,C ++没有DLL中元数据的概念。 Instead you must explicitly provide the metadata in the form of a header file. 相反,您必须以头文件的形式显式提供元数据。 These files are included in your C++ project and then the DLL is delay loaded at runtime. 这些文件包含在您的C ++项目中,然后DLL在运行时加载延迟。 You don't actually "add a reference" so to speak in C++ but include a header file instead. 你实际上并没有“添加引用”,所以要用C ++来代替,而是包含一个头文件。

Once the header file is included, you can then access the namespace by including it in your CPP files 包含头文件后,您可以通过将其包含在CPP文件中来访问命名空间

using namespace SomeNamespace;

First of all, if you are trying to use the same DLL you used in your C# application, if you are using pure native C++, it is not straightforward to make calls into that DLL. 首先,如果您尝试使用在C#应用程序中使用的相同DLL,如果您使用纯本机C ++,则调用该DLL并不简单。 The problem is the DLL you are referencing in C# relies on the .NET framework in order to execute (it is a "Managed" DLL, as all C#, VB.NET and C++/CLI assemblies are). 问题是您在C#中引用的DLL依赖于.NET框架才能执行(它是一个“托管”DLL,因为所有C#,VB.NET和C ++ / CLI程序集都是如此)。 There is an easy way to reference "managed" code from C++ and that is by making a managed C++ project (AKA C++/CLI) (choosing from "CLR" section in the C++ project wizard in Visual Studio). 有一种从C ++引用“托管”代码的简单方法,即通过创建托管C ++项目(AKA C ++ / CLI)(从Visual Studio中的C ++项目向导中选择“CLR”部分)。 Otherwise the only way to access the managed DLL is by exposing it to COM and using COM to access the object. 否则,访问托管DLL的唯一方法是将其公开给COM并使用COM访问该对象。

EDIT: The previous answer will be more helpful if you're using unmanaged c++; 编辑:如果您使用非托管c ++,以前的答案会更有帮助; I assumed because of the C# reference that you were targeting managed C++. 我假设你因为C#引用而瞄准托管C ++。

The 'Add Reference' dialog should have a series of tabs - 'Projects' lists projects in the current solution; “添加引用”对话框应该有一系列选项卡 - “项目”列出当前解决方案中的项目; .NET lists the libraries installed in the GAC and 'Browse' lets you find a DLL yourself. .NET列出了GAC中安装的库,“浏览”允许您自己查找DLL。

If you just want to add a reference to the DLL you should be able to do it with 'Browse'. 如果您只想添加对DLL的引用,您应该可以使用“浏览”来执行此操作。 If it's the output of a project you have the source to, add the project to the solution and it'll appear under the 'Projects' tab. 如果它是您拥有源的项目的输出,则将项目添加到解决方案中,它将显示在“项目”选项卡下。

If this doesn't help, which version of Visual Studio are you using, and where/what is the DLL you want to use? 如果这没有帮助,您使用的是哪个版本的Visual Studio,以及您要使用的DLL在哪里/什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Visual Studio 2013将C / C ++ DLL生成并交付到C#应用程序中 - Build and deliver a C/C++ DLL into a C# app using Visual Studio 2013 在Visual Studio 2012 C#中使用由Eclipse制作的C ++ DLL? - Using c++ dll made from eclipse in visual studio 2012 c#? Visual Studio 2017 上 c# 中的 C++.dll - C++ .dll in c# on Visual Studio 2017 除非安装了visual studio,否则无法使用C ++ / CLI DLL - Cannot use a C++/CLI DLL unless visual studio is installed 在 Visual Studio 中添加对 C++ 项目的新 DLL 引用 - Add new DLL reference to C++ project inside Visual Studio 无法将Visual Studio制作的C ++ DLL导入Windows上的Visual Studio C#项目 - Unable to Import a Visual Studio made C++ DLL into a Visual Studio C# Project on Windows 如何使用在Visual C ++中使用C#创建的dll? - How to use dll created using C# in Visual C++? 通过C ++ / CLI包装器将本地C ++ dll与C#连接起来,Visual Studio问题 - Connecting native C++ dll with C# by C++/CLI wrapper, Visual Studio issue 在Visual Studio 2010中将本机/ C ++ DLL链接到托管C ++ / CLI包装器 - Linking Native/C++ DLL to Managed C++/CLI Wrapper in Visual Studio 2010 如何从 Visual Studio.NET 或 Visual Studio 2005 中的本机 Visual C++ 代码调用托管 DLL - How to call a managed DLL from native Visual C++ code in Visual Studio.NET or in Visual Studio 2005
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM