C# adding reference error – Checking a C++ DLL is valid for a C# project
DLLs compatible with C# projects can either be managed or unmanaged. However, if they’re unmanaged, they cannot be included in the project using the standard References->Add Reference->Browse->OK method, as only managed DLLs can be added in .net applications in this method. To add unmanaged DLLs you need to use a form of DLLImport, which I won’t be going into detail here – but Google can be your friend if this is what you want to do.
If you try to add a reference to an unmanaged DLL in the above method, you might find you get a message along these lines:
A reference to ‘your dll’ could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.”
It can be very frustrating to get this error when you’re working with your own C++ DLL project, but it can be easily fixed.
Fixing your C++ project
All you need to do is set your C++ project to use common language runtime (clr) support. You can find this by going to your project properties->Configuration Properties->General->Common Language Runtime Support and selecting Common Language Runtime Support (/clr). Recompile your project and the subsequent DLL will be able to be successfully added to your C# project. The below screenshots show you what you should be looking for in Visual Studio to solve this problem.
Remember to recompile your project after making these changes and check you’re then adding the right DLL to your C# project and you should be good to go.