I use this code. It's from the VideoLAN project.
 

char *CStrFromBSTR(int codePage, BSTR bstr)
{
    UINT len = SysStringLen(bstr);
    if( len > 0 )
    {
        size_t mblen = WideCharToMultiByte(codePage,
                0, bstr, len, NULL, 0, NULL, NULL);
        if( mblen > 0 )
        {
            char *buffer = (char *)CoTaskMemAlloc(mblen+1);
            ZeroMemory(buffer, mblen+1);
            if( WideCharToMultiByte(codePage, 0, bstr, len, buffer, mblen, NULL, NULL) )
            {
                buffer[mblen] = '\0';
                return buffer;
            }
        }
    }
    return NULL;
};

BSTR BSTRFromCStr(int codePage, const char *s)
{
    int wideLen = MultiByteToWideChar(codePage, 0, s, -1, NULL, 0);
    if( wideLen )
    {
        WCHAR* wideStr = (WCHAR*)CoTaskMemAlloc(wideLen*sizeof(WCHAR));
        if( NULL != wideStr )
        {
            BSTR bstr;

            ZeroMemory(wideStr, wideLen*sizeof(WCHAR));
            MultiByteToWideChar(codePage, 0, s, -1, wideStr, wideLen);
            bstr = SysAllocString(wideStr);
            free(wideStr);

            return bstr;
        }
    }
    return NULL;
};

and then in the code use them like this:
char *strzTest = CStrFromBSTR(CP_ACP, m_bstr);
// use strzTest
free(strzTest);
 
or..
 
BSTR bstrTest = 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 <noreply@sourceforge.net> wrote:

Read and respond to this message at:
https://sourceforge.net/forum/message.php?msg_id=3400628
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=286529


-------------------------------------------------------
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
MinGW-users@lists.sourceforge.net

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users