I want to call members of a C++/CLI class from native C++ code. Compiler is prohibiting me from doing that. I know that we can use native pointers/references in managed code but don't know way of going opposite. Can anyone help me with this by giving a simple example.Thanks in advance.
2
votes
1 Answers
2
votes
It can't be done. Memory layout of managed types is determined by the JIT, so unless your native code uses the CLR programming interface, it has no way of discovering and accessing them. Keeping references visible to the garbage collector is another problem, although overcoming that is more straightforward.
However it is possible to create native types with C++/CLI and those will have a memory layout fixed at compile time, so both managed and native code can use them. That is the way to cross the managed-native boundary in reverse. (Function pointers created from delegates are another way to cross in reverse)
gcrootis easy enough to use. You may want to then investigate having mixed mode code withgcrootin a separate dll to the managed and unmanaged code and allow the mixed mode dll to export a native C++ class; i.e. 3 dlls, native/unmanged, mixed, managed. - Niall