Conversion between QString and BSTR I'm currently working on a Qt project writing Excel files through a third party DLL.
As the third party DLL has a COM interface, I have to do lots of conversions between QString and BSTR...

I wrote the following two methods to do the conversions:

QString ExcelClass::bstrToQString(const BSTR &amp;bStr) const
{
        _bstr_t convert(bStr, false);
        return QString::fromUtf16(reinterpret_cast&lt;const ushort*&gt;((const WCHAR*)convert));
}

BSTR ExcelClass::qstringToBstr(const QString &amp;qstring) const
{
        WCHAR* const pBuffer = new WCHAR[1000000]; //We have to deal with very large strings...
        MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, qstring.toLocal8Bit().data(), -1, pBuffer, 1000000);
        BSTR result = SysAllocString(pBuffer);
        delete [] pBuffer;
        return result;
}


Even if those methods do their job properly, I have the following questions:


Are those conversions completely correct or can they cause errors in rare cases?
Is there any other (better/easier) way to convert?
Can I somehow avoid allocating pBuffer every time I call the function?


The line WCHAR* const pBuffer = new WCHAR[1000000]; seems to slow the program down significantly, as I am calling the method ~60000 times... I can speed it up a lot when I do the conversion (qstringToBstr()) directly inside the calling function but I want it capsuled in this method...
(converted directly inside the caller, memory was only allocated once per 5-column-line, so still ~12000 times, but that was much faster...)
why not use the toWcharArray?

BSTR ExcelClass::qstringToBstr(const QString &amp;qstring) const
{
        WCHAR* const pBuffer = new WCHAR[qstring.size()+1]; //large enough to hold the string plus null terminator
        qstring.toWCharArray(pBuffer);
        pBuffer[qstring.size()]=0;//terminate with 0
        BSTR result = SysAllocString(pBuffer);
        delete [] pBuffer;
        return result;
}


or simply preallocate result with SysAllocStringLen(&amp;result, qstring.length()); and translate into that

BSTR ExcelClass::qstringToBstr(const QString &amp;qstring) const
{            
        BSTR result= SysAllocStringLen(0, qstring.length());
        qstring.toWCharArray(result);
        return result;
}