neetha
neetha

Reputation:

how to import a header file into a C# project which imports a C++ dll

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

Upvotes: 2

Views: 8363

Answers (3)

Binary Worrier
Binary Worrier

Reputation: 51739

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.

Upvotes: 2

Dan Byström
Dan Byström

Reputation: 9244

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! :-)

Upvotes: 0

John Leidegren
John Leidegren

Reputation: 61067

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.

Upvotes: 1

Related Questions