What is the difference between a COM string (BSTR) and a .NET string?

陌路散爱 提交于 2019-12-05 11:40:15

The two string types are not related at all. A transformation has to occur to convert one type to another.

A BSTR has a number of conventions that must be followed in including allocated via SysAllocString*, deallocated with SysFreeString, having a length prefix, and a terminator of two null characters.

http://msdn.microsoft.com/en-us/library/ms221069.aspx

A .Net string is a managed type that is allocated via the managed heap. It's lifetime is managed by the CLR garbage collector.

To construct your own BSTR, it would be much better to use Marshal.StringToBSTR:

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.stringtobstr.aspx

If that's not good enough you can pinvoke SysAllocString:

http://msdn.microsoft.com/en-us/library/ms221458.aspx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!