Passing a structure through VARIANT from VC++ Client to COM Server [modified]
-
Hai all, Do anyone know , how to pass a user define structure from a VC++ Client to a COM Server, through VARIANT. Thanks, George.
-
Hai all, Do anyone know , how to pass a user define structure from a VC++ Client to a COM Server, through VARIANT. Thanks, George.
You don't need a
VARIANT
. Anyway, you can pass a pointer of the struct in thebyref
member of theVARIANT union
(of course theCOM
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.
-
You don't need a
VARIANT
. Anyway, you can pass a pointer of the struct in thebyref
member of theVARIANT union
(of course theCOM
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.
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
-
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
VT_BYREF
by itself isn't a legal type. If you must pass the struct in aVARIANT
, you'll need to serialize it into a packet of bytes (for example, XML) and pass that as aSAFEARRAY
ofVT_UI1
. There is alsoIRecordInfo
, 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.
-
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
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 otherCOM objects
without problems. (2) Copy the content (memory) of your struct to aSAFEARRAY
(even to aBSTR
, a bit ugly, but AFAIK, working) and pass it to the out-of processCOM server
that can claim the struct back from the memeory content of theSAFEARRY
(orBSTR
!). 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.
-
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
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.
-
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
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.