I have a DLL that I've been using with no problem in Visual C# (simply adding the reference and using the namespace). Now I'm trying to learn C++, and I don't understand how you reference a namespace from a 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?
问题:
回答1:
C++ is a lot different from C#/VB.Net when it comes to processing DLL references. In C# all that is needed to do a reference is a DLL because it contains metadata describing the structures that lay inside. 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. 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. You don't actually "add a reference" so to speak in C++ but include a header file instead.
Once the header file is included, you can then access the namespace by including it in your CPP files
using namespace SomeNamespace;
回答2:
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. 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). 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). Otherwise the only way to access the managed DLL is by exposing it to COM and using COM to access the object.
回答3:
EDIT: The previous answer will be more helpful if you're using unmanaged c++; I assumed because of the C# reference that you were targeting managed 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.
If you just want to add a reference to the DLL you should be able to do it with 'Browse'. 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?