-2

I've some questions about C++/CLR C++ Interop (basically mixing C++ unmanaged and managed code).

  1. If I create an application with C++/CLR and I write unmanaged and managed code, for example:

    int main(int argc, char*argv[])
    {
        int a = 30;                                   
        int* a_ptr = &a;                              
        std::cout << a_ptr << std::endl;              
        Console::WriteLine("This is managed code");   
    }
    

As the 4th line is managed .NET code, it will go through the CLR. But will the first three lines go through the CLR too or will they be handled separately? Will it reduce the performance if i write only C++ unmanaged code in an CLR project?

  1. Does the C++/CLR project change anything in the C++ language, for example the primitives or stuff like that?

  2. How does it work? How is the CLR being called if it detects that a command uses it... or does everything go through it?

qxz
  • 3,814
  • 1
  • 14
  • 29
  • 1
    One question per question please. And a meaningful title. – Lightness Races in Orbit Oct 09 '16 at 22:28
  • @qxz: "sth" is short for "something", not "stuff". – Lightness Races in Orbit Oct 09 '16 at 22:29
  • It goes through the just-in-time compiler. Which converts the MSIL that the compiler generated from your main() function to machine code. Nothing dramatically different from what a normal compiler does, they all use a front-end that parses code and turns it into IL and a back-end that generates machine code. Compare to LLVM. The only difference is that this IL translation step happens at runtime instead of compile time. – Hans Passant Oct 09 '16 at 22:56

1 Answers1

0
  1. No. Performance will be same. In C++ CLI a function either can be managed or unmanaged. And regardless a function is managed or not they are equal to the processor after being JITted. So a call from native function to managed function is not so different than native-> native, managed-> managed. The only big difference happens when passing the parameters. Depending on the case some parameters are marshalled from one world to another. If that happens you then lose performance. (Same goes with return values as well). In your example there is no cross world parameter passing or returning so there is no performance ovearhead.

  2. No. Everything stays the same.

  3. From managed to unmanaged it uses internalcall mechanism just like CLR itself. From managed to unmanaged, its just calls the address. native function doesn't care if it calls a managed code or not (except parameter marshalling).

Onur Gumus
  • 1,389
  • 11
  • 27