PDA

View Full Version : BSTR to QString Conversion



bismitapadhy
7th April 2010, 13:45
How can i convert BSTR to QString. Please Give a example.

_Stefan
7th April 2010, 13:58
This probably works:



QString convertedBSTR((QChar*) bstr, wcslen(bstr));


Don't forget to free the BSTR, else you will get memory leaks!

bismitapadhy
8th April 2010, 07:42
This probably works:



QString convertedBSTR((QChar*) bstr, wcslen(bstr));


Don't forget to free the BSTR, else you will get memory leaks!

This is not working give me some other solution.
BSTR containing the starting base address of a string. I need to convert into QString.

_Stefan
8th April 2010, 11:39
Can you elaborate on the exact problem?

Show some code, debug messages?

bismitapadhy
8th April 2010, 12:00
Can you elaborate on the exact problem?

Show some code, debug messages?

BSTR codesetID;

if(pGetID_Outron->GetCodesetID(&codesetID, &codesetLength) == S_OK)
{
}
CodesetID containg the starting address.
Now we need to get in QString, to which BSTR is pointing to.

wysota
8th April 2010, 14:16
But what happens when you use the suggested code?

squidge
8th April 2010, 19:12
This probably works:It is my understanding that this will not work as the first 4 bytes of a BSTR contain the length of the string, so you would have to compensate for this. Maybe QString convertedBSTR((QChar*) &bstr[4], wcslen(bstr)); ?

wysota
8th April 2010, 19:57
http://www.codeproject.com/kb/string/bstrsproject1.aspx

hvkleist
7th November 2011, 19:33
Try:

std::wstring intermediateString(myBSTR, SysStringLen(myBSTR);
QString finalString = QString::fromStdWString(intermediateString);

-K

bmn
16th February 2012, 12:51
No need to use std:
QString qstr((QChar*)bstr, ::SysStringLen(bstr));