I have the source code for a C library that I need to use with my C# ASP.Net application. It sounds like the way to handle this would be to create a C++ dll wrapper and DLLImport that into my C# project. I have found this to be kind of ugly as far as when it throws an exception the entire application crashes. Is this the best way to accomplish this, or is there a safer way? I am trying to do some research on how I might be able to make a COM object with the C library, but haven't really found much there. Is it possible to make a COM object and reference it as .NET managed code?
Asked
Active
Viewed 124 times
2
-
A C++/CLI class library ? – aybe Oct 16 '14 at 00:50
1 Answers
3
I googled my way around this. I found this great article comparing
- a C# facade using with PInvoke and a lot of marshalling
- a Facade into the interop layer in C++/CLI and compiling in mixed mode for consumption by C#.
Also, this MSDN citation : C++ Interop is recommended over explicit PInvoke because it provides better type safety, is typically less tedious to implement, is more forgiving if the unmanaged API is modified, and makes performance enhancements possible that are not possible with explicit PInvoke.
Also your idea(C++ COM facade) seems legit as preached by the good book with 2 advantages:
- The resulting classes can be used from languages other than Visual C++.
- The details of the COM interface can be hidden from the managed client code. .NET data types can be used in place of native types, and the details of data marshaling can be performed transparently inside the custom runtime callable wrappers.

andrei.ciprian
- 2,895
- 1
- 19
- 29