Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use structs/class in Dispatch interface methods

Status
Not open for further replies.

Kirilla

Programmer
Jul 12, 2000
101
HU
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top