CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2003
    Posts
    815

    casting from BSTR* to stl string

    Hello,

    I know I asked about the other way...

    Anyone can help me cast from BSTR* to std::string

    Thanks

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449
    I think you mean convert, not cast. A BSTR* is a pointer, a std::string is an object. You can only cast a pointer type to another pointer type.

    Anyway, shouldn't it be a BSTR, not BSTR*? A BSTR is a representation of an entire string, and a std::string represents an entire string.

    Maybe you should clarify what you're trying to do.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Sep 2003
    Posts
    815
    What I'm trying to do

    I have COM interface that is written in C++, but need to server VB & ASP clients

    I want VB to use a specific function in the interface, after trying a few parameters type I saw VB must use BSTR* and not char* as my in parameters (Do you think I can use BSTR instead BSTR*?, then I need this conversion as well...)

    My C++ interface after getting the params for VB is calling a c++ function that its parameters are string* (not string my mistake)
    so I need to convert those VB BSTR* (or BSTR if it's possible) to string* so I could use my function

    thanks
    avi

  4. #4
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Check the FAQ on converting from Unicode to ANSI and modify the code a little so that instead of char *'s you use std::strings. The thing is that BSTRs are Unicode (UTF16) and std::strings are ANSI, so it's more than just casting.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  5. #5
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515

    Re: casting from BSTR* to stl string

    Originally posted by avi123
    Hello,

    I know I asked about the other way...

    Anyone can help me cast from BSTR* to std::string

    Thanks
    As mentioned, it's more of a conversion than casting.
    You said you're using COM; you can use the USES_CONVERSION macros...
    (rough, uncompiled, untested, but the general idea should be there...)
    Code:
    USES_CONVERSION;
    char* psz = BSTR2A( yourbstr );
    std::string thestring( psz );
    or
    Code:
    USES_CONVERSION;
    std::string thestring( BSTR2A( yourbstr ) );
    Of course, use of the macros implies your BSTR doesn't contain embedded NULLs...
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

  6. #6
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    The W2A macros just call WideCharToMultiByte internally anyways and another thing to keep in mind is that they allocate the temporary ANSI string on the stack, so if the strings get large, don't use these macros and more important even, do not use these macros inside loops, otherwise you'll run out of stack space very quickly.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  7. #7
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    Originally posted by Yves M
    The W2A macros just call WideCharToMultiByte internally anyways and another thing to keep in mind is that they allocate the temporary ANSI string on the stack, so if the strings get large, don't use these macros and more important even, do not use these macros inside loops, otherwise you'll run out of stack space very quickly.
    Right, thanks for mentioning that, Yves. If loops are necessary, one could potentially use a separate function to do the conversion. Nonetheless, a large string could still cause a 0xc00000fd.
    If one is using VS.NET, CW2A and related classes are an interesting alternative to the USES_CONVERSION macros... With that family of classes, a stack overflow isn't a concern...
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

  8. #8
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    I just checked those macros out (part of ATL 7) and they suffer from one flaw. They assume that the resulting ANSI string has at most twice as many characters as the original Unicode string. This is fine in many cases, but when e.g. converting to UTF8 this can very easily break things. But well, for most ACPs it should be fine.

    Code:
    From ATL7
    int nLengthW = lstrlenW( psz )+1;
    int nLengthA = nLengthW*2;
    // ...
    ::WideCharToMultiByte( nCodePage, 0, psz, nLengthW, m_psz, nLengthA, NULL, NULL )
    // ...
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  9. #9
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    Originally posted by Yves M
    I just checked those macros out (part of ATL 7)
    Which macros, Yves? The CW*, CT*, and CA* are (template) classes in ATL 7.x, are they not?
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

  10. #10
    Join Date
    Sep 2003
    Posts
    815
    where is BSTR2A is decleraed, I mean what .h file do I need to include

    I get the compilation error: d:\MyClass\MyClass.cpp(17): error C3861: 'BSTR2A': identifier not found, even with argument-dependent lookup

  11. #11
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    Originally posted by avi123
    where is BSTR2A is decleraed, I mean what .h file do I need to include

    I get the compilation error: d:\MyClass\MyClass.cpp(17): error C3861: 'BSTR2A': identifier not found, even with argument-dependent lookup
    Atlconv.h, but you may wish to include atlbase.h.
    Keeping in mind the aforementioned warning about embedded NULLs, perhaps W2CA would work better for you, since BSTR2A doesn't seem to exist. (I checked Google and got hits for it so I assumed it existed, rather than checking atlconv.h, which does not define a BSTR2A...)
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

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