3
votes

I am working with C++/CLI for C Library. I explored in the net about it. I got several links about it.

Mixed mode C++/CLI performance considerations - best practices

I am developing a C++/CLI DLL which will wrap a C static library.

There was one suggestion that I really wanted to discuss here is "One should not mix up managed and unmanaged C++ code in wrapper". I don't understand meaning of it.

The managed DLL will, of course, contain managed C++ code and unmanaged C++ code.

The purpose of the wrapper is to translate calls from the static library to managed code DLL.

Please clear my doubts - I wanted comments on this.

1
You should probably add the URL for where you found the 'One should mix up ...' quote. I'm still not clear what you are asking. - Jonathan Leffler
@JonathanLeffler updated the question - Chris_vr
Your URL (another SO question) says "you should not mix managed and unmanaged code", which is different from what you quote. A missing 'not' can completely wreck a question. - Jonathan Leffler
@JonathanLeffler: yeah its my mistake updated the question.please clarify that. - Chris_vr

1 Answers

2
votes

If you have a regular C++ library (non-CLI), you should avoid turning on the 'CLI' compilation option for that library, for performance reasons.

Instead it is good practice to create a library that just has your wrapper classes in it. This library will of course be C++/CLI, and will create an assembly that can be referenced by regular .Net libraries.

So that's probably what the advice would be talking about - create a wrapper library for your CLI wrappers

-- addendum for the updated question

A managed C++/CLI class should not contain unmanaged code because it /cannot/ contain many types of unmanaged code.

For example, a C++/CLI class cannot have any unmanaged member variables that are not references or pointers. This is because the .Net runtime garbage collector may decide to put the object somewhere else in memory at any time (this is the reason you need to pin memory etc.). If the GC decides to move your native C++ objects to some other place in memory, this will potentially invalidate any pointers you have to that object. This is obviously bad.

C++/CLI is a great language. If you use it, however, you should either decide to write pure .Net code, or you should use it as an interface between native C++ and .Net. Having mixed memory models in the same class just confuses things.