string - BSTR 和字符串

标签 string visual-c++ com bstr

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/

相关文章:

c++ - 如何在托管 C++ 中正确实现返回 "this"的类的方法?

c++ - std::visit 和 MSVC 调试器的堆栈损坏 "overloaded"结构

c++ - DllRegisterServer 错误 0xc0000005,(C++ COM Dll)。如何在 Visual Studio 2008 中调试我的 DllRegisterServer 函数?

javascript子字符串帮助

python - 如何使用python将数组中的字符串切片到另一个数组中

c++ - 使用标准集时的一些问题

c++ - VirtualBox IGuestSession::ProcessCreate 返回 0x8000FFFF (E_UNEXPECTED)

c++ - 将 MFC 对话框从 STA 更改为 MTA?

java - 是否可以显示带有新行内联的字符串

c# - String.Replace 方法忽略带有特殊字符的大小写。