Why does printf work with managed Strings?

前端 未结 1 823
执念已碎
执念已碎 2021-01-12 16:18

we are currently digging through some really old C++/CLI-Code (Old Syntax .NET Beta) and were a bit surprised to see something like this:

System::String ^sou         


        
相关标签:
1条回答
  • 2021-01-12 16:24

    I believe it is because the compiler is turning it into this IL:

    call vararg int32 modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl) printf(int8 modopt([mscorlib]System.Runtime.CompilerServices.IsSignUnspecifiedByte) modopt([mscorlib]System.Runtime.CompilerServices.IsConst)*, ..., string)
    

    Which ends up as a pinvoke call to printf, so the runtime is being a little sneaky by marshalling it for you. You are still in a managed runtime, and the runtime will provide marhsalling as a service when it's needed.


    Some notes:

    It seems that clr!GenericPInvokeCalliHelper is doing this lifting on the x86 .NET 4 Workstation CLR.

    the following doesn't work

    That's because that is straight C++. It has no chance to go through marshalling because it isn't needed.

    0 讨论(0)
提交回复
热议问题