|
-
February 22nd, 2010, 11:16 PM
#1
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.
-
February 23rd, 2010, 08:47 AM
#2
Re: trouble converting BSTR
You probably need to link with comsuppw.lib.
-
February 23rd, 2010, 09:09 AM
#3
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
-
February 23rd, 2010, 09:48 PM
#4
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.
-
February 24th, 2010, 03:20 AM
#5
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?
-
February 24th, 2010, 08:54 AM
#6
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?
-
February 24th, 2010, 01:37 PM
#7
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.
-
February 24th, 2010, 01:53 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|