CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2009
    Location
    California
    Posts
    31

    trouble converting BSTR

    I'm using the itunes COM sdk and several of the functions return a BSTR(which is a visual basic string). to be able to use the result in say 'cout <<' it has to be converted but ALL examples i've found online havn't worked. I'm using borlands compiler bcc32. here's my code (with 2 of the ways i've found shown):
    Code:
    BSTR track_str = 0;
    track->getArtist((BSTR *)&track_str);
    //now if i display this i get something like '00xxxxxx' for the output
    //1st method(must include comdef.h)
    _bstr_t bstrt(track_str);
    TCHAR szfinal[255];
    _stprintf(szfinal, _T("%s"),(LPCSTR)bstrt);
    //when i tried to compile i got an error: 'unresolved external  
    __stdcall _com_util::ConvertBSTRToString(wchar_t *) referenced from myfile.obj
    
    //### 2nd method
    wstring wstr;
    string strRet;
    track->getArtist((BSTR *)&track_str);
    wstr = track_str;
    size_t len = wstr.size();
    strRet.resize(len);
    for (int j = 0; int j < len; j++)
    {
        strRet[j] = static_cast<char>(wstr[j]);
    }
    //this method compiles, but when run the program crashes.

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: trouble converting BSTR

    You probably need to link with comsuppw.lib.

  3. #3
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: trouble converting BSTR

    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  4. #4
    Join Date
    Nov 2009
    Location
    California
    Posts
    31

    Re: trouble converting BSTR

    the problem i have with using comsuppw.lib is that it's a vc++ lib and i can't use it(as far as i know) with borland. i could use implib but there's no comsuppw.dll.

  5. #5
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: trouble converting BSTR

    I think I remember that in one of my projects, the only solution I could come up with was to copy the code of _com_util::... which I found somewhere on the net. Back then I either did not know which lib to link to or I didn't find it. Maybe copying the code is an option for you as well?

  6. #6
    Join Date
    Feb 2005
    Posts
    2,160

    Re: trouble converting BSTR

    BSTR is just a wide character string with a few differences: it may not have a NULL terminator, or may have embedded NULLS; it has a DWORD allocated just before the actual pointer returned that contains the length of the string. You can usually ignore those differences in the majority of cases (in practice they can be treated as "wide" character strings). A simple cast to wchar_t * (does Borland have the wchar_t type?) should do the trick. If you want to use std C++ containers, use a std::wstring.

    How is the BSTR being returned from the itunes API? DO you allocate it your self (with SysAllocStr) or is it passed from the API and must be deallocated? Do you deallocate it, or does the API?

  7. #7
    Join Date
    Nov 2009
    Location
    California
    Posts
    31

    Re: trouble converting BSTR

    you raised some good questions hoxiesw: from everything I've seen you don't need to use sysallocate(and as far as i know i don't need to deallocate it), you initialize the bstr to 0, then i'm passing the pointer to that in the function call.
    Code:
    BSTR bstr = 0;
    obj->get_Name((BSTR *)&bstr);
    another thing i found is coff2omf, however the output file is like 1kb. i saw some post on other forums where they said the format changed with VC++ 6 and that you have to convert the lib file to a previous version using vc++ tool - link /lib /convert comsuppw.lib - when i tried that it says /convert is unrecognized.

  8. #8
    Join Date
    Nov 2009
    Location
    California
    Posts
    31

    Re: trouble converting BSTR

    Hi guys, so (lol) I believe I got it working literally 5min after my last post: here's what works(thanks 2 hoxsiew):
    Code:
    wchar_t *wch;
    wch = (wchar_t *)track_str;
    wcout << wch << endl;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured