Hi.
I've a DLL, what I want to implement to my application. I'd like to pass user defined structures and get it back. As I know, Automation doesn't support it only the MIDL base types. I did it, but I don't know how safe it is.
I made a simple struct to test it:
typdef struct MyStruct
{
CString sName;
} MyStruct;
Because Automation supports only the MIDL base types, I used the IUnknown interface to pass my structure. I had to use IUnknown** because of this is an indirectional parameter.
This is the interface declaration:
interface IDisp01 : IDispatch
{
[id(1), helpstring("method GetMyStruct"
] HRESULT
GetMyStruct([in, out] IUnknown** myStruct);
};
The CoCreate of this DLL:
...
hr=CoCreateInstance(clsid,pUnk,dwClsCtx,IID_IDisp01,(void**)&lpDisp);
iDisp=new IDisp01(lpDisp);
MyStruct* ss=new MyStruct();
ss->csName=_T("Something"
;
iDisp->GetMyStruct((LPUNKNOWN*)ss);
...
I made a new MyStruct object and put the "Something" text into it and I cast it to IUnknown**. (It works!!!)
Here is the method inside the DLL:
STDMETHODIMP Disp01::GetMyStruct(IUnknown **myStruct)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
MyStruct* ss;
ss=(MyStruct*)&*myStruct;
ss->csName=_T("Everything"
;
myStruct=(LPUNKNOWN*)&ss;
return S_OK;
}
I got the IUnknow**, I cast it back to MyStruct, change the text inside it and cast it to IUnknown**, because it is an out parameter too. (it works again)
It seems like it works, but I'm not sure it is a safe solution when I create a struct, which contains vectors, classes and other things.
best regards,
Kiri
I've a DLL, what I want to implement to my application. I'd like to pass user defined structures and get it back. As I know, Automation doesn't support it only the MIDL base types. I did it, but I don't know how safe it is.
I made a simple struct to test it:
typdef struct MyStruct
{
CString sName;
} MyStruct;
Because Automation supports only the MIDL base types, I used the IUnknown interface to pass my structure. I had to use IUnknown** because of this is an indirectional parameter.
This is the interface declaration:
interface IDisp01 : IDispatch
{
[id(1), helpstring("method GetMyStruct"
GetMyStruct([in, out] IUnknown** myStruct);
};
The CoCreate of this DLL:
...
hr=CoCreateInstance(clsid,pUnk,dwClsCtx,IID_IDisp01,(void**)&lpDisp);
iDisp=new IDisp01(lpDisp);
MyStruct* ss=new MyStruct();
ss->csName=_T("Something"
iDisp->GetMyStruct((LPUNKNOWN*)ss);
...
I made a new MyStruct object and put the "Something" text into it and I cast it to IUnknown**. (It works!!!)
Here is the method inside the DLL:
STDMETHODIMP Disp01::GetMyStruct(IUnknown **myStruct)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
MyStruct* ss;
ss=(MyStruct*)&*myStruct;
ss->csName=_T("Everything"
myStruct=(LPUNKNOWN*)&ss;
return S_OK;
}
I got the IUnknow**, I cast it back to MyStruct, change the text inside it and cast it to IUnknown**, because it is an out parameter too. (it works again)
It seems like it works, but I'm not sure it is a safe solution when I create a struct, which contains vectors, classes and other things.
best regards,
Kiri