存根會呼叫 type_to_xmit 函式,將應用程式呈現的類型轉換成傳輸的類型。 函式定義為:
void __RPC_USER <type>_to_xmit (
<type> __RPC_FAR *, <xmit_type> __RPC_FAR * __RPC_FAR *);
第一個參數是應用程式數據的指標。 第二個參數是由函式所設定,以指向傳輸的數據。 函式必須配置傳輸類型的記憶體。
在下列範例中,用戶端會呼叫具有 [in, out] 類型的遠端程式DOUBLE_LINK_TYPE。 用戶端存根會呼叫名為 DOUBLE_LINK_TYPE_to_xmit 的 type_to_xmit 函式,將雙鏈接清單數據轉換成重設大小的陣列。
函式會決定清單中的元素數目、配置夠大的陣列來保存這些元素,然後將清單元素複製到陣列中。 在函式傳回之前,第二個參數 ppArray,會設定為指向新配置的數據結構。
void __RPC_USER DOUBLE_LINK_TYPE_to_xmit (
DOUBLE_LINK_TYPE __RPC_FAR * pList,
DOUBLE_XMIT_TYPE __RPC_FAR * __RPC_FAR * ppArray)
{
short cCount = 0;
DOUBLE_LINK_TYPE * pHead = pList; // save pointer to start
DOUBLE_XMIT_TYPE * pArray;
/* count the number of elements to allocate memory */
for (; pList != NULL; pList = pList->pNext)
cCount++;
/* allocate the memory for the array */
pArray = (DOUBLE_XMIT_TYPE *) MIDL_user_allocate
(sizeof(DOUBLE_XMIT_TYPE) + (cCount * sizeof(short)));
pArray->sSize = cCount;
/* copy the linked list contents into the array */
cCount = 0;
for (i = 0, pList = pHead; pList != NULL; pList = pList->pNext)
pArray->asNumber[cCount++] = pList->sNumber;
/* return the address of the pointer to the array */
*ppArray = pArray;
}