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.
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.
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.
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.
To summarise my problem:
- 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. - I have a Managed C++ project (CLR), let's call it
MyManagedProject.vxproj
- How can I use
MyUnmanagedDLL.dll
inMyManagedProject.vxproj
? Is there a correct way to reference it, or should I just copy it to the same directory asMyManagedProject.exe
? (This works but I'm not sure if its good practise...)
Extra information: Windows 7, Visual Studio 2010 Ultimate, CMake 2.8.10.2
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.
This approach has no difference from previous answers but it will help you to keep code a little bit more neat.
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: