I have looked into lot of online searches and most have examples which talk about C# with C or C++ with C# but have never come across using C DLL calling from C++ (specifically from C++/CLI with WPF in my case).
Can someone please explain with an example how to call C DLL functions in C++. I have a C DLL which has all its functions defined as extern "C" funcName() and there is also a export functions .def file which has all the function names which need to be exported. Now having this C DLL how can I call its exported functions in a C++ code.
Thanks.
OK, so "WPF with C++" is not a language. WPF is a .NET library. C++/CLI is the language you are writing in.
Calling C dlls from C++/CLI is "pretty darn easy", depending on how the DLL was exported. You can either use the .NET libraries to do this, or the C libraries (I suggest the C libraries). There are two ways to link to a dll: implicit linking or explicit linking.
Implicit linking is much cleaner, imho. But you require access to a header file (Edit: possibly with declspec decorations, see BenVoigt's notes below) and a .lib file. If you have any of these, simply add the .lib file to the Additional Dependencies (right-click project->properties->Configuration Properties->Linker->Input) and add the header file path to the include (right-click project->properties->Configuration Properties->C/C++->General). Then you can include the header file (props to metacubed for mentioning this) as an extern "C" header file:
extern "C"
{
#inlude "c_header.h"
}
(this is because C++ does name-mangling so that you can have overloading & namespaces & classes and stuff. It'll interpret your C header file as a C++ header file (and mangle all your names) if you don't use the extern "C")
http://msdn.microsoft.com/en-us/library/d14wsce5.aspx
Or you can link explicitly
http://msdn.microsoft.com/en-us/library/784bt7z7.aspx http://msdn.microsoft.com/en-us/library/zzk20sxw.aspx
But I suspect that figuring out that you're using C++/CLI, that'll tell you what to google. (Though C++ answers will work for you too).
For the .NET way, an easy trick is to look up the C# call (please note that this is also explicit linking, and thus doesn't require the .lib file and headers).
http://msdn.microsoft.com/en-us/library/eyzhw3s8.aspx
Now the tricky bit is that if you're using C++/CLI and want to get your fancy managed classes into C form, you'll have to marshal the code. Here is a handy link to a tabel that shows you how.
http://msdn.microsoft.com/en-us/library/ac7ay120%28vs.71%29.aspx
Calling a C dll function from a C++ dll is exactly the same, whether it is for WPF, C++/CLI or native C++.
.lib as an import dependency to your project. Also set the linker to consume the DLL. See Linking implicitly for all the details.The header file include should be declared as:
extern "C" {
#include "c_header.h"
}
That's all!
EDIT: Also, check this out: Call a C function from C++ code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With