Array is a chunk of data block located in heap, or data area of the application. The starting address of array can be passed in functions as pointer. Programming languages like C/C++/VB uses pointer to pass arrays. Array can be passed to the functions located in same application code or to an external dynamic link library. Function of external DLL also maps to application address space so the array can be accessed with out any issue.

DCOM works in a different way. There COM client and server are not in same process space. Marshalling process copies array block while functions are executed and parameters are exchanged. Now Marshalling process of DCOM should know the size of the array block so that it can copy that amount of data to LPC or RPC buffer. IDL uses one parameter to indicate the array size and size_is(parameter) is used in the attribute of the array. Passing a raw block array in COMaNow this data block is located in same address space size of the

IDL size_is

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

};

Array in COM server

STDMETHODIMP CDaysArray::GetDaysString(long cDays, BSTR *pDays)
{

  unsigned short *days[5] = {
    L"Monday",
    L"Tuesday",
    L"Wednesday",
    L"Thursday",
    L"Friday"
  };
  long nLbound;
  BSTR strDay;
  for(nLbound = 0; nLbound < cDays ; nLbound++)
  {
    strDay = SysAllocString(days[nLbound]);
    pDays[nLbound] = strDay;
  }
  return S_OK;
}

Array in COM client

#import "CWeekDaysEnumXXX.dll"

int main(int argc, char* argv[])
{
  
  HRESULT hr;
  BSTR Days[5];
  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(5, Days);
  if(hr == S_OK)
  {
    for (int i = 0; i < 5; 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;
}

Array in COM VB client

Option Explicit
Option Base 0

Private Sub Form_Load()
  Dim DaysArray As DaysArray
  Dim days(0 To 4) As String
  Dim i As Integer

  Set DaysArray = New CWEEKDAYSENUMXXXLib.DaysArray
  DaysArray.GetDaysString 5, days(0)
  For i = 0 To 4
    MsgBox days(i)
  Next
  Set DaysArray = Nothing
End Sub

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.

#