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. convert Variant to CString?

convert Variant to CString?

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 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.
  • T Offline
    T Offline
    ThinkingPrometheus
    wrote on last edited by
    #1

    hiho@ll i have to convert a string from Variant to CString (or LPCTSTR) i read some articles about bstr conversion, but doesn't seem to work 1. i have a variant variable 2. i want to use MessageBox to show the Variant-String how do i do this? some code would be perfect thx

    G N A J 4 Replies Last reply
    0
    • T ThinkingPrometheus

      hiho@ll i have to convert a string from Variant to CString (or LPCTSTR) i read some articles about bstr conversion, but doesn't seem to work 1. i have a variant variable 2. i want to use MessageBox to show the Variant-String how do i do this? some code would be perfect thx

      G Offline
      G Offline
      Graham Bradshaw
      wrote on last edited by
      #2

      I'll help with the conversion (I'm sure you can handle the MessageBox!)

      CString GetString(const VARIANT* pvaData)
      {
      	if (pvaData->vt == VT_BSTR)
      		return pvaData->bstrVal;
      
      	if (pvaData->vt == (VT_BSTR | VT_BYREF))
      		return *pvaData->pbstrVal;
      
      	if (pvaData->vt == (VT_VARIANT | VT_BYREF))
      		return GetString(pvaData->pvarVal);
      
      
      	VARIANT varDest;
      	VariantInit(&varDest);
      
      
      	if (SUCCEEDED(VariantChangeTypeEx(&varDest, (VARIANT*) pvaData, LOCALE_SYSTEM_DEFAULT, 0, VT_BSTR)))
      		return GetString(&varDest);
      
      	return _T("");
      }
      
      1 Reply Last reply
      0
      • T ThinkingPrometheus

        hiho@ll i have to convert a string from Variant to CString (or LPCTSTR) i read some articles about bstr conversion, but doesn't seem to work 1. i have a variant variable 2. i want to use MessageBox to show the Variant-String how do i do this? some code would be perfect thx

        N Offline
        N Offline
        Nilesh K
        wrote on last edited by
        #3

        Check here[^]

        - Nilesh "Reading made Don Quixote a gentleman. Believing what he read made him mad" -George Bernard Shaw

        T 1 Reply Last reply
        0
        • N Nilesh K

          Check here[^]

          - Nilesh "Reading made Don Quixote a gentleman. Believing what he read made him mad" -George Bernard Shaw

          T Offline
          T Offline
          ThinkingPrometheus
          wrote on last edited by
          #4

          wow cool thx to both of you

          1 Reply Last reply
          0
          • T ThinkingPrometheus

            hiho@ll i have to convert a string from Variant to CString (or LPCTSTR) i read some articles about bstr conversion, but doesn't seem to work 1. i have a variant variable 2. i want to use MessageBox to show the Variant-String how do i do this? some code would be perfect thx

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

            hi you can take the variable of bstr type and assign variant to the bstr and then pass the bstr value in messagebox . It works cheers

            1 Reply Last reply
            0
            • T ThinkingPrometheus

              hiho@ll i have to convert a string from Variant to CString (or LPCTSTR) i read some articles about bstr conversion, but doesn't seem to work 1. i have a variant variable 2. i want to use MessageBox to show the Variant-String how do i do this? some code would be perfect thx

              J Offline
              J Offline
              James R Twine
              wrote on last edited by
              #6

              If you know that the VARIANT contains a BSTR you can get the BSTR, which is really a special form of a wide-character string, and pass it to the wide-character version of MessageBox:

              MessageBoxW( hWndParent, vtMyVariant.bstrVal, L"The Caption", ( MB_ICONINFORMATION | MB_OK ) )

              (You could also pass in a dereferenced pbstrVal.)    Peace! -=- James


              If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
              Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
              DeleteFXPFiles & CheckFavorites (Please rate this post!)

              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