Managed and unmanaged Classes
-
Hi, I have a question regarding C++/CLI and classes. I can declare a managed class like this:
#pragma managed public ref class A { public: void DoSomething(); };
The compiler will translate this one into MSIL or CIL. And I can use this class in any .Net language by adding my compiled dll/exe as a reference e.g. in a C# project. And here's the unmanaged pendant:#pragma unmanaged class B { public: void DoSomething(); };
This is code gets translated into native code. But what happens, if I have the following:#pragma managed class C { public: void DoSomething(); };
What does the compiler exactly with this code? I'm confused :~ -
Hi, I have a question regarding C++/CLI and classes. I can declare a managed class like this:
#pragma managed public ref class A { public: void DoSomething(); };
The compiler will translate this one into MSIL or CIL. And I can use this class in any .Net language by adding my compiled dll/exe as a reference e.g. in a C# project. And here's the unmanaged pendant:#pragma unmanaged class B { public: void DoSomething(); };
This is code gets translated into native code. But what happens, if I have the following:#pragma managed class C { public: void DoSomething(); };
What does the compiler exactly with this code? I'm confused :~class C gets compiled to MSIL. Check it out in the disassembler :)
"Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")
-
class C gets compiled to MSIL. Check it out in the disassembler :)
"Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")
Hi Marc, thank you for the information. Well, I used Reflector.Net and didn't see these "hyrid" classes. But how is it possible that e.g. the managed class C could inherit from native class B? And if I call call a native function in class C, does it use internally PInvoke to call it? So, is it basically faster to call managed functions/methods from class C than calling native functions?
-
Hi Marc, thank you for the information. Well, I used Reflector.Net and didn't see these "hyrid" classes. But how is it possible that e.g. the managed class C could inherit from native class B? And if I call call a native function in class C, does it use internally PInvoke to call it? So, is it basically faster to call managed functions/methods from class C than calling native functions?
class C is not a managed class. It is compiled to MSIL because of the #pragma managed (or by default if no #pragma used and the /CLR compiler switch is used) but that doesn't make it a managed class. I'm not sure about the specifics of how native calls get done from MSIL but I don't think it involves pinvoke since it's not a managed class.
Ludi83 wrote:
So, is it basically faster to call managed functions/methods from class C than calling native functions?
Whether you compile your unmanaged classes to MSIL or native, all the same rules apply when mixing managed and unmanaged classes. Intuitively, any MSIL code should be slightly slower than native C++ code but since it's JIT compiled (not interpreted) there's no noticable difference in speed that I've seen. I've never benchmarked it but I bet the difference is minimal. If performance is an issue, marshalling between managed and unmanaged code is a way bigger issue than how the unmanaged code is compiled. Mark
"Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")