Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. How to convert *string to _bstr_t and *BSTR

How to convert *string to _bstr_t and *BSTR

Scheduled Pinned Locked Moved ATL / WTL / STL
tutorialc++jsonhelp
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    agarunk
    wrote on last edited by
    #1

    Hi, I'm creating dll using VS2005 ATL project. I'm using third party SDK API's which extracts data into a *string variable. Now I'm facing problem with converting the *string to _bstr_t and *BSTR. Please guide me whether is it possible to convert *string to _bstr_t and *BSTR, if so how to do it. If possible with a sample piece of code. thanking you, Arun

    L M 2 Replies Last reply
    0
    • A agarunk

      Hi, I'm creating dll using VS2005 ATL project. I'm using third party SDK API's which extracts data into a *string variable. Now I'm facing problem with converting the *string to _bstr_t and *BSTR. Please guide me whether is it possible to convert *string to _bstr_t and *BSTR, if so how to do it. If possible with a sample piece of code. thanking you, Arun

      L Offline
      L Offline
      Lim Bio Liong
      wrote on last edited by
      #2

      Hello agarunk, By "*string", do you mean a pointer to a STL string ? By "*BSTR", do you mean a pointer to a BSTR ? - Bio.

      A 1 Reply Last reply
      0
      • L Lim Bio Liong

        Hello agarunk, By "*string", do you mean a pointer to a STL string ? By "*BSTR", do you mean a pointer to a BSTR ? - Bio.

        A Offline
        A Offline
        agarunk
        wrote on last edited by
        #3

        Hi Bio, Its "std::string*", i.e., pointer to std string. and yes "BSTR*" is pointer to a BSTR. -Arun

        S 1 Reply Last reply
        0
        • A agarunk

          Hi Bio, Its "std::string*", i.e., pointer to std string. and yes "BSTR*" is pointer to a BSTR. -Arun

          S Offline
          S Offline
          Steve S
          wrote on last edited by
          #4

          Both the _bstr_t wrapper class and BSTR expect the text to be UNICODE, rather than ANSI or MBCS. While _bstr_t can convert from LPCSTR, BSTR on it's own can't. You will need to use the c_str() member function of the std::string to get your LPCSTR in the first place (unlike MFC/ATL/WTL CString class, there's no LPCTSTR operator). Like I say, you can then use this pointer with something like A2W or MultiByteToWideChar.

          Steve S Developer for hire

          A Z 2 Replies Last reply
          0
          • S Steve S

            Both the _bstr_t wrapper class and BSTR expect the text to be UNICODE, rather than ANSI or MBCS. While _bstr_t can convert from LPCSTR, BSTR on it's own can't. You will need to use the c_str() member function of the std::string to get your LPCSTR in the first place (unlike MFC/ATL/WTL CString class, there's no LPCTSTR operator). Like I say, you can then use this pointer with something like A2W or MultiByteToWideChar.

            Steve S Developer for hire

            A Offline
            A Offline
            agarunk
            wrote on last edited by
            #5

            Hi Steve, Below is the implementation which I'm using now.... BSTR* TargetBSTR ; string *SourceStr=new string; string StrBuff; const char *CharBuff; StrBuff=*SourceStr; CharBuff=StrBuff.c_str(); *TargetBSTR = _com_util::ConvertStringToBSTR(CharBuff); Is this what you are asking me to do??? It is working fine for me for time being... I would like to know if I'm doing any mistake. Is there any issues with it??? -Arun

            Z 1 Reply Last reply
            0
            • A agarunk

              Hi, I'm creating dll using VS2005 ATL project. I'm using third party SDK API's which extracts data into a *string variable. Now I'm facing problem with converting the *string to _bstr_t and *BSTR. Please guide me whether is it possible to convert *string to _bstr_t and *BSTR, if so how to do it. If possible with a sample piece of code. thanking you, Arun

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #6

              Check out The Complete Guide to C++ Strings, Part II - String Wrapper Classes[^]

              --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

              1 Reply Last reply
              0
              • S Steve S

                Both the _bstr_t wrapper class and BSTR expect the text to be UNICODE, rather than ANSI or MBCS. While _bstr_t can convert from LPCSTR, BSTR on it's own can't. You will need to use the c_str() member function of the std::string to get your LPCSTR in the first place (unlike MFC/ATL/WTL CString class, there's no LPCTSTR operator). Like I say, you can then use this pointer with something like A2W or MultiByteToWideChar.

                Steve S Developer for hire

                Z Offline
                Z Offline
                Zac Howland
                wrote on last edited by
                #7

                Steve S wrote:

                Both the _bstr_t wrapper class and BSTR expect the text to be UNICODE, rather than ANSI or MBCS. While _bstr_t can convert from LPCSTR, BSTR on it's own can't.

                This isn't completely accurate. _bstr_t has a constructor that takes a const char*. It will do the conversion for you. Basically, if you need to convert std::string* to a _bstr_t* (or even to a BSTR*), here is a simple way to do it:

                std::string* pStlString = new string("my stuff");
                _bstr_t* pBstrWrapper = new _bstr_t(pStlString->c_str());
                BSTR* pBstr = pBstrWrapper->GetAddress();
                

                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                S 1 Reply Last reply
                0
                • A agarunk

                  Hi Steve, Below is the implementation which I'm using now.... BSTR* TargetBSTR ; string *SourceStr=new string; string StrBuff; const char *CharBuff; StrBuff=*SourceStr; CharBuff=StrBuff.c_str(); *TargetBSTR = _com_util::ConvertStringToBSTR(CharBuff); Is this what you are asking me to do??? It is working fine for me for time being... I would like to know if I'm doing any mistake. Is there any issues with it??? -Arun

                  Z Offline
                  Z Offline
                  Zac Howland
                  wrote on last edited by
                  #8

                  agarunk wrote:

                  BSTR* TargetBSTR ; string *SourceStr=new string; string StrBuff; const char *CharBuff; StrBuff=*SourceStr; CharBuff=StrBuff.c_str(); *TargetBSTR = _com_util::ConvertStringToBSTR(CharBuff);

                  That is far more code than you need to do for this. See my response below.

                  If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                  1 Reply Last reply
                  0
                  • Z Zac Howland

                    Steve S wrote:

                    Both the _bstr_t wrapper class and BSTR expect the text to be UNICODE, rather than ANSI or MBCS. While _bstr_t can convert from LPCSTR, BSTR on it's own can't.

                    This isn't completely accurate. _bstr_t has a constructor that takes a const char*. It will do the conversion for you. Basically, if you need to convert std::string* to a _bstr_t* (or even to a BSTR*), here is a simple way to do it:

                    std::string* pStlString = new string("my stuff");
                    _bstr_t* pBstrWrapper = new _bstr_t(pStlString->c_str());
                    BSTR* pBstr = pBstrWrapper->GetAddress();
                    

                    If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                    S Offline
                    S Offline
                    Steve S
                    wrote on last edited by
                    #9

                    I sit corrected :) I confess that these days I very rarely use ANSI/MBCS, since I no longer do stuff for Win9x/ME platforms. Lucky me, my end-customers all use W2K or later...

                    Steve S Developer for hire

                    Z 1 Reply Last reply
                    0
                    • S Steve S

                      I sit corrected :) I confess that these days I very rarely use ANSI/MBCS, since I no longer do stuff for Win9x/ME platforms. Lucky me, my end-customers all use W2K or later...

                      Steve S Developer for hire

                      Z Offline
                      Z Offline
                      Zac Howland
                      wrote on last edited by
                      #10

                      Since I'm in the Linux world at my new job, I'm a bit rusty myself. I do find that using the T-functions (along with classes that can use both char* and wchar_t*) alleviates much of those headaches.

                      If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups