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. COM
  4. How to convert a CString to _bstr_t

How to convert a CString to _bstr_t

Scheduled Pinned Locked Moved COM
tutorial
5 Posts 3 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.
  • G Offline
    G Offline
    ganesa moorthy
    wrote on last edited by
    #1

    Hi, Can any one expplain How to convert a CString to _bstr_t...... thanks in advance

    Thanks a lot

    R 1 Reply Last reply
    0
    • G ganesa moorthy

      Hi, Can any one expplain How to convert a CString to _bstr_t...... thanks in advance

      Thanks a lot

      R Offline
      R Offline
      Roger Stoltz
      wrote on last edited by
      #2

      One of the constructors of _bstr_t takes a const char* as argument. Since CString has a type conversion operator LPCTSTR that returns a const char* you can do the following:

      CString str = "The string";
      _bstr_t bstrTest( str ); // When you declare your _bstr_t variable, or
      bstrTest = _bstr_t( str ); // when assigning the variable


      "It's supposed to be hard, otherwise anybody could do it!" - selfquote
      "High speed never compensates for wrong direction!" - unknown

      S 1 Reply Last reply
      0
      • R Roger Stoltz

        One of the constructors of _bstr_t takes a const char* as argument. Since CString has a type conversion operator LPCTSTR that returns a const char* you can do the following:

        CString str = "The string";
        _bstr_t bstrTest( str ); // When you declare your _bstr_t variable, or
        bstrTest = _bstr_t( str ); // when assigning the variable


        "It's supposed to be hard, otherwise anybody could do it!" - selfquote
        "High speed never compensates for wrong direction!" - unknown

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

        [fx: pedant mode on]

        Roger Stoltz wrote:

        Since CString has a type conversion operator LPCTSTR that returns a const char* you can do the following:

        No it doesn't. It returns const TCHAR*, which in a UNICODE build won't be the same as const char*. However, _bstr_t has a constructor that accepts const wchar_t* as well as one for const char*, so it will work in either case. [fx: pedant mode off] :)

        Steve S Developer for hire

        R 1 Reply Last reply
        0
        • S Steve S

          [fx: pedant mode on]

          Roger Stoltz wrote:

          Since CString has a type conversion operator LPCTSTR that returns a const char* you can do the following:

          No it doesn't. It returns const TCHAR*, which in a UNICODE build won't be the same as const char*. However, _bstr_t has a constructor that accepts const wchar_t* as well as one for const char*, so it will work in either case. [fx: pedant mode off] :)

          Steve S Developer for hire

          R Offline
          R Offline
          Roger Stoltz
          wrote on last edited by
          #4

          Now you're just being picky Steve... but you're right of course. :-D However, I think the OP got the picture and depending on the OP's level of expertise it may have been confusing to start talking about unicode.


          "It's supposed to be hard, otherwise anybody could do it!" - selfquote
          "High speed never compensates for wrong direction!" - unknown

          S 1 Reply Last reply
          0
          • R Roger Stoltz

            Now you're just being picky Steve... but you're right of course. :-D However, I think the OP got the picture and depending on the OP's level of expertise it may have been confusing to start talking about unicode.


            "It's supposed to be hard, otherwise anybody could do it!" - selfquote
            "High speed never compensates for wrong direction!" - unknown

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

            Of course I'm being picky. When you get to my age, it's one of the few pleasures left in life that doesn't cost money or end up causing some kind of muscular spasm... X| I agree that it might have been confusing, which is one of the problems of CP. It's hard to know what level the OP is at (or in some cases, they think they're at), and that makes it difficult sometimes to give the 'right' answer, because they may end up worse off than before. I did like the thread here[^] where someone else came in and finished explaining it while I was asleep :)

            Steve S Developer for hire

            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