2

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.

IdeaHat
  • 7,641
  • 1
  • 22
  • 53
eecs
  • 141
  • 2
  • 13
  • 1
    Just call the exported functions directly. This is entirely the point of using C++/CLI. – Hans Passant May 16 '14 at 18:22
  • @HansPassant Its *almost* that easy...but it is not that easy. Gotta link like you normally would. Gotta wrap your includes in exten C. – IdeaHat May 16 '14 at 18:24

2 Answers2

7

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

IdeaHat
  • 7,641
  • 1
  • 22
  • 53
  • Sorry I quickly corrected my question from C++ to C++/CLI but I guess by the time I edited, you already spotted me wrong. :-) Thanks for your answer though, I will give a shot with your Implicit Linking solution, I was suspecting that but wanted expert advises on it. Thanks! – eecs May 16 '14 at 18:15
  • No declspec decorations needed. That's the whole point of an import library, actually. – Ben Voigt May 16 '14 at 18:25
  • @BenVoigt As far as I know, from a *dll*, __declspec(import) is required for implicit linking. http://msdn.microsoft.com/en-us/library/zw3za17w.aspx. Explicit linking wouldn't require it, but would require you to call LoadLibrary and do a bunch more stuff. Though you are right for a static library. – IdeaHat May 16 '14 at 19:00
  • @madsciencedreams, I'm not sure how you turned "saves a thunk but slows DLL load" into "required". – Ben Voigt May 16 '14 at 19:40
2

Calling a C dll function from a C++ dll is exactly the same, whether it is for WPF, C++/CLI or native C++.

  1. Include the header file. See below for syntax.
  2. Add the .lib as an import dependency to your project. Also set the linker to consume the DLL. See Linking implicitly for all the details.
  3. Make sure that the consumed DLL is present in the run-time class path when the program executes.
  4. Use the function defined in the header file.

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.

Community
  • 1
  • 1
metacubed
  • 7,031
  • 6
  • 36
  • 65
  • Thanks metacubed, @MadScienceDreams + others for the feedback. After getting a better understanding on what mechanism would work best between "Implicit Linking" & "Explicit Linking", I see that I will have to go for Explicit Linking, knowing that I want to link the DLL at run-time and not during compile time. I am planning to load a DLL using my WPF app and then use the exported functions from the DLL I load. But I am having a hard time in understanding how could I do that in a WPF app written in C++/CLI environment. Any further help in explaining Explicit Linking in WPF will be helpful. – eecs May 16 '14 at 22:03