home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / rpc / data / xmit / xmitc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-11  |  6.8 KB  |  195 lines

  1. /****************************************************************************
  2.                    Microsoft RPC Version 2.0
  3.            Copyright Microsoft Corp. 1992, 1993, 1994- 1996
  4.                        xmit Example
  5.  
  6.     FILE:       xmitc.c
  7.  
  8.     USAGE:      xmitc  -n network_address
  9.                        -p protocol_sequence
  10.                        -e endpoint
  11.                        -o options
  12.                        -c count of elements in linked list
  13.                        -v value of first element in linked list
  14.                        -d delta between values in linked list
  15.  
  16.     PURPOSE:    Client side of RPC distributed application.
  17.                 This sample demonstrates the transmit_as example.
  18.                 A doubly-linked list is transmitted over the network
  19.                 as a sized array.
  20.  
  21.     RELATED:    xmits.c - server main
  22.                 xmitp.c - remote procedures
  23.                 xmitu.c - utility procedures
  24.  
  25.     FUNCTIONS:  main() - bind to server and call remote procedure
  26.  
  27.     COMMENTS:   This sample program generates a linked list to
  28.                 demonstrate how a list with aliasing can be transmitted
  29.                 using the transmit_as attribute as a sized array.
  30.                 The pointers are rebuilt on the server side.
  31.  
  32.                 The [transmit_as] attribute (used in the typedef of
  33.                 DOUBLE_LINK_TYPE in the file XMIT.IDL) requires the
  34.                 four user-supplied functions whose names start with
  35.                 the name of the presented type, DOUBLE_LINK_TYPE.
  36.  
  37.                 The [in, out] attributes applied to remote procedure
  38.                 parameters require the two user-supplied functions
  39.                 midl_user_allocate and midl_user_free.
  40.  
  41.                 The other functions are utilities that are used to
  42.                 build or display the data structures.
  43.  
  44.  
  45. ****************************************************************************/
  46.  
  47. #include <stdlib.h>
  48. #include <stdio.h>
  49. #include <ctype.h>
  50. #include "xmit.h"     // header file generated by MIDL compiler
  51. #include "xmitu.h"    // utility function prototypes
  52.  
  53. #define PURPOSE \
  54. "This Microsoft RPC Version 2.0 sample program demonstrates\n\
  55. the use of the [transmit_as] attribute. For more information\n\
  56. about the attributes and the RPC API functions, see the\n\
  57. RPC programming guide and reference.\n\n"
  58.  
  59. #define MAX_ELEMENTS 50
  60.  
  61. void Usage(char * pszProgramName)
  62. {
  63.     fprintf(stderr, "%s", PURPOSE);
  64.     fprintf(stderr, "Usage:  %s\n", pszProgramName);
  65.     fprintf(stderr, " -p protocol_sequence\n");
  66.     fprintf(stderr, " -n network_address\n");
  67.     fprintf(stderr, " -e endpoint\n");
  68.     fprintf(stderr, " -o options\n");
  69.     fprintf(stderr, " -c count_of_elements\n");
  70.     fprintf(stderr, " -v value\n");
  71.     fprintf(stderr, " -d delta\n");
  72.     exit(1);
  73. }
  74.  
  75. void _CRTAPI1 main(int argc, char **argv)
  76. {
  77.     RPC_STATUS status;
  78.     unsigned char * pszUuid             = NULL;
  79.     unsigned char * pszProtocolSequence = "ncacn_np";
  80.     unsigned char * pszNetworkAddress   = NULL;
  81.     unsigned char * pszEndpoint         = "\\pipe\\xmit";
  82.     unsigned char * pszOptions          = NULL;
  83.     unsigned char * pszStringBinding    = NULL;
  84.     int i;
  85.     int cElements = 10;
  86.     short sValue = 100;
  87.     short sDelta = 10;
  88.  
  89.     DOUBLE_LINK_TYPE *pFirst, *pCurrent;
  90.  
  91.     /* allow the user to override settings with command line switches */
  92.     for (i = 1; i < argc; i++) {
  93.         if ((*argv[i] == '-') || (*argv[i] == '/')) {
  94.             switch (tolower(*(argv[i]+1))) {
  95.             case 'p':  // protocol sequence
  96.                 pszProtocolSequence = argv[++i];
  97.                 break;
  98.             case 'n':  // network address
  99.                 pszNetworkAddress = argv[++i];
  100.                 break;
  101.             case 'e':
  102.                 pszEndpoint = argv[++i];
  103.                 break;
  104.             case 'o':
  105.                 pszOptions = argv[++i];
  106.                 break;
  107.             case 'c':
  108.                 cElements = atoi(argv[++i]);
  109.                 if (cElements > MAX_ELEMENTS)
  110.                     cElements = MAX_ELEMENTS;
  111.                 break;
  112.             case 'v':
  113.                 sValue = (short)atoi(argv[++i]);
  114.                 break;
  115.             case 'd':
  116.                 sDelta = (short)atoi(argv[++i]);
  117.                 break;
  118.             case 'h':
  119.             case '?':
  120.             default:
  121.                 Usage(argv[0]);
  122.             }
  123.         }
  124.         else
  125.             Usage(argv[0]);
  126.     }
  127.  
  128.     /* initialize a list with a number of elements */
  129.     pFirst = InsertNewNode(sValue, NULL);
  130.     pCurrent = pFirst;   // assign some values to the list nodes
  131.     sValue += sDelta;    // make them different values
  132.  
  133.     for (i = 1; i < cElements; i++) {
  134.         pCurrent = InsertNewNode(sValue, pCurrent);
  135.         sValue += sDelta;
  136.     }
  137.     ListWalkProc(pFirst);
  138.  
  139.     /* Use a convenience function to concatenate the elements of the string */
  140.     /* binding into the syntax needed by RpcBindingFromStringBinding.       */
  141.     status = RpcStringBindingCompose(pszUuid,
  142.                                      pszProtocolSequence,
  143.                                      pszNetworkAddress,
  144.                                      pszEndpoint,
  145.                                      pszOptions,
  146.                                      &pszStringBinding);
  147.     printf("RpcStringBindingCompose returned 0x%x\n", status);
  148.     printf("pszStringBinding = %s\n", pszStringBinding);
  149.     if (status) {
  150.         exit(status);
  151.     }
  152.  
  153.     /* Set the binding handle that will be used to bind to the server. */
  154.     status = RpcBindingFromStringBinding(pszStringBinding,
  155.                                          &hXmit);
  156.     printf("RpcBindingFromStringBinding returned 0x%x\n", status);
  157.     if (status) {
  158.         exit(status);
  159.     }
  160.  
  161.     RpcTryExcept {
  162.         printf("Calling the remote procedure 'ModifyListProc'\n");
  163.         ModifyListProc(pFirst);  // call the remote procedure
  164.  
  165.         printf("Calling the remote procedure 'Shutdown'\n");
  166.         Shutdown();  // shut down the server side
  167.     }
  168.     RpcExcept(1) {
  169.         printf("Runtime reported exception %ld\n", RpcExceptionCode() );
  170.     }
  171.     RpcEndExcept
  172.  
  173.     printf("After ModifyListProc, the list appears as follows:\n");
  174.     ListWalkProc(pFirst);  // call the utility that displays the list
  175.  
  176.     /* The calls to the remote procedures are complete.            */
  177.     /* Free the string and the binding handle using RPC API calls. */
  178.     status = RpcStringFree(&pszStringBinding);
  179.     printf("RpcStringFree returned 0x%x\n", status);
  180.     if (status) {
  181.         exit(status);
  182.     }
  183.  
  184.     status = RpcBindingFree(&hXmit);
  185.     printf("RpcBindingFree returned 0x%x\n", status);
  186.     if (status) {
  187.         exit(status);
  188.     }
  189.  
  190.     exit(0);
  191.  
  192. }  // end main()
  193.  
  194. /* end file xmitc.c */
  195.