BSTR DoSOmething()
{
return L"";
}
或者是否可以将 TCHAR * 传递给以 BSTR 作为输入参数的 API。
可以通过return语句将wchar_t字符串转换为BSTR吗?会导致内存损坏吗?
最佳答案
不,这是不行的,因为某些 API 期望的不仅仅是一个 WCHAR*
指针(BSTR
也是如此),而是一个真正的 BSTR
带有附加长度信息的指针。仍然以这种方式进行转换通常可能效果很好,但这可能会产生误导。
有关 BSTR 的所有信息:Eric's Complete Guide To BSTR Semantics .
来自那里的相关引用:
2) A BSTR must be allocated and freed with the SysAlloc* family of functions. A PWSZ can be an automatic-storage buffer from the stack or allocated with malloc, new, LocalAlloc or any other memory allocator.
3) A BSTR is of fixed length. A PWSZ may be of any length, limited only by the amount of valid memory in its buffer.
4) A BSTR always points to the first valid character in the buffer. A PWSZ may be a pointer to the middle or end of a string buffer.
关于string - BSTR 和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12619348/