News for hackers. Stuff that matters.
Re:Strings and the CLR – A Special Relationship by ygra on Wednesday June 01, 2016 @01:27AM (#11812226)
Another point for this, besides optimization, is easier interop with existing technologies. The memory layout is identical to Windows' BSTR, as far as I know, and also includes a terminating U+0000. All of that makes it possible to marshal a CLR string as is in many circumstances without the need to copy or convert the characters around.
Re:Strings and the CLR – A Special Relationship by MarkSweep on Wednesday June 01, 2016 @02:22AM (#11812404)
There is an amusing comment in the CLR about it's support for odd length BSTRs[0]. There can be an extra wchar after the null terminator to store the extra byte.

It looks like if you use the automatic BSTR marshaler it creates a copy[1], though the CLR is so big it's hard for me to say that with confidence. Taking the address of a string with the fixed statement in C# of course won't allocate and that pointer can be passed to APIs that expected Unicode strings.

[0]: https://github.com/dotnet/coreclr/blob/a123c3972c447c8bcfa18... [1]: https://github.com/dotnet/coreclr/blob/a123c3972c447c8bcfa18...

Re:Strings and the CLR – A Special Relationship by asveikau on Wednesday June 01, 2016 @10:16AM (#11814499)
I would think, especially for out parameters and return values, you would need to copy to interop with BSTRs because they are supposed to live on the COM heap (SysAllocString) and CLR wouldn't use that by default.
Re:Strings and the CLR – A Special Relationship by matthewwarren on Wednesday June 01, 2016 @04:52AM (#11812950)
I love comments like that, it's almost a history lesson on VB and the CLR!
Re:Strings and the CLR – A Special Relationship by adzm on Wednesday June 01, 2016 @02:43PM (#11817042)
Then you'd probably love to know why the OLE DATE epoch is 1899-12-30...

also the B in BSTR is for Basic!

https://blogs.msdn.microsoft.com/ericlippert/2003/09/16/eric...

Re:Strings and the CLR – A Special Relationship by matthewwarren on Wednesday June 01, 2016 @04:51AM (#11812944)
Yeah that's a good point, I completely steered clear from talking about marshalling and/or interop (mostly due to lack of time), maybe I'll do a Part 2 and include that.