How to Modify the Buffer of BSTR in VARIANT directly?

362 Views Asked by At

I am using Visual C++ 2008. In my code, I get a variable of BSTR. Now I want to change the first half of the characters in it. How to do so?

For example, if the string contains 5000 characters, then I want to modify the first 2500 characters in its buffer directly. Is there a easy way to do so?

I understand that I can convert BSTR to CString, or std::string, then do the replacement. But that will consume extra memory and time, especially when the string is very long. So I hope to modify the buffer of BSTR directly. That will be fast and efficient.

1

There are 1 best solutions below

0
On

you dont need to convert anything and waste memory, use Attach/Detach

BSTR str;
CComBSTR wrapper;
wrapper.Attach(str);
// work with wrapper
wrapper.Detach();