1

I'm attempting to access C++ code from a managed .NET app. To do so, I'll need to write .NET wrappers for the unmanaged C++ classes I need, in C++/CLI.

If C++/CLI compiles to plain old CIL, then why can't you write the same wrapper class in C# using an unsafe class, since C# supports pointers as well?

Is it because the C++/CLI compiler is different from the C# compiler and can reference native C++ code while C# can't?

If I can, I'd like to, because I'm more familiar with the C# syntax than with C++.

1
  • C++ code is difficult because of the name mangling. You can definitely import the functions though! Try some interop tutorials (insert link when found here) to find out more about accessing unmanaged code from a managed environment. Commented Mar 21, 2013 at 12:32

1 Answer 1

2

You can (try) and with sufficient effort, you can probably make things work; even w/o resorting to unsafe code. But I think you'll find the C++/CLI syntax for interop situations sufficiently similar to C#.

Perhaps the biggest problem with this approach however is the long-term maintenance of your interop layer. Every time something changes on the C++ side, you could have to manually make changes to C#. And you won't get much help from the C# compiler alerting you to such things. It's only at runtime when the mangled C++ name can't be located in the DLL that you'll know.

It's can also be error-prone to re-implement C++ .H files in C#, especially if you need the same code to work for both 32-bit and 64-bit.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.