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. VC++ DLL - ? assign value to VARIANT type variable

VC++ DLL - ? assign value to VARIANT type variable

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
6 Posts 2 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.
  • L Offline
    L Offline
    lgatcodeproject
    wrote on last edited by
    #1

    Hi EveryOne, I have added a class which contains a method that accepts a VARIANT type variable in an ActivexDll written in VC++. In one of sample code I found the a value is assigned to the VARIANT type variable using a SAFEARRAY. My question is how can a string value be assigned to VARIANT type variable(type casting in strcpy does't work). Can we achieve it only by using SAFEARRAY? if yes why is it so? Is there any other way to obtain the same? kindly explain. Thank you. Regards, LG.

    lgatcodeproject

    CPalliniC L 2 Replies Last reply
    0
    • L lgatcodeproject

      Hi EveryOne, I have added a class which contains a method that accepts a VARIANT type variable in an ActivexDll written in VC++. In one of sample code I found the a value is assigned to the VARIANT type variable using a SAFEARRAY. My question is how can a string value be assigned to VARIANT type variable(type casting in strcpy does't work). Can we achieve it only by using SAFEARRAY? if yes why is it so? Is there any other way to obtain the same? kindly explain. Thank you. Regards, LG.

      lgatcodeproject

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Probably you should use a BSTR insted. :)

      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

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • L lgatcodeproject

        Hi EveryOne, I have added a class which contains a method that accepts a VARIANT type variable in an ActivexDll written in VC++. In one of sample code I found the a value is assigned to the VARIANT type variable using a SAFEARRAY. My question is how can a string value be assigned to VARIANT type variable(type casting in strcpy does't work). Can we achieve it only by using SAFEARRAY? if yes why is it so? Is there any other way to obtain the same? kindly explain. Thank you. Regards, LG.

        lgatcodeproject

        L Offline
        L Offline
        lgatcodeproject
        wrote on last edited by
        #3

        Hi, Even if I use the BSTR type while copying a string constant using strcpy with a type cast the application crashes. How to copy a string constant by-passing this problem? Please find the below snippet. STDMETHODIMP CRAPClientX::Connect(BSTR serverIP, BSTR serverPort,BSTR output) { // TODO: Add your implementation code here **strcpy((char *)output, "connected");** return S_OK; } Regards, LG.

        lgatcodeproject

        CPalliniC 1 Reply Last reply
        0
        • L lgatcodeproject

          Hi, Even if I use the BSTR type while copying a string constant using strcpy with a type cast the application crashes. How to copy a string constant by-passing this problem? Please find the below snippet. STDMETHODIMP CRAPClientX::Connect(BSTR serverIP, BSTR serverPort,BSTR output) { // TODO: Add your implementation code here **strcpy((char *)output, "connected");** return S_OK; } Regards, LG.

          lgatcodeproject

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          lgatcodeproject wrote:

          STDMETHODIMP CRAPClientX::Connect(BSTR serverIP, BSTR serverPort,BSTR output) { // TODO: Add your implementation code here strcpy((char *)output, "connected"); return S_OK; }

          Change to

          STDMETHODIMP CRAPClientX::Connect(BSTR serverIP, BSTR serverPort,BSTR * pOutput)
          {
          *pOutput = SysAllocString(L"connected");
          return S_OK;
          }

          :)

          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

          In testa che avete, signor di Ceprano?

          L 1 Reply Last reply
          0
          • CPalliniC CPallini

            lgatcodeproject wrote:

            STDMETHODIMP CRAPClientX::Connect(BSTR serverIP, BSTR serverPort,BSTR output) { // TODO: Add your implementation code here strcpy((char *)output, "connected"); return S_OK; }

            Change to

            STDMETHODIMP CRAPClientX::Connect(BSTR serverIP, BSTR serverPort,BSTR * pOutput)
            {
            *pOutput = SysAllocString(L"connected");
            return S_OK;
            }

            :)

            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

            L Offline
            L Offline
            lgatcodeproject
            wrote on last edited by
            #5

            Hi, Thanks its working now but then I have a doubt, what is that L do inside SysAllocString()function. Regards, LG.

            lgatcodeproject

            CPalliniC 1 Reply Last reply
            0
            • L lgatcodeproject

              Hi, Thanks its working now but then I have a doubt, what is that L do inside SysAllocString()function. Regards, LG.

              lgatcodeproject

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              A BSTR is substantially a wide char string (technically it is a OLECHAR one). the L prefix creates a wide char literal. :)

              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

              In testa che avete, signor di Ceprano?

              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