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.
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.