Doubt on CString and BSTR LPWSTR
-
Hi All, I have on basic doubt. In windows environment both BSTR and LPWSTR are typedef ed to unsigned short *. But the contents in that pointer are different. BSTR is a wide char string with length in the first byte. LPWSTR is a null terminated string. But,
CString str; LPWSTR lpw = L"lpwstr"; BSTR bst = AllocSysString("bstr"); str = lpw; //how at runtime identified whether first char is length or actual data? str = bsw; //Both of the statements work fine
The code works fine, and behaves as we expect. My doubt is how at run time the decision on the first byte is decided? i.e whether the first byte is the data or the length?? how the compiler identifies. Thanks and regards, Raja Pratap -
Hi All, I have on basic doubt. In windows environment both BSTR and LPWSTR are typedef ed to unsigned short *. But the contents in that pointer are different. BSTR is a wide char string with length in the first byte. LPWSTR is a null terminated string. But,
CString str; LPWSTR lpw = L"lpwstr"; BSTR bst = AllocSysString("bstr"); str = lpw; //how at runtime identified whether first char is length or actual data? str = bsw; //Both of the statements work fine
The code works fine, and behaves as we expect. My doubt is how at run time the decision on the first byte is decided? i.e whether the first byte is the data or the length?? how the compiler identifies. Thanks and regards, Raja PratapRaj Prathap wrote:
whether the first byte is the data or the length??
Starting address of a BSTR variable always points to real data that is the character string and length is stored at an offset minus starting address of a BSTR variable. For eg:
BSTR bstrLengthTest = ::SysAllocString( L"Nibu" ); DWORD dwLength = \*(DWORD\*)((DWORD)bstrLengthTest-sizeof(DWORD)); cout << endl<< "String length in bytes is: " << dwLength << endl;
Hence BSTR always works both ways but an LCWSTR won't always work both ways.
Nibu thomas A Developer Code must be written to be read, not by the compiler, but by another human being. http:\\nibuthomas.wordpress.com
-
Hi All, I have on basic doubt. In windows environment both BSTR and LPWSTR are typedef ed to unsigned short *. But the contents in that pointer are different. BSTR is a wide char string with length in the first byte. LPWSTR is a null terminated string. But,
CString str; LPWSTR lpw = L"lpwstr"; BSTR bst = AllocSysString("bstr"); str = lpw; //how at runtime identified whether first char is length or actual data? str = bsw; //Both of the statements work fine
The code works fine, and behaves as we expect. My doubt is how at run time the decision on the first byte is decided? i.e whether the first byte is the data or the length?? how the compiler identifies. Thanks and regards, Raja PratapRaj Prathap wrote:
But the contents in that pointer are different. BSTR is a wide char string with length in the first byte.
Actually the length field is 4 bytes long.
Raj Prathap wrote:
BSTR bst = AllocSysString("bstr");
The above should be: BSTR bst = SysAllocString(L"bstr");
Raj Prathap wrote:
The code works fine, and behaves as we expect. My doubt is how at run time the decision on the first byte is decided? i.e whether the first byte is the data or the length?? how the compiler identifies
The trick is in
SysAllocString
that returs a pointer to the data string, not to the length prefix, see here http://msdn2.microsoft.com/en-us/library/ms221069.aspx[^] for a better explanation. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Hi All, I have on basic doubt. In windows environment both BSTR and LPWSTR are typedef ed to unsigned short *. But the contents in that pointer are different. BSTR is a wide char string with length in the first byte. LPWSTR is a null terminated string. But,
CString str; LPWSTR lpw = L"lpwstr"; BSTR bst = AllocSysString("bstr"); str = lpw; //how at runtime identified whether first char is length or actual data? str = bsw; //Both of the statements work fine
The code works fine, and behaves as we expect. My doubt is how at run time the decision on the first byte is decided? i.e whether the first byte is the data or the length?? how the compiler identifies. Thanks and regards, Raja PratapRaj Prathap wrote:
how the compiler identifies.
It doesn't the code tells it (declares) what the types are. as in the following "declarations":
LPWSTR lpw BSTR bst
Also....Raj Prathap wrote:
My doubt is how at run time the decision on the first byte is decided?
Raj Prathap wrote:
how the compiler identifies.
The compiler does NOT do ANYTHING at "run time".
-
Hi All, I have on basic doubt. In windows environment both BSTR and LPWSTR are typedef ed to unsigned short *. But the contents in that pointer are different. BSTR is a wide char string with length in the first byte. LPWSTR is a null terminated string. But,
CString str; LPWSTR lpw = L"lpwstr"; BSTR bst = AllocSysString("bstr"); str = lpw; //how at runtime identified whether first char is length or actual data? str = bsw; //Both of the statements work fine
The code works fine, and behaves as we expect. My doubt is how at run time the decision on the first byte is decided? i.e whether the first byte is the data or the length?? how the compiler identifies. Thanks and regards, Raja PratapYou have to keep track yourself of which strings are
BSTR
s and which are just C-style strings. It would be nice if the compiler could do that, but the fact thatBSTR
andLPWSTR
aretypedef
'd to the same thing makes that impossible.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
Raj Prathap wrote:
But the contents in that pointer are different. BSTR is a wide char string with length in the first byte.
Actually the length field is 4 bytes long.
Raj Prathap wrote:
BSTR bst = AllocSysString("bstr");
The above should be: BSTR bst = SysAllocString(L"bstr");
Raj Prathap wrote:
The code works fine, and behaves as we expect. My doubt is how at run time the decision on the first byte is decided? i.e whether the first byte is the data or the length?? how the compiler identifies
The trick is in
SysAllocString
that returs a pointer to the data string, not to the length prefix, see here http://msdn2.microsoft.com/en-us/library/ms221069.aspx[^] for a better explanation. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
Thanks. The link provided is really helpful. I was not clear about the parts of the BSTR i.e length+data+terminator. I used think terminator would not be there since the length is already there. Any ways with the link to MSDN article, all doubts are cleared. Thanks a ton. /pratap
-
Thanks. The link provided is really helpful. I was not clear about the parts of the BSTR i.e length+data+terminator. I used think terminator would not be there since the length is already there. Any ways with the link to MSDN article, all doubts are cleared. Thanks a ton. /pratap