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.
No, you cannot - if the code that believes it is a BSTR calls SysStringLen() it will run into undefined behavior since that function relies on some implementation-specific service data.
If you are trying to pass
LPCTSTR
s asBSTR
s you will find that you will randomly blow up any interop marshalling code you get near. The marshalling will pull the 4 byte prefix (which is random data) for anLPCTSTR
and try and Marshall the string.You will find that the 4 byte prefix turns out to be the stack contents prior to your buffer, or even better character data from the string before it. You will then try to marshall megabytes of data... :-)
use the
CComBSTR
wrappers and use.Detach()
if you need to keep theBSTR
pointer.Be nice if the compiler spat out a warning on this one.
No, you cannot cast them directly. However, you can make a string that does both. A C-String doesn't have a 4-byte header. However, the bit in the middle is the same so if you find yourself needing both representations, make a wrapper class that constructs a string with a 4byte header and a null terminator but can return accessors to the BSTR portion and the C-String.
This code is intended as an incomplete example, I haven't compiled this!
It cannot be, because then the four bytes in memory preceding the
LPTSTR
would be considered as the length of the resultingBSTR
. This might cause a memory protection fault on the spot (not very likely), but it would certainly result in aBSTR
with a length that could be way larger than the length of the originalLPTSTR
. So when someone tries to read or write from that they may access invalid memory.Generally speaking no, but there are ways to make them compatible to some extent via helper classes and macros (see below).
The main reason why a 1:1 mapping will never be possible is that a
BSTR
(and consequentlyCComBSTR
can contain'\0'
in the string, because ultimately it is a counted string type.Your best choice when using C++ would be to go for the ATL class
CComBSTR
in place ofBSTR
proper. In either case you can make use of the ATL/MFC conversion macrosCW2A
and friends.Also note that the documentation (MSDN) says:
... which applies to your use case.
Please see John Dibling's answer for an alternative (
_bstr_t
).If your program is unicode and your
LPTSTR
therefore is aLPWSTR
, you can use SysAllocString to convert from a pointer to a wide character string toBSTR
.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.