Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Wrapping "Legacy" C/C++

Greetings All!

I had some questions regarding using C/C++ libraries from .NET and hope that somebody out there could offer some answers. Basically, we have a vendor-supplied API that is available as both a COM DLL and as a collection of C functions with a static LIB. Here are my questions (please forgive my .NET ignorance):

1) Is it possible to utilize calls from a C library in Managed C++ and then compile the code into a .NET-usable DLL?

2) If it is possible will this be more efficient than just calling the supplied COM DLL via .NET COM Interop?

Thanx in advance for the wisdom!

Dave Langer
Wednesday, September 25, 2002

It is possible to create a Managed C++ component that wraps the functionality in a native library or DLL.  The "Managed Extensions for C++ Migration Guide" on MSDN discusses the trade-offs of this approach.

I haven't completed my own "wrapping legacy C++" project yet, so I don't have empirical evidence about the efficiency of this approach vs COM Interop. I certainly expect the Managed C++ component to be at least as efficient, if not slightly more so, because there are fewer layers between the managed and native code.  Your biggest efficiency concern will be the overhead of switching between managed and native code.  You can address this by designing the interface of the managed component to be "chunky" instead of "chatty" (reducing the number of times the process crosses the native code boundary.)

I like the fact that the wrapping approach allows you to avoid having to register the COM components just to use them from .NET.  In your scenario, you could simply link the static library directly into the new Managed C++ component, drastically simplifying your deployment.

Friday, September 27, 2002

I do have experience with legacy components, and using managed C++ to wrap is always faster than relying on the builtin COM interop. You can also use managed C++ to make COM calls without having to use the .NET infrastructure. This can be more efficient, but is more complex since you have to marshal the data from the unmanaged to the managed yourself. If speed is critical or you only need to pass simple data through the boundary, managed C++ is definately the way to go.

Michael Davidson
Monday, September 30, 2002

*  Recent Topics

*  Fog Creek Home