BSTR in C++
-
Your functions are wrong. Note that a bstr is not required to be null terminated. If you want to get the size of a BSTR, use for example wcslen. To convert between BSTR and C-style strings, use W2A, OLE2A macros from <AtlBase.h>. Also see CComBSTR class.
-- ===== Arman
I replaced with wcslen and still same problem. I would prefer to understand how to convert it corectly, manually. That way I don't have to depend on more links and stuff that can break from one windows release to the other : ).
-= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^
-
Why do you have to roll out convertion functions for BSTR's on your own. We already have
ConvertStringToBSTR
andConvertBSTRToString
. Why not use them. Link tocomsupp.lib
for using them.
Nibu thomas A Developer Code must be written to be read, not by the compiler, but by another human being. http:\\nibuthomas.wordpress.com
LPCSTR d = _com_util::ConvertBSTRToString(FontInformation.Name); MessageBox(0, d, 0, 0);
This still returns garbage data : ( ... what do you make of: "???r" instead of "Arial". And it's sure that it's a BSTR there. For example, my function works but doesn't process the BSTR correctly. It would be easier for me to fix my function ... if only I knew what's wrong : D.-= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^
-
Your functions are wrong. Note that a bstr is not required to be null terminated. If you want to get the size of a BSTR, use for example wcslen. To convert between BSTR and C-style strings, use W2A, OLE2A macros from <AtlBase.h>. Also see CComBSTR class.
-- ===== Arman
Arman Z. Sahakyan wrote:
Note that a bstr is not required to be null terminated
Actually the above is not true, see, for instance: http://www.ecs.syr.edu/faculty/fawcett/handouts/CSE775/Presentations/BruceMcKinneyPapers/COMstrings.htm[^] Anyway I agree on the overall argument, since BSTR may contain NULL inside too. :)
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.
-
LPCSTR d = _com_util::ConvertBSTRToString(FontInformation.Name); MessageBox(0, d, 0, 0);
This still returns garbage data : ( ... what do you make of: "???r" instead of "Arial". And it's sure that it's a BSTR there. For example, my function works but doesn't process the BSTR correctly. It would be easier for me to fix my function ... if only I knew what's wrong : D.-= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^
Axonn Echysttas wrote:
This still returns garbage data : ( ... what do you make of: "???r" instead of "Arial". And it's sure that it's a BSTR there. For example, my function works but doesn't process the BSTR correctly. It would be easier for me to fix my function ... if only I knew what's wrong : D.
Watch memory for this variable to see if the contents is in the variable as you expect it to be.
Nibu thomas A Developer Code must be written to be read, not by the compiler, but by another human being. http:\\nibuthomas.wordpress.com
-
I replaced with wcslen and still same problem. I would prefer to understand how to convert it corectly, manually. That way I don't have to depend on more links and stuff that can break from one windows release to the other : ).
-= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^
Axonn Echysttas wrote:
I would prefer to understand how to convert it corectly, manually
Hence you have to be careful. Have a look at this http://www.ecs.syr.edu/faculty/fawcett/handouts/CSE775/Presentations/BruceMcKinneyPapers/COMstrings.htm[^]. :)
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.
-
Your functions are wrong. Note that a bstr is not required to be null terminated. If you want to get the size of a BSTR, use for example wcslen. To convert between BSTR and C-style strings, use W2A, OLE2A macros from <AtlBase.h>. Also see CComBSTR class.
-- ===== Arman
-
Hi everybody. I got a tiny question about a strange problem I'm having when I'm trying to receive a string from Visual Basic into C++. I got a VB string saying "Arial". But when I'm receiving it in C++, it becomes "Ail". Probably some wide characters versus normal characters problem, as i get 1 character and the next not, and then the next, I get. In order to make the conversion I'm using these two functions.
//Finds out the length of a BSTR. long GetLengthOfBSTR (BSTR Target) { long iRet = 0; //Result to return. long iCursor = 0; //Interator through the BSTR. bool bForever = false; //To cycle forever. //If the string is NULL. if (Target == NULL) return(0); //Returning zero length. while (!bForever) //Cycling forever. { unsigned int uiPiece = Target[iCursor]; //Piece by piece. //Until reaching the null termination of the string. if (uiPiece == 0) return(iRet); //Returning the computed length. else iRet++; //Increasing the length. iCursor++; //Increasing the BSTR iterator. } return -1; //Mostly to avoid warnings ::- ). } //Converts a BSTR to a Char. Needed when working with strings with Visual Basic. char *ConvertBSTRToChar (BSTR Target) { char *sResult; //Result to return. long iCounter; //Iterator. long iLength; //Length of Target. iCounter = iLength = GetLengthOfBSTR(Target); //Initializing. sResult = new char[iLength + 1]; //Creating a character big enough to hold the result. strcpy(sResult, ""); //Initializing. for (iCounter = 0; iCounter < iLength; iCounter++) //Going through the Target BSTR. { unsigned int uiPiece = Target[iCounter]; //Taking it piece by piece. sResult[iCounter] = uiPiece; //Adding it in the final result. sResult[iCounter + 1] = '\0'; //Adding null termination. } return sResult; //Returning computed char. }
And then I do:LPCSTR c; c = ConvertBSTRToChar(StuffFromVB); MessageBox(0, c, "Test", 0);
SO? Anybody here to enlighten me a bit about this? : ). Thank you very much for your time here anyway!-= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^
-
Hi, Try using:
template BSTR ConvertToBSTR(T a_Str) { return(_bstr_t(a_Str).copy()); }
It's the same function I posted in your article two years ago. :) Best Wishes, -Randor (David Delaune)Hhahahaah, Hi : D. That should be a lesson to me huh? To better remember stuff. Hm, however, this is to return a BSTR, I need the other way around. A Char from a BSTR. : D.
-= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^
-
Hhahahaah, Hi : D. That should be a lesson to me huh? To better remember stuff. Hm, however, this is to return a BSTR, I need the other way around. A Char from a BSTR. : D.
-= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^
Hi again, The _bstr_t has a built-in char* operator. So this means you can just do something like this:
BSTR test; char buffer[SOME_LENTH]; _bstr_t bstr(test,true); strcpy(buffer,(char*)bstr);
The above code does not check buffer length. A simple new/delete using the BSTR length should take care of that. :) Best Wishes, Randor (David Delaune) -
Hi again, The _bstr_t has a built-in char* operator. So this means you can just do something like this:
BSTR test; char buffer[SOME_LENTH]; _bstr_t bstr(test,true); strcpy(buffer,(char*)bstr);
The above code does not check buffer length. A simple new/delete using the BSTR length should take care of that. :) Best Wishes, Randor (David Delaune)What's very interesting is that I managed to solve everything just like this: LPCSTR a = (LPCSTR)myBSTR; MessageBox(0, a, 0, 0); //-> it works, if you can believe it!!! Probably because I send only non-Unicode stuff from VB. Anyway, this is how it's gonna stay for a while and I don't intend to ever switch this program to Unicode. Works just fine like it is.
-= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^