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. _variant_t to CString

_variant_t to CString

Scheduled Pinned Locked Moved C / C++ / MFC
question
16 Posts 4 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.
  • C CPallini

    I suppose you need the string representation of the _variant_t value. You've to properly format a CString object, depending on the value of the vt member of the encapsulated VARIANT struct. For instance

    CString str;
    _variant_t v1;

    v1 = 0.53;

    switch (v1.vt)
    {
    //...
    case VT_R8:
    str.Format(_T("%g"), v1.dblVal);
    break;
    //...
    }

    MessageBox(str, _T("Variant value"));

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
    [My articles]

    S Offline
    S Offline
    Stuart Dootson
    wrote on last edited by
    #4

    Noooooo - you don't need to bother with that - there's a VariantChangeType function that'll do that for you when you convert a VARIANT to a BSTR. And even more conveniently, the combination of _variant_t extractor operators and CString constructors will do all that for you. A simple assignment of _variant_t to CString is all that's needed...

    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

    C 1 Reply Last reply
    0
    • C CPallini

      I suppose you need the string representation of the _variant_t value. You've to properly format a CString object, depending on the value of the vt member of the encapsulated VARIANT struct. For instance

      CString str;
      _variant_t v1;

      v1 = 0.53;

      switch (v1.vt)
      {
      //...
      case VT_R8:
      str.Format(_T("%g"), v1.dblVal);
      break;
      //...
      }

      MessageBox(str, _T("Variant value"));

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      R Offline
      R Offline
      rdop
      wrote on last edited by
      #5

      Thanks for responce.I hava a biray type data in _variant_t. _variant_t v1; v1=(54 '6',53 '5',52 '4',54 '6',52 '4',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); Can i convert these values in CString or not. Origal values is"65464". Plz help me

      C 1 Reply Last reply
      0
      • S Stuart Dootson

        In VS2008, this should do the job...

        _variant_t v;
        // Set a value in v
        CString s = v;

        CString has assignment operator and constructor overl;oads that take a const VARIANT&. _variant_t has an extrractor that returns a VARIANT.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        R Offline
        R Offline
        rdop
        wrote on last edited by
        #6

        Plz help me some one

        S 1 Reply Last reply
        0
        • R rdop

          Hi All How can i convert _variant_t to CString?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #7

          Hi rdop, The following snippit will work on all Microsoft compilers:_variant_t v; v = 3.141; ::VariantChangeType(&v, &v, 0, VT_BSTR); CString s = (LPTSTR)(_bstr_t)v.bstrVal;
          Best Wishes, -David Delaune

          1 Reply Last reply
          0
          • R rdop

            Plz help me some one

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #8

            I (and Microsoft, who wrote the _variant_t and CString code) have helped you - you just assign the _variant_t to the CString - what more do you want?!?!?!?!?

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            R 1 Reply Last reply
            0
            • S Stuart Dootson

              Noooooo - you don't need to bother with that - there's a VariantChangeType function that'll do that for you when you convert a VARIANT to a BSTR. And even more conveniently, the combination of _variant_t extractor operators and CString constructors will do all that for you. A simple assignment of _variant_t to CString is all that's needed...

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #9

              Nice to know :thumbsup:. What about _variant_t containing arrays? :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              S 1 Reply Last reply
              0
              • S Stuart Dootson

                I (and Microsoft, who wrote the _variant_t and CString code) have helped you - you just assign the _variant_t to the CString - what more do you want?!?!?!?!?

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                R Offline
                R Offline
                rdop
                wrote on last edited by
                #10

                See what happen.

                _variant_t vtValue;
                vtValue = m_pRset->Fields->GetItem("Bianry")->GetValue();
                CString b=vtValue;
                output is b = "??4"
                when i copy the vtValues at the time of debug then i got values
                safearray of UI1 = [20](54 '6',53 '5',52 '4',54 '6',52 '4',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)

                Any more information if you nedd then i will give you.

                S 1 Reply Last reply
                0
                • R rdop

                  Thanks for responce.I hava a biray type data in _variant_t. _variant_t v1; v1=(54 '6',53 '5',52 '4',54 '6',52 '4',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); Can i convert these values in CString or not. Origal values is"65464". Plz help me

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #11

                  What is v1.vt value? :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  1 Reply Last reply
                  0
                  • R rdop

                    See what happen.

                    _variant_t vtValue;
                    vtValue = m_pRset->Fields->GetItem("Bianry")->GetValue();
                    CString b=vtValue;
                    output is b = "??4"
                    when i copy the vtValues at the time of debug then i got values
                    safearray of UI1 = [20](54 '6',53 '5',52 '4',54 '6',52 '4',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)

                    Any more information if you nedd then i will give you.

                    S Offline
                    S Offline
                    Stuart Dootson
                    wrote on last edited by
                    #12

                    As it's an array, VariantChangeType's not going to work. So, I'd suggest the code below. I'm assuming you want to access the non-null characters of the array and make them into a string?

                    CComSafeArray<char, VT_UI1> sa(V_ARRAY(&v));
                    CString s;
                    for(int index=sa.GetLowerBound();index<=sa.GetUpperBound()&&sa[index];++index)
                    {
                    s+=sa[index];
                    }

                    [edit] You'll also need to #include atlsafe.h and oleauto.h [/edit]

                    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                    L R 2 Replies Last reply
                    0
                    • C CPallini

                      Nice to know :thumbsup:. What about _variant_t containing arrays? :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      S Offline
                      S Offline
                      Stuart Dootson
                      wrote on last edited by
                      #13

                      CPallini wrote:

                      What about _variant_t containing arrays?

                      Use CComSafeArray[^].

                      _variant_t v;
                      // Assign a SAFEARRAY value to v
                      CComSafeArray<char, VT_UI1> sa(V_ARRAY(&v));
                      // Now we can access the SAFEARRAY using sa.

                      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                      1 Reply Last reply
                      0
                      • S Stuart Dootson

                        As it's an array, VariantChangeType's not going to work. So, I'd suggest the code below. I'm assuming you want to access the non-null characters of the array and make them into a string?

                        CComSafeArray<char, VT_UI1> sa(V_ARRAY(&v));
                        CString s;
                        for(int index=sa.GetLowerBound();index<=sa.GetUpperBound()&&sa[index];++index)
                        {
                        s+=sa[index];
                        }

                        [edit] You'll also need to #include atlsafe.h and oleauto.h [/edit]

                        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #14

                        Hi Stuart, What do you think about this VT_UI1 SAFEARRAY to CStringA conversion method?BSTR b; BstrFromVector(pSA,&b); CStringA s = (LPCSTR)b;
                        Best Wishes, -David Delaune

                        S 1 Reply Last reply
                        0
                        • L Lost User

                          Hi Stuart, What do you think about this VT_UI1 SAFEARRAY to CStringA conversion method?BSTR b; BstrFromVector(pSA,&b); CStringA s = (LPCSTR)b;
                          Best Wishes, -David Delaune

                          S Offline
                          S Offline
                          Stuart Dootson
                          wrote on last edited by
                          #15

                          New to me - that'll teach me to do what I've told many other people to do and read the documentation :laugh: And of course it'll work with wide CStrings as well.

                          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                          1 Reply Last reply
                          0
                          • S Stuart Dootson

                            As it's an array, VariantChangeType's not going to work. So, I'd suggest the code below. I'm assuming you want to access the non-null characters of the array and make them into a string?

                            CComSafeArray<char, VT_UI1> sa(V_ARRAY(&v));
                            CString s;
                            for(int index=sa.GetLowerBound();index<=sa.GetUpperBound()&&sa[index];++index)
                            {
                            s+=sa[index];
                            }

                            [edit] You'll also need to #include atlsafe.h and oleauto.h [/edit]

                            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                            R Offline
                            R Offline
                            rdop
                            wrote on last edited by
                            #16

                            thanks it's working for me.

                            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