2
votes

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.

1
Will the "native" class be compiled as mixed mode, or is it purely unmanaged code? - Niall
I not planning for COM right now. - ravi
It will be pure unmanaged code ( except one object of managed class on which I would just call some functions ). - ravi
If not COM, and not mixed mode, then I think plain old C-style functions may be all that you are left with. If you can handle the mixed mode, then gcroot is easy enough to use. You may want to then investigate having mixed mode code with gcroot in 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
@Niall Thanks alot. Got your point. - ravi

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)