2

The header file used by the C++ dll contains a few user defined type definitions and function declarations.I have browsed many sites and all the posters say that its not possible to import header file into C#.I would like to know if there is any way to import header file into C# code as it is required to declare the functions of the imported dll in the C# application class.

Thanks

3 Answers3

2

This is more or less a duplicate of Importing a C++ dll in C# however that question has some poor "information lite" answers.

Unfortunately there is no short answer to this, however you do have several options.

  1. Wrap your C++ class in a managed C++ class. Then your C# project just references the managed C++ project, and everything works.
  2. Like 1, create a managed C++ class, but create a facade. This a simplified interface to your class exposing only the functionality you need.
  3. The other approach is to use PInvoke to call the methods on the class. You'd need to built a C# representation of the class for the pinvoke calls. This can be problematic and involves a lot of trial and error if you don't know what you're doing.

All of the above involve learning some technologies that will be new to you (manged C++ or the intracies of PInvoke). Unfortunatly there's no other way.

If you can I'd go with 2.

Community
  • 1
  • 1
Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
1

Have you considered bridging this gap with a C++/CLI project? If you write all your wrapper library and interoperability code in a C++/CLI project you can easily define managed types that expose the defines in C/C++ header files.

John Leidegren
  • 59,920
  • 20
  • 131
  • 152
0

When I have submbled upon the same problem - given .h and .lib files intended for C/C++ use, I have written a wrapper for it in C++ with managed extensions. It is really quite simple. Your C++ assembly will play neatly in your Visual Studio solution and you'll be able to single step right from C# into managed C++ into unmanaged C++ and back again light as a breeze! :-)

Dan Byström
  • 9,067
  • 5
  • 38
  • 68