How to pass a object of a .NET user defined type to a COM in VC++?
-
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
-
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
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.
-
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.
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.
-
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.
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 -
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 formatHi 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.
-
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.
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 -
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 -
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, 2005Hi Gizzo, Many Many Thanks!!! Object wasn't initialized properly!!!! Its working :). Great!!! Once again Thanks. Keep it up!!!! Diana.