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 pass a object of a .NET user defined type to a COM in VC++?

How to pass a object of a .NET user defined type to a COM in VC++?

Scheduled Pinned Locked Moved COM
csharpc++comhelptutorial
8 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.
  • D Offline
    D Offline
    Diana Fernandez
    wrote on last edited by
    #1

    Hi Is it possible to pass the object (in C# which has got methods and member variables) to a COM in VC++? If so how to implement it? Please Help. Many Thanks Diana

    G 1 Reply Last reply
    0
    • D Diana Fernandez

      Hi Is it possible to pass the object (in C# which has got methods and member variables) to a COM in VC++? If so how to implement it? Please Help. Many Thanks Diana

      G Offline
      G Offline
      Gizzo
      wrote on last edited by
      #2

      Hi Diana I'm not sure if there is a way of doing what you want, probably yes. Maybe some COM guru over here could point it out. You'd have to manage interfaces like IMarshal, IStreamXXX, and similars to stream your object from one side to the other, but as I said I don't know too much about the subject. Anyway you could encapsulate your object into a COM interface (which provide access to members and methods), and then, pass the interface pointer to COM. You can ask in microsoft.public.win32.programmer.ole. I think you will get a better answer there. Best regards.

      D 1 Reply Last reply
      0
      • G Gizzo

        Hi Diana I'm not sure if there is a way of doing what you want, probably yes. Maybe some COM guru over here could point it out. You'd have to manage interfaces like IMarshal, IStreamXXX, and similars to stream your object from one side to the other, but as I said I don't know too much about the subject. Anyway you could encapsulate your object into a COM interface (which provide access to members and methods), and then, pass the interface pointer to COM. You can ask in microsoft.public.win32.programmer.ole. I think you will get a better answer there. Best regards.

        D Offline
        D Offline
        Diana Fernandez
        wrote on last edited by
        #3

        Hi I think I need to explain more. There is one class say Class1 and it is implementing an interface IClass1. Object of this class is communicating with a COM in VC++ by calling a method in a class from COM. Before coming out of this method it is required to call method in Class1. IClass1 pointer is available in COM through type library and object of the class is passed to COM as VARIANT. I have reached till this point and don't know how to proceed. Or is there any other way to implement it? Thanks in Advance Diana.

        G 1 Reply Last reply
        0
        • D Diana Fernandez

          Hi I think I need to explain more. There is one class say Class1 and it is implementing an interface IClass1. Object of this class is communicating with a COM in VC++ by calling a method in a class from COM. Before coming out of this method it is required to call method in Class1. IClass1 pointer is available in COM through type library and object of the class is passed to COM as VARIANT. I have reached till this point and don't know how to proceed. Or is there any other way to implement it? Thanks in Advance Diana.

          G Offline
          G Offline
          Gizzo
          wrote on last edited by
          #4

          Diana Fernandez wrote:

          IClass1 pointer is available in COM through type library and object of the class is passed to COM as VARIANT. I have reached till this point and don't know how to proceed. Or is there any other way to implement it?

          If I have undertood well you have a VARIANT that contains a IClass1 pointer...so you can VARIANT vtPointer; // <--- let's say this is the VARIANT if(vtPointer == VT_DISPATCH) // { IDispatch * pDisp = vtPointer.pdispVal; // I asume here that IClass1 is derived from IDispatch IClass1 * pClass1 = NULL; HRESULT hr = pDisp->QueryInterface(IID_IClass1, (void**)&pClass1); if(FAILED(hr)) { // ... manage error } // ... and here you should be able to use pClass1 to call IClass1 methods // and then...release the object when you finish pClass1->Release(); // .... } I hope it helps. Best regards. -- modified at 3:29 Friday 25th November, 2005 aaarrggh!!! I don't know how to insert tabs in the code!! sorry for the bad format

          D 1 Reply Last reply
          0
          • G Gizzo

            Diana Fernandez wrote:

            IClass1 pointer is available in COM through type library and object of the class is passed to COM as VARIANT. I have reached till this point and don't know how to proceed. Or is there any other way to implement it?

            If I have undertood well you have a VARIANT that contains a IClass1 pointer...so you can VARIANT vtPointer; // <--- let's say this is the VARIANT if(vtPointer == VT_DISPATCH) // { IDispatch * pDisp = vtPointer.pdispVal; // I asume here that IClass1 is derived from IDispatch IClass1 * pClass1 = NULL; HRESULT hr = pDisp->QueryInterface(IID_IClass1, (void**)&pClass1); if(FAILED(hr)) { // ... manage error } // ... and here you should be able to use pClass1 to call IClass1 methods // and then...release the object when you finish pClass1->Release(); // .... } I hope it helps. Best regards. -- modified at 3:29 Friday 25th November, 2005 aaarrggh!!! I don't know how to insert tabs in the code!! sorry for the bad format

            D Offline
            D Offline
            Diana Fernandez
            wrote on last edited by
            #5

            Hi Gizzo, Many Thanks for the prompt reply. Its working fine. I think the QueryInterface is giving me a new pointer to IClass1. Not the same which is there in the variant. Because while debugging its going into the methods of Class1, but all the other members of the Class1 remains undefined. Thanks & Regards, Diana.

            G 1 Reply Last reply
            0
            • D Diana Fernandez

              Hi Gizzo, Many Thanks for the prompt reply. Its working fine. I think the QueryInterface is giving me a new pointer to IClass1. Not the same which is there in the variant. Because while debugging its going into the methods of Class1, but all the other members of the Class1 remains undefined. Thanks & Regards, Diana.

              G Offline
              G Offline
              Gizzo
              wrote on last edited by
              #6

              Diana Fernandez wrote:

              I think the QueryInterface is giving me a new pointer to IClass1

              I don't think so. What QueryInterface does is basically a casting. An implementation example could be: HRESULT CClass1::QueryInterface(REFIID riid, void** ppvObject) { if(riid == IID_IUnknown) *ppvObject = reinterpret_cast<IUnknown*>(this); else if(riid == IID_IDispatch) *ppvObject = reinterpret_cast<IDispatch*>(this); else if(riid == IID_IClass1) *ppvObject = static_cast<IClass1*>(this); else { *ppvObject = NULL; return E_NOINTERFACE; } AddRef(); return S_OK; } So, when you call QueryInterface, the object returns a pointer to the requested interface, but the object is the same. You say that the members remains undefined. Well, maybe the object wasn't initialized properly, but is should be the same object. Regards. -- modified at 5:15 Friday 25th November, 2005

              J D 2 Replies Last reply
              0
              • G Gizzo

                Diana Fernandez wrote:

                I think the QueryInterface is giving me a new pointer to IClass1

                I don't think so. What QueryInterface does is basically a casting. An implementation example could be: HRESULT CClass1::QueryInterface(REFIID riid, void** ppvObject) { if(riid == IID_IUnknown) *ppvObject = reinterpret_cast<IUnknown*>(this); else if(riid == IID_IDispatch) *ppvObject = reinterpret_cast<IDispatch*>(this); else if(riid == IID_IClass1) *ppvObject = static_cast<IClass1*>(this); else { *ppvObject = NULL; return E_NOINTERFACE; } AddRef(); return S_OK; } So, when you call QueryInterface, the object returns a pointer to the requested interface, but the object is the same. You say that the members remains undefined. Well, maybe the object wasn't initialized properly, but is should be the same object. Regards. -- modified at 5:15 Friday 25th November, 2005

                J Offline
                J Offline
                Jorgen Sigvardsson
                wrote on last edited by
                #7

                That's not necessarily true. Tear-off interfaces are an example in which QI doesn't just cast. :) -- Pictures[^] from my Japan trip.

                1 Reply Last reply
                0
                • G Gizzo

                  Diana Fernandez wrote:

                  I think the QueryInterface is giving me a new pointer to IClass1

                  I don't think so. What QueryInterface does is basically a casting. An implementation example could be: HRESULT CClass1::QueryInterface(REFIID riid, void** ppvObject) { if(riid == IID_IUnknown) *ppvObject = reinterpret_cast<IUnknown*>(this); else if(riid == IID_IDispatch) *ppvObject = reinterpret_cast<IDispatch*>(this); else if(riid == IID_IClass1) *ppvObject = static_cast<IClass1*>(this); else { *ppvObject = NULL; return E_NOINTERFACE; } AddRef(); return S_OK; } So, when you call QueryInterface, the object returns a pointer to the requested interface, but the object is the same. You say that the members remains undefined. Well, maybe the object wasn't initialized properly, but is should be the same object. Regards. -- modified at 5:15 Friday 25th November, 2005

                  D Offline
                  D Offline
                  Diana Fernandez
                  wrote on last edited by
                  #8

                  Hi Gizzo, Many Many Thanks!!! Object wasn't initialized properly!!!! Its working :). Great!!! Once again Thanks. Keep it up!!!! Diana.

                  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