views:

258

answers:

2

For example, if I have 100 C++ methods (basically a native library) that interacts with a core window component.

I want to basically make a wrapper for these c++ methods in C#, so all my new hire employees can use that instead of C++, etc. the C++ code is legacy and scares me, so I Want to deal with it just once.

Is the approach here, for each method have a corresponding C# method ... in fact is there any other way of doing this?

Can I have some sort of wrapper subsystem. How do you people generally do this?

also, are there any performance considerations, etc.?

+1  A: 

Why not just use COM Interop to wrap the library? Then you can more or less treat the C++ code as .Net native code that can be called in the normal fashion.

1800 INFORMATION
+5  A: 

If your C++ methods are in a COM object, then you can use COM interop from C#. See CLR Inside Out: Introduction to COM Interop for a good introduction.

If those C++ methods are more like traditional API calls, then you'll want to use Platform Invoke (i.e. PInvoke). That entails creating managed prototypes in C# for the unmanaged (C++ functions). A good place to start is the Platform Invoke Tutorial.

As far as performance considerations go, there typically won't be much to worry about. Calling from C# might be fractionally slower than calling directly from C++, in large part due to marshaling data. Unless the code you're calling is in a critical loop, you're not going to notice any difference.

It really depends on what those native functions do. The more you have to share data between the unmanaged and managed worlds, the more difficult the process becomes. Without more information about your specific functions, it's difficult to say where you might encounter problems.

Jim Mischel
COM seems to be what I guess I'm somehow inclined towards. So basically, create a COM wrapper for my functions, and so all my C++ functions (legacy) will have another wrapper (my new C++ Com stuff) and then this gets called by others (my new hires!!)?
If it's not already COM, I'd hesitate to make a COM wrapper. I'd be more inclined to make an OO wrapper in C# that uses PInvoke to call the native functions. But again, that's just general advice based on experience and no knowledge of your individual case.
Jim Mischel
I'm fine with wrapping C# on them, I just thought (from what you said/or I inferred) that COM is/will be faster.