--- mstools/samples/rpc/data/xmit/xmitc.c 2018/08/09 18:20:59 1.1 +++ mstools/samples/rpc/data/xmit/xmitc.c 2018/08/09 18:22:10 1.1.1.2 @@ -8,7 +8,6 @@ -p protocol_sequence -e endpoint -o options - -u uuid -c count of elements in linked list -v value of first element in linked list -d delta between values in linked list @@ -20,28 +19,19 @@ RELATED: xmits.c - server main xmitp.c - remote procedures + xmitu.c - utility procedures FUNCTIONS: main() - bind to server and call remote procedure - PDOUBLE_LINK_TYPE_to_xmit - convert list to array - PDOUBLE_LINK_TYPE_from_xmit - convert array to list - PDOUBLE_LINK_TYPE_free_inst - free linked list memory - PDOUBLE_LINK_TYPE_free_xmit - free array memory - MIDL_user_allocate - user-supplied memory allocator - MIDL_user_free - user-supplied routine to free memory - - ArrayWalkProc - utility to display the array - ListWalkProc - utility to display the linked list - InsertNewNode - utility to add a node to the list COMMENTS: This sample program generates a linked list to - demonstrate how the list can be transmitted over - the network more efficiently as a sized array. + demonstrate how a list with aliasing can be transmitted + using the transmit_as attribute as a sized array. The pointers are rebuilt on the server side. The [transmit_as] attribute (used in the typedef of - PDOUBLE_LINK_TYPE in the file XMIT.IDL) requires the + DOUBLE_LINK_TYPE in the file XMIT.IDL) requires the four user-supplied functions whose names start with - the name of the presented type, PDOUBLE_LINK_TYPE. + the name of the presented type, DOUBLE_LINK_TYPE. The [in, out] attributes applied to remote procedure parameters require the two user-supplied functions @@ -57,6 +47,21 @@ #include #include // RPC API functions, types #include "xmit.h" // header file generated by MIDL compiler +#include "xmitu.h" // utility function prototypes + +DOUBLE_LINK_TYPE * InsertNewNode(short sValue, DOUBLE_LINK_TYPE * pPrevious); + +void ArrayWalkProc(DOUBLE_XMIT_TYPE * pArray); +void ListWalkProc(DOUBLE_LINK_TYPE * pList); +void * MIDL_user_allocate(size_t len); +void MIDL_user_free(void * ptr); + +void DOUBLE_LINK_TYPE_free_inst (DOUBLE_LINK_TYPE * pList); +void DOUBLE_LINK_TYPE_free_xmit (DOUBLE_XMIT_TYPE * pSizedArray); +void DOUBLE_LINK_TYPE_to_xmit (DOUBLE_LINK_TYPE * pList, + DOUBLE_XMIT_TYPE ** ppArray); +void DOUBLE_LINK_TYPE_from_xmit (DOUBLE_XMIT_TYPE * pArray, + DOUBLE_LINK_TYPE * pDblLinkedList); #define PURPOSE \ "This Microsoft RPC Version 1.0 sample program demonstrates\n\ @@ -74,7 +79,6 @@ void Usage(char * pszProgramName) fprintf(stderr, " -n network_address\n"); fprintf(stderr, " -e endpoint\n"); fprintf(stderr, " -o options\n"); - fprintf(stderr, " -u uuid\n"); fprintf(stderr, " -c count_of_elements\n"); fprintf(stderr, " -v value\n"); fprintf(stderr, " -d delta\n"); @@ -82,122 +86,11 @@ void Usage(char * pszProgramName) exit(1); } -DOUBLE_LINK_TYPE * InsertNewNode(short sValue, DOUBLE_LINK_TYPE * pPrevious) -{ -DOUBLE_LINK_TYPE * pNew; - - pNew = (DOUBLE_LINK_TYPE *) malloc(sizeof(DOUBLE_LINK_TYPE)); - if (pNew != NULL) { - pNew->sNumber = sValue; /* insert b between a and c */ - pNew->pPrevious = pPrevious; /* prev(b) = a */ - if (pPrevious != NULL) { - pNew->pNext = pPrevious->pNext; /* next(b) = c */ - pPrevious->pNext = pNew; /* next(a) = b */ - if (pNew->pNext != NULL) - (pNew->pNext)->pPrevious = pNew; /* prev(c) = b */ - } - else - pNew->pNext = NULL; - } - return(pNew); -} - -void ArrayWalkProc(DOUBLE_XMIT_TYPE * pArray) -{ - int i; - printf("Display contents of transmitted array:\n"); - - for (i = 0; i < pArray->sSize; i++) - printf("pArray->asNumber[%d] = %d\n", i, pArray->asNumber[i]); -} - -void ListWalkProc(DOUBLE_LINK_TYPE * pList) -{ - printf("Display contents of doubly linked list:\n"); - while (pList != NULL) { - printf("pList @0x%x = %d, Next = 0x%x\n", pList, pList->sNumber, pList->pNext); - pList = pList->pNext; - } -} - -void * MIDL_user_allocate(size_t len) -{ - return(malloc(len)); -} - -void MIDL_user_free(void * ptr) -{ - free(ptr); -} - -/* free the doubly linked list */ -void DOUBLE_LINK_TYPE_free_inst (DOUBLE_LINK_TYPE * pList) -{ - while (pList->pNext != NULL) /* go to end of list */ - pList = pList->pNext; - for (pList = pList->pPrevious; pList != NULL; pList = pList->pPrevious) - free(pList->pNext); -} - -/* free the array structure */ -void DOUBLE_LINK_TYPE_free_xmit (DOUBLE_XMIT_TYPE * pSizedArray) -{ - free(pSizedArray); -} - - -/* convert from linked list to array */ -void DOUBLE_LINK_TYPE_to_xmit (DOUBLE_LINK_TYPE * pList, - DOUBLE_XMIT_TYPE ** 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 *) malloc(sizeof(DOUBLE_XMIT_TYPE) + (cCount * sizeof(short))); - pArray->sSize = cCount; - - /* copy the linked list contents into the array */ - cCount = 0; - for (pList = pHead; pList != NULL; pList = pList->pNext) - pArray->asNumber[cCount++] = pList->sNumber; - - /* return the address of the pointer to the array */ - *ppArray = pArray; -} - -/* convert from array to linked list */ -void DOUBLE_LINK_TYPE_from_xmit (DOUBLE_XMIT_TYPE * pArray, - DOUBLE_LINK_TYPE * pDblLinkedList) -{ - PDOUBLE_LINK_TYPE pCurrent, pNew; - - int i; - - pCurrent = pDblLinkedList; - pCurrent->sNumber = pArray->asNumber[0]; - - DOUBLE_LINK_TYPE_free_inst(pCurrent); - pCurrent->pNext = NULL; /* wipe out pointer to old list */ - - for (i = 1; i < pArray->sSize; i++) { - pNew = InsertNewNode(pArray->asNumber[i], pCurrent); - pCurrent = pNew; - } - return; -} - - /* main: establish the binding to the server, call the remote procedure */ -void main(int argc, char **argv) +void _CRTAPI1 main(int argc, char **argv) { - RPC_STATUS status; // returned by RPC API function - unsigned char * pszUuid = "12345678-1234-1234-1234-123456789ABC"; + RPC_STATUS status; // returned by RPC API function + unsigned char * pszUuid = NULL; unsigned char * pszProtocolSequence = "ncacn_np"; unsigned char * pszNetworkAddress = NULL; unsigned char * pszEndpoint = "\\pipe\\xmit"; @@ -208,8 +101,7 @@ void main(int argc, char **argv) short sValue = 100; short sDelta = 10; - DOUBLE_LINK_TYPE * pFirst; - DOUBLE_LINK_TYPE * pCurrent, * pNew; + PDOUBLE_LINK_TYPE pFirst, pCurrent; // allow the user to override settings with command line switches for (i = 1; i < argc; i++) { @@ -227,9 +119,6 @@ void main(int argc, char **argv) case 'o': pszOptions = argv[++i]; break; - case 'u': - pszUuid = argv[++i]; - break; case 'c': cElements = atoi(argv[++i]); if (cElements > MAX_ELEMENTS) @@ -257,11 +146,9 @@ void main(int argc, char **argv) sValue += sDelta; /* make them different values */ for (i = 1; i < cElements; i++) { - pNew = InsertNewNode(sValue, pCurrent); - pCurrent = pNew; + pCurrent = InsertNewNode(sValue, pCurrent); sValue += sDelta; } - printf("Client main: "); ListWalkProc(pFirst); /* Use a convenience function to concatenate the elements of the string */ @@ -276,7 +163,7 @@ void main(int argc, char **argv) printf("RpcStringBindingCompose returned 0x%x\n", status); printf("pszStringBinding = %s\n", pszStringBinding); if (status) - exit(2); + exit(status); /* Set the binding handle that will be used to bind to the server. */ @@ -284,7 +171,7 @@ void main(int argc, char **argv) &hXmit); printf("RpcBindingFromStringBinding returned 0x%x\n", status); if (status) - exit(2); + exit(status); printf("Calling the remote procedure 'ModifyListProc'\n"); @@ -302,13 +189,13 @@ void main(int argc, char **argv) status = RpcStringFree(&pszStringBinding); // remote calls done; unbind printf("RpcStringFree returned 0x%x\n", status); if (status) - exit(2); + exit(status); status = RpcBindingFree(&hXmit); // remote calls done; unbind printf("RpcBindingFree returned 0x%x\n", status); if (status) - exit(2); + exit(status); exit(0); /* successful completion */ } -/* end \xmitc.c */ +/* end xmitc.c */