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. C / C++ / MFC
  4. BSTR --> CString ?

BSTR --> CString ?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
14 Posts 9 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.
  • D Daniel Strigl

    What's the fastest and easiest way to convert a BSTR to a MFC CString? Daniel ;) --------------------------- Never change a running system!

    C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #4

    _bstr_t is a wrapper class that does the conversion for you, so you can do this: CString s((_bstr_t)BSTR); This will create a _bstr_t, which will return a char * to the CString constructor. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002 Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002

    K D 2 Replies Last reply
    0
    • C Christian Graus

      _bstr_t is a wrapper class that does the conversion for you, so you can do this: CString s((_bstr_t)BSTR); This will create a _bstr_t, which will return a char * to the CString constructor. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002 Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002

      K Offline
      K Offline
      klphua
      wrote on last edited by
      #5

      sorry i forgot, that bstr is of type _bstr_t. u r rite, Chris.

      C 1 Reply Last reply
      0
      • C Christian Graus

        _bstr_t is a wrapper class that does the conversion for you, so you can do this: CString s((_bstr_t)BSTR); This will create a _bstr_t, which will return a char * to the CString constructor. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002 Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002

        D Offline
        D Offline
        Daniel Strigl
        wrote on last edited by
        #6

        Thanks! Daniel ;) --------------------------- Never change a running system!

        1 Reply Last reply
        0
        • C Christian Graus

          Does that work ? I don't see how it can..... Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002 Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002

          D Offline
          D Offline
          Daniel Turini
          wrote on last edited by
          #7

          Yes, this works, but be careful: by definition a NULL BSTR is an empty string. This cast can be dangerous. Your solution is safer. Q261186 - Computer Randomly Plays Classical Music

          1 Reply Last reply
          0
          • K klphua

            sorry i forgot, that bstr is of type _bstr_t. u r rite, Chris.

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #8

            No worries :-) Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002 Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002

            1 Reply Last reply
            0
            • D Daniel Strigl

              What's the fastest and easiest way to convert a BSTR to a MFC CString? Daniel ;) --------------------------- Never change a running system!

              Y Offline
              Y Offline
              Yaron Nir
              wrote on last edited by
              #9

              the best & easy way to conver BSTR to string is use the com_util helper as follows: _bstr_t tmpBSTR = _com_util::ConvertStringToBSTR(szString); or the other way around: CString szString = _com_util::ConvertBSTRToString((BSTR)tmpBSTR); you can check out the definitions of this functions in COMUTIL.H hope this helps ya Yaron Ask not what the application can do for you, ask what you can do for your application

              D 1 Reply Last reply
              0
              • Y Yaron Nir

                the best & easy way to conver BSTR to string is use the com_util helper as follows: _bstr_t tmpBSTR = _com_util::ConvertStringToBSTR(szString); or the other way around: CString szString = _com_util::ConvertBSTRToString((BSTR)tmpBSTR); you can check out the definitions of this functions in COMUTIL.H hope this helps ya Yaron Ask not what the application can do for you, ask what you can do for your application

                D Offline
                D Offline
                dabs
                wrote on last edited by
                #10

                Nope, the best and easiest way to construct a CString from a BSTR is to use the LPCWSTR constructor that CString has. So: CString str( (LPCWSTR)bstr ); should do what you want. Note that this works because a BSTR is also a LPCWSTR (although a LPCWSTR is not necessarily a BSTR!). This method means that no temporary copies will be made, the Unicode-to-MBCS conversion will be made inside CString and no unnecessary string manipulation happens. Using _bstr_t will also work but with the overhead of a temporary copy of both the _bstr_t and the char* (_bstr_t will first construct itself from BSTR, then it will make an MBCS version and then CString will make a copy of it).


                Wenn ist das Nunstück git und Slotermeyer? Ja! Beierhund das oder die Flipperwaldt gersput!

                L 1 Reply Last reply
                0
                • D dabs

                  Nope, the best and easiest way to construct a CString from a BSTR is to use the LPCWSTR constructor that CString has. So: CString str( (LPCWSTR)bstr ); should do what you want. Note that this works because a BSTR is also a LPCWSTR (although a LPCWSTR is not necessarily a BSTR!). This method means that no temporary copies will be made, the Unicode-to-MBCS conversion will be made inside CString and no unnecessary string manipulation happens. Using _bstr_t will also work but with the overhead of a temporary copy of both the _bstr_t and the char* (_bstr_t will first construct itself from BSTR, then it will make an MBCS version and then CString will make a copy of it).


                  Wenn ist das Nunstück git und Slotermeyer? Ja! Beierhund das oder die Flipperwaldt gersput!

                  L Offline
                  L Offline
                  Le centriste
                  wrote on last edited by
                  #11

                  The casting is unneeded here, since: typedef wchar_t *LPCWSTR; typedef wchar_t *BSTR; There is no need for casting, unless bstr is not a pure BSTR. Michel It is a lovely language, but it takes a very long time to say anything in it, because we do not say anything in it, unless it is worth taking a very long time to say, and to listen to.
                  - TreeBeard

                  D 1 Reply Last reply
                  0
                  • L Le centriste

                    The casting is unneeded here, since: typedef wchar_t *LPCWSTR; typedef wchar_t *BSTR; There is no need for casting, unless bstr is not a pure BSTR. Michel It is a lovely language, but it takes a very long time to say anything in it, because we do not say anything in it, unless it is worth taking a very long time to say, and to listen to.
                    - TreeBeard

                    D Offline
                    D Offline
                    dabs
                    wrote on last edited by
                    #12

                    Correct. It does however make it clear what constructor we are using - but then again we might not care :-)


                    Wenn ist das Nunstück git und Slotermeyer? Ja! Beierhund das oder die Flipperwaldt gersput!

                    J 1 Reply Last reply
                    0
                    • D dabs

                      Correct. It does however make it clear what constructor we are using - but then again we might not care :-)


                      Wenn ist das Nunstück git und Slotermeyer? Ja! Beierhund das oder die Flipperwaldt gersput!

                      J Offline
                      J Offline
                      JT Anderson
                      wrote on last edited by
                      #13

                      It is a gratuitious cast and has the potential to break the code in the future. NEVER use an explicit cast unless you have to. -------- There are 10 types of people in this world. Those who know binary and those who don't.

                      1 Reply Last reply
                      0
                      • D Daniel Strigl

                        What's the fastest and easiest way to convert a BSTR to a MFC CString? Daniel ;) --------------------------- Never change a running system!

                        A Offline
                        A Offline
                        Alvaro Mendez
                        wrote on last edited by
                        #14

                        CString str = bstr; Regards, Alvaro There is much pleasure to be gained from useless knowledge. - Bertrand Russell

                        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