Important: Please read the Qt Code of Conduct -

Problems transforming QString to _bstr_t

  • Hi,
    I am trying to transform a QString to _bstr_t type as follows:


    QString mFilename="C:/agatebo/amahoro.doc";

    QByteArray srcBa1 = mFilename.toLocal8Bit();
    const char *srcString1 =;
    CString myStringSrc(srcString1,srcBa1.size());
    BSTR bstrUser = myStringSrc.AllocSysString();
    _bstr_t user(bstrUser,TRUE);


    but when I pass the _bstr_t I get to this function:




    PDFCreator ,a program whose COM interface I am using just crashes .I suspect this has something to do with unicode but can't figure out what yet.I should mention that when I directly pass a path to the file like this:




    all is ok ,I simply want to be able to use QStrings that come from other modules of my Qt application.I am compiling with Qt 4.8 msvc2010 if this matters. I would appreciate any help on this.

  • It probably is related to how the BSTR is created (possibly the type; char vs wchar_t). I do this with the following:

    // char_buffer is a block of memory
    // char_buffer is zero'd prior to use (not shown)

    bstr_val = SysAllocString(char_buffer);

    If you call a function that returns BSTR you are responsible to free the memory associated with the variable.

    bstr = some_com_function_that_returns_BSTR();

    QString return_string(QString::fromWCharArray(bstr,lstrlen(bstr)));

    return return_string;

Log in to reply