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. Passing a structure through VARIANT from VC++ Client to COM Server [modified]

Passing a structure through VARIANT from VC++ Client to COM Server [modified]

Scheduled Pinned Locked Moved COM
c++comsysadmintutorial
7 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.
  • G Offline
    G Offline
    georgekjolly
    wrote on last edited by
    #1

    Hai all, Do anyone know , how to pass a user define structure from a VC++ Client to a COM Server, through VARIANT. Thanks, George.

    C 1 Reply Last reply
    0
    • G georgekjolly

      Hai all, Do anyone know , how to pass a user define structure from a VC++ Client to a COM Server, through VARIANT. Thanks, George.

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

      You don't need a VARIANT. Anyway, you can pass a pointer of the struct in the byref member of the VARIANT union (of course the COM server must be aware of the struct declaration). Hope that helps. :)

      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.

      G 1 Reply Last reply
      0
      • C CPallini

        You don't need a VARIANT. Anyway, you can pass a pointer of the struct in the byref member of the VARIANT union (of course the COM server must be aware of the struct declaration). Hope that helps. :)

        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.

        G Offline
        G Offline
        georgekjolly
        wrote on last edited by
        #3

        Hi Pallini, My code is given below /************Client Side****************************************/ typedef struct { int i; char ch[10]; }StrctMine; VARIANT vrt; StrctMine gOb; gOb.i = 10; strcpy(gOb.ch,"geo"); vrt.vt = VT_BYREF; vrt.byref = (void*)&gOb; myPtr.CreateInstance(__uuidof(MyVar)); myPtr->VarFun(&vrt); /*************************************************/ In Server side , I have written my structure in idl file, And I called a method , that is given below. STDMETHODIMP CMyVar::VarFun(VARIANT *vPtr_i) { // TODO: Add your implementation code here MessageBox(NULL,"Hai","",MB_OK); return S_OK; } But this code is not working. Thanks George

        M C L 4 Replies Last reply
        0
        • G georgekjolly

          Hi Pallini, My code is given below /************Client Side****************************************/ typedef struct { int i; char ch[10]; }StrctMine; VARIANT vrt; StrctMine gOb; gOb.i = 10; strcpy(gOb.ch,"geo"); vrt.vt = VT_BYREF; vrt.byref = (void*)&gOb; myPtr.CreateInstance(__uuidof(MyVar)); myPtr->VarFun(&vrt); /*************************************************/ In Server side , I have written my structure in idl file, And I called a method , that is given below. STDMETHODIMP CMyVar::VarFun(VARIANT *vPtr_i) { // TODO: Add your implementation code here MessageBox(NULL,"Hai","",MB_OK); return S_OK; } But this code is not working. Thanks George

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          VT_BYREF by itself isn't a legal type. If you must pass the struct in a VARIANT, you'll need to serialize it into a packet of bytes (for example, XML) and pass that as a SAFEARRAY of VT_UI1. There is also IRecordInfo, which MSDN says is for passing UDTs, but I've never used it myself.

          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

          1 Reply Last reply
          0
          • G georgekjolly

            Hi Pallini, My code is given below /************Client Side****************************************/ typedef struct { int i; char ch[10]; }StrctMine; VARIANT vrt; StrctMine gOb; gOb.i = 10; strcpy(gOb.ch,"geo"); vrt.vt = VT_BYREF; vrt.byref = (void*)&gOb; myPtr.CreateInstance(__uuidof(MyVar)); myPtr->VarFun(&vrt); /*************************************************/ In Server side , I have written my structure in idl file, And I called a method , that is given below. STDMETHODIMP CMyVar::VarFun(VARIANT *vPtr_i) { // TODO: Add your implementation code here MessageBox(NULL,"Hai","",MB_OK); return S_OK; } But this code is not working. Thanks George

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

            I have to admint (and I don't like it a bit) that I underestimated you problem (and the fact that memory have to cross process boundaries). Anyway, there are, of course, solutions. You can: (1) Take a drastic approach, coverting you struct into a COM object, which can be created and passed to other COM objects without problems. (2) Copy the content (memory) of your struct to a SAFEARRAY (even to a BSTR, a bit ugly, but AFAIK, working) and pass it to the out-of process COM server that can claim the struct back from the memeory content of the SAFEARRY (or BSTR!). I think there are, as well, other solutions, but unfortunately, I can't remember, I cant't recall them... :)

            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.

            1 Reply Last reply
            0
            • G georgekjolly

              Hi Pallini, My code is given below /************Client Side****************************************/ typedef struct { int i; char ch[10]; }StrctMine; VARIANT vrt; StrctMine gOb; gOb.i = 10; strcpy(gOb.ch,"geo"); vrt.vt = VT_BYREF; vrt.byref = (void*)&gOb; myPtr.CreateInstance(__uuidof(MyVar)); myPtr->VarFun(&vrt); /*************************************************/ In Server side , I have written my structure in idl file, And I called a method , that is given below. STDMETHODIMP CMyVar::VarFun(VARIANT *vPtr_i) { // TODO: Add your implementation code here MessageBox(NULL,"Hai","",MB_OK); return S_OK; } But this code is not working. Thanks George

              L Offline
              L Offline
              Lim Bio Liong
              wrote on last edited by
              #6

              Hello georgekjolly, I've written some sample codes for you which demonstrates the use of SafeArrays and the IRecordInfo for passing a struct as a VARIANT parameter. Contact me via : bio_lim_2004@yahoo.com I'll email it to you. Best Regards, Bio.

              1 Reply Last reply
              0
              • G georgekjolly

                Hi Pallini, My code is given below /************Client Side****************************************/ typedef struct { int i; char ch[10]; }StrctMine; VARIANT vrt; StrctMine gOb; gOb.i = 10; strcpy(gOb.ch,"geo"); vrt.vt = VT_BYREF; vrt.byref = (void*)&gOb; myPtr.CreateInstance(__uuidof(MyVar)); myPtr->VarFun(&vrt); /*************************************************/ In Server side , I have written my structure in idl file, And I called a method , that is given below. STDMETHODIMP CMyVar::VarFun(VARIANT *vPtr_i) { // TODO: Add your implementation code here MessageBox(NULL,"Hai","",MB_OK); return S_OK; } But this code is not working. Thanks George

                L Offline
                L Offline
                Lim Bio Liong
                wrote on last edited by
                #7

                Hello George, Thanks, I've received your email this morning. I've tried twice to send you the sample codes as a zip file attachment but your email server kept messaging me of send failure. Do you have another email address that I can send to ? Best Regards, Bio.

                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