Fixed size array

C/C++ functions can pass array as pointer. There should be some common logic between client and server to access the elements based.

Either it would be a constant size array or the length is provided by Client to be used by server. Server should set the array size for Output or return variables and client should access based on that.

size_is() in IDL

DCOM client and servers are not in same process space. So it is very necessary to know the size of the array. Array of a known size is always handled with an extra variable and array size is calculated with size_is(size variable).

Client allocates the required length of buffer and sets the size variable. Marshalling process copies the size of the array to LPC buffer or network buffer based on the length given in size_is(variable).

size_is() interface example

Here is one example of fixed array returned by server.

[
  object,
  uuid(014EA17F-66F0-43A9-AC45-5796F0113291),
  dual,
  helpstring("IDaysArray Interface"),
  pointer_default(unique)
]
interface IDaysArray : IDispatch
{
  [id(1), helpstring("method GetDaysString")]
  HRESULT GetDaysString([in] long cDays, [out, size_is(cDays)] BSTR * pDays);
};

BSTR fixed array server

STDMETHODIMP CDaysArray::GetDaysString(long cDays, BSTR *pDays)
{
  unsigned short *days[7] = {
    L"Monday",
    L"Tuesday",
    L"Wednesday",
    L"Thursday",
    L"Friday",
    L"Saturday",
    L"Sunday"
  };
  long nLbound;
  BSTR strDay;
  if ((cDays <= 0) || (cDays > 7))
  {
    return E_INVALIDARG;
  }
  for(nLbound = 0; nLbound < cDays; nLbound++) {
    strDay = SysAllocString(days[nLbound]);
    pDays[nLbound] = strDay;
  }
  return S_OK;
}

BSTR fixed array client

#import "CWeekDaysEnumXXX.dll"

int main(int argc, char* argv[])
{
  HRESULT hr;
  BSTR Days[7];
  CWEEKDAYSENUMXXXLib::IDaysArrayPtr  DaysArrayPtr;
  CoInitialize(NULL);
  hr =DaysArrayPtr.CreateInstance("CWeekDaysEnumXXX.DaysArray");
  if(hr != S_OK)
  {
    wprintf(L"CreateInstance(\"CWeekDaysEnumXXX.DaysArray\") error %x\n",hr);
    CoUninitialize();
    return hr;
  }
  hr = DaysArrayPtr->GetDaysString(7, Days);
  if(hr == S_OK)
  {
    for (int i = 0; i < 7; i++)
    {
      wprintf(L"Day %d : %s \n", (i + 1), Days[i]);
      SysFreeString(Days[i]);
    }
  } else {
    wprintf(L"GetDaysString error %x\n",hr);
  }
  DaysArrayPtr.Release();
  CoUninitialize();
  return 0;
}

BSTR fixed array output

Day 1 : Monday
Day 2 : Tuesday
Day 3 : Wednesday
Day 4 : Thursday
Day 5 : Friday
Day 6 : Saturday
Day 7 : Sunday

About our authors: Team EQA

You have viewed 1 page out of 67. Your COM/DCOM learning is 0.00% complete. Login to check your learning progress.

#