Click here to Skip to main content
15,896,606 members

Response to: CComSafeArray Class for my Interface, structures

Revision 1
Greetings!
First of all it is nessesary to get an info from type library using IRecordInfo interface:
C++
GUID GUID_SMyObj2 = __uuidof(SMyObj2);
HRESULT hr;
CComPtr<irecordinfo> srecnfo;
hr = GetRecordInfoFromGuids(LIBID_MyLib /*here is your type lib identifier*/, 1, 0, 0, GUID_SMyObj2, &srecnfo);
if(FAILED(hr))
  _com_issue_errorex(hr, NULL, IID_NULL);</irecordinfo>

If this part will be successful, then it is possible to create a safearray or custom type:
C++
const LONG l_bound = 0, u_bound = 2;
LPSAFEARRAY lpSMyObj2 = SafeArrayCreateVectorEx(VT_RECORD, l_bound,
  u_bound - l_bound + 1, srecnfo);
if(!lpSMyObj2)
  _com_issue_errorex(E_FAIL, NULL, IID_NULL);

or access and use existing:
C++
//LPSAFEARRAY lpSMyObj2 got from somewhere
//getting bounds of array
  LONG l_bound = 0, u_bound = 0;
  HRESULT hr1 = SafeArrayGetLBound(lpSMyObj2, 1, &l_bound);
  HRESULT hr2 = SafeArrayGetUBound(lpSMyObj2, 1, &u_bound);
  if(FAILED(hr1) || FAILED(hr2) )
  {
      // handle an error
  }
  else
  { 
     ...
     SMyObj2 * pSMyObj2 = NULL;
     HRESULT hr = SafeArrayAccessData(lpSMyObj2, (void**)&pSMyObj2);
     if(FAILED(hr))
       _com_issue_errorex(hr, NULL, IID_NULL);
     ...
  }
  // modifying data if needed
  for(int i = l_bound; i <= u_bound - l_bound; i++)
  {
    // do something with pSMyObj2[i].iobject
  }
  ...
  // Releasing an array
  hr = SafeArrayUnaccessData(lpSMyObj2);
  if(FAILED(hr))
    _com_issue_errorex(hr, NULL, IID_NULL);

Please see http://msdn.microsoft.com/en-us/library/aa148975.aspx[^] for more details
Posted 18-Sep-12 23:30pm by skydger.
Tags: ,