linking/referencing a C++ dll in a programming project

I want to know how safe it is to reference a dll composed of c++ code into a visual studio project that is going to be c#.
well, the dlls i have are in a bin folder, but there is another folder called lib, and it has .lib and .exp files. I believe that the .dll is the entire reference to the lib and obj.
It is as safe as the dll in question. Without knowing anything else, we can only say: C# and .net allows the use of DLL's so why would your case be any different?

If, however, you have more information, then maybe we can say more.

As for LIB and DLL: The C# project only needs the dll. The LIB file is for linking with C++ only.
If the DLL is correctly written and exposes the right kind of interface, it's perfectly safe.

If you are compiling the DLL yourself, you could add a managed C++ (C++/CLI) wrapper class (or classes) so the DLL exposes .Net assemblies and can therefore be added as a reference to your C# project. Then you can just carry of as normal.

The other approach is to expose a C-API from the DLL (global, exported functions) and then call it using C#'s PInvoke mechanism. The PInvoke code could be wrapped so that the rest of your C# code is oblivious to it.

What you can't do is call exported, regular C++ classes from C# code. So if your C++ DLL is exporting classes, it's not usable as-is. (Also see P.P.S.).

Andy

P.S. It might help you a bit to read up about DLLs; your use of the term "reference" doesn't always make sense. A DLL contains the whole implementation of the DLL's functions. See (e.g.) "What is a DLL?": http://support.microsoft.com/kb/815065

P.P.S. You could also write a COM object (in C++) which uses the DLL and call that from .Net using the COM interop approach. But I only mention this for completeness, as it's a pointlessly complicated solution for your problem. So unless you are feeling masochistic...
Last edited on
Topic archived. No new replies allowed.