|
From: Devin S. <dev...@gm...> - 2005-10-27 03:45:58
|
I use this code. It's from the VideoLAN project.
char *CStrFromBSTR(int codePage, BSTR bstr)
{
UINT len =3D SysStringLen(bstr);
if( len > 0 )
{
size_t mblen =3D WideCharToMultiByte(codePage,
0, bstr, len, NULL, 0, NULL, NULL);
if( mblen > 0 )
{
char *buffer =3D (char *)CoTaskMemAlloc(mblen+1);
ZeroMemory(buffer, mblen+1);
if( WideCharToMultiByte(codePage, 0, bstr, len, buffer, mblen, NULL, NULL) =
)
{
buffer[mblen] =3D '\0';
return buffer;
}
}
}
return NULL;
};
BSTR BSTRFromCStr(int codePage, const char *s)
{
int wideLen =3D MultiByteToWideChar(codePage, 0, s, -1, NULL, 0);
if( wideLen )
{
WCHAR* wideStr =3D (WCHAR*)CoTaskMemAlloc(wideLen*sizeof(WCHAR));
if( NULL !=3D wideStr )
{
BSTR bstr;
ZeroMemory(wideStr, wideLen*sizeof(WCHAR));
MultiByteToWideChar(codePage, 0, s, -1, wideStr, wideLen);
bstr =3D SysAllocString(wideStr);
free(wideStr);
return bstr;
}
}
return NULL;
};
and then in the code use them like this:
char *strzTest =3D CStrFromBSTR(CP_ACP, m_bstr);
// use strzTest
free(strzTest);
or..
BSTR bstrTest =3D BSTRFromCStr(CP_ACP, m_cstr);
// use bstrTest
SysFreeString(bstrTest);
Switch out CP_ACP (ANSI Code Page) for something else if you wish.
Good luck,
Devin
On 10/26/05, SourceForge.net <no...@so...> wrote:
>
>
> Read and respond to this message at:
> https://sourceforge.net/forum/message.php?msg_id=3D3400628
> By: gamblerzg
>
> I need to convert c string (char *) into BSTR. The usual way to do it is
> through
> a _bstr_t class, whcih is defined in comutil.h. However, MinGW does gives
> an
> endless list errors when I tinclude that header. Is there any solution to
> this
> problem? I have to have BSTR, because I work with Variants, but I really
> do
> not want to use MS Studio.
>
> ______________________________________________________________________
> You are receiving this email because you elected to monitor this forum.
> To stop monitoring this forum, login to SourceForge.net and visit:
> https://sourceforge.net/forum/unmonitor.php?forum_id=3D286529
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by the JBoss Inc.
> Get Certified Today * Register for a JBoss Training Course
> Free Certification Exam for All Training Attendees Through End of 2005
> Visit http://www.jboss.com/services/certification for more information
> _______________________________________________
> MinGW-users mailing list
> Min...@li...
>
> You may change your MinGW Account Options or unsubscribe at:
> https://lists.sourceforge.net/lists/listinfo/mingw-users
>
|