Purpose of the Blog

Interesting articles about life of IT professionals in sydney and Technical articles in .NET and C++.

Monday, September 6, 2010

ATL/COM - Passing UDT in an IDL file.

There are two general scenerios that needs to be discussed.
1) The user wants to put an array field inside a structure, something like
struct DVDStructure
{
SAFEARRAY(long) *m_dvdID;
BSTR m_comment;
}

To put elements into this safearray, you need to create a safearray using the function

SAFEARRAY *dvdArr;

dvdArr = SafeArrayCreateVector(VT_INT, 0, len);
// iterate and put elements into this safearray.
SafeArrayPutElement()

To get the elements from this safearray,
SafeArrayGetLBound();

SafeArrayGetUBound();
//iterate from lbound to ubound and then get the element.
SafeArrayGetElement();

2) The user wants to pass the array of UDT from the IDL file.

HRESULT PassStructure([out, retval]SAFEARRAY(struct CdvdGenStruct) *vecObjects);

The structure needs to be defined in the library section of the idl file(if you donn't follow this you get element not found error at runtime) .
[uuid(give a guid here)]

struct CdvdGenStruct
{
long m_id;
BSTR m_comments;
};
 
HRESULT hr = GetRecordInfoFromGuids(LIBID,
1, 0, 0x409, __uuidof(CdvdGenStruct),
&pRecInfo);

// Create SafeArray
SafeArrayCreateEx(VT_RECORD, 1, rgbounds, pRecInfo);
hr = SafeArrayAccessData(CdvdGenStruct,(void **)&actualData);
// put the data inside this safearray
hr = SafeArrayUnaccessData(CdvdGenStruct);

Whereever you need to access the data , simply do
SafeArrayAccessData(*vecObjects,(void **)&pActualData);
and then use the pActualData.

In case , you have a UDT inside a UDT that needs to be passed through IDL file, then the inner UDT also needs to have a GUID of its own.

No comments:

Post a Comment