你能把一个LPTSTR转换为BSTR吗? [英] Can you cast a LPTSTR to a BSTR?
问题描述
将LPTSTR直接投放到BSTR是合法的吗?
根据我的了解BSTR ,直接将一个LPTSTR转换为BSTR会留下一个损坏的长度前缀。示例代码显式声明,字符串文字不能存储到BSTR。任何人都可以确认一个LPTSTR / LPCTSTR不能直接转换到BSTR而不破坏长度前缀?
编辑:
我的困惑是看到这在调用COM对象时使用。事实证明,当编译COM dll时,生成一个.tli文件,创建一个中间方法。此方法接受类型 _bstr_t
。 _bstr_t
可以在其构造函数中使用 LPTSTR
,所以一切顺利。
LPWSTR
,则可以使用 SysAllocString 将指针转换为宽字符串转换为 BSTR
。 无法直接投放,因为两者有不同的内存表示。
如果使用C ++,您可以使用 _bstr_t 类,以简化 BSTR
字符串的使用。
Is it legal to cast a LPTSTR directly to a BSTR?
Based on my understanding of BSTR, casting a LPTSTR to a BSTR directly will leave you with a corrupted length prefix. The example code explicitly states that a string literal cannot be stored to a BSTR. Can anyone confirm for me that a LPTSTR/LPCTSTR cannot be cast directly to a BSTR without corrupting the length prefix?
EDIT:
My confusion is from seeing this used in a call to a COM object. It turns out that when compiling the COM dll, a .tli file is generated that creates an intermediate method. This method takes type _bstr_t
. The _bstr_t
can take LPTSTR
in its constructor, so everything works smoothly.
If your program is unicode and your LPTSTR
therefore is a LPWSTR
, you can use SysAllocString to convert from a pointer to a wide character string to BSTR
.
A direct cast is not possible because the two have different memory representations.
If you use C++, you can use the _bstr_t class to simplify the usage of BSTR
strings.
这篇关于你能把一个LPTSTR转换为BSTR吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!