Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problems transforming QString to _bstr_t
QtWS25 Last Chance

Problems transforming QString to _bstr_t

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • musimbateM Offline
    musimbateM Offline
    musimbate
    wrote on last edited by
    #1

    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 = srcBa1.data();
    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:

    @

    pdfObject->cPrintFile(user);

    @

    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:

    @

    pdfObject->cPrintFile(L"c:\agatebo\amahoro.doc");

    @

    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.

    Why join the navy if you can be a pirate?-Steve Jobs

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rondog
      wrote on last edited by
      #2

      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)

      input_string.toWCharArray(char_buffer);
      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)));
      SysFreeString(bstr);

      return return_string;
      @

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved