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 / dunion / dunionc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-11  |  6.0 KB  |  192 lines

  1. /****************************************************************************
  2.                    Microsoft RPC Version 2.0
  3.            Copyright Microsoft Corp. 1992, 1993, 1994- 1996
  4.                     Discriminated Union Example
  5.  
  6.     FILE:       dunionc.c
  7.  
  8.     USAGE:      dunionc  -n network_address
  9.                          -p protocol_sequence
  10.                          -e endpoint
  11.                          -o options
  12.                          -d discriminant
  13.                          -v union_value
  14.  
  15.     PURPOSE:    Client side of RPC distributed application
  16.  
  17.     FUNCTIONS:  main() - binds to server and calls remote procedure
  18.  
  19.     COMMENTS:   This distributed application illustrates discriminated
  20.                 union.
  21.  
  22. ****************************************************************************/
  23.  
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #include "dunion.h"    // header file generated by MIDL compiler
  28.  
  29.  
  30. void Usage(char * pszProgramName)
  31. {
  32.     fprintf(stderr, "Usage:  %s\n", pszProgramName);
  33.     fprintf(stderr, " -p protocol_sequence\n");
  34.     fprintf(stderr, " -n network_address\n");
  35.     fprintf(stderr, " -e endpoint\n");
  36.     fprintf(stderr, " -o options\n");
  37.     fprintf(stderr, " -d discriminant\n");
  38.     fprintf(stderr, " -v union_value\n");
  39.     exit(1);
  40. }
  41.  
  42. void DisplayUnionValue(DISCRIM_UNION_PARAM_TYPE *up,
  43.                        short                    sDiscrim)
  44. {
  45.     printf("sDiscrim = %d, data = ", sDiscrim);
  46.     switch(sDiscrim) {
  47.     case 0:
  48.         printf("short: %d\n", up->sVal);
  49.         break;
  50.     case 1:
  51.         printf("float: %f\n", up->fVal);
  52.         break;
  53.     case 2:
  54.         printf("char: %c\n", up->chVal);
  55.         break;
  56.     default:
  57.         printf("invalid\n");
  58.         break;
  59.     }
  60. }
  61.  
  62. void _CRTAPI1 main(int argc, char **argv)
  63. {
  64.     RPC_STATUS status;
  65.     unsigned char * pszUuid             = NULL;
  66.     unsigned char * pszProtocolSequence = "ncacn_np";
  67.     unsigned char * pszNetworkAddress   = NULL;
  68.     unsigned char * pszEndpoint         = "\\pipe\\dunion";
  69.     unsigned char * pszOptions          = NULL;
  70.     unsigned char * pszStringBinding    = NULL;
  71.     int i;
  72.  
  73.     short sDiscrim = 0;
  74.     DISCRIM_UNION_PARAM_TYPE  up = {1};
  75.     DISCRIM_UNION_STRUCT_TYPE us = {0, 1};
  76.  
  77.     /* allow the user to override settings with command line switches */
  78.     for (i = 1; i < argc; i++) {
  79.         if ((*argv[i] == '-') || (*argv[i] == '/')) {
  80.             switch (tolower(*(argv[i]+1))) {
  81.             case 'p':  // protocol sequence
  82.                 pszProtocolSequence = argv[++i];
  83.                 break;
  84.             case 'n':  // network address
  85.                 pszNetworkAddress = argv[++i];
  86.                 break;
  87.             case 'e':
  88.                 pszEndpoint = argv[++i];
  89.                 break;
  90.             case 'o':
  91.                 pszOptions = argv[++i];
  92.                 break;
  93.             case 'd':
  94.                 sDiscrim = (short) atoi(argv[++i]);
  95.                 if ((sDiscrim > 3) || (sDiscrim < 0))
  96.                     sDiscrim = 0;
  97.                 us.sDiscrim = sDiscrim;
  98.                 break;
  99.             case 'v':
  100.                 switch(sDiscrim) {
  101.                 case 0:
  102.                     up.sVal =  (short) atoi(argv[++i]);
  103.                     us.u.sVal = up.sVal;
  104.                     break;
  105.                 case 1:
  106.                     up.fVal =  (float) atof(argv[++i]);
  107.                     us.u.fVal = up.fVal;
  108.                     break;
  109.                 case 2:
  110.                     up.chVal = *(argv[++i]);
  111.                     us.u.chVal = up.chVal;
  112.                     break;
  113.                 default:
  114.                     break;
  115.                 }
  116.                 break;
  117.             case 'h':
  118.             case '?':
  119.             default:
  120.                 Usage(argv[0]);
  121.             }
  122.         }
  123.         else
  124.             Usage(argv[0]);
  125.     }
  126.  
  127.     /* Use a convenience function to concatenate the elements of  */
  128.     /* the string binding into the proper sequence.               */
  129.     status = RpcStringBindingCompose(pszUuid,
  130.                                      pszProtocolSequence,
  131.                                      pszNetworkAddress,
  132.                                      pszEndpoint,
  133.                                      pszOptions,
  134.                                      &pszStringBinding);
  135.     printf("RpcStringBindingCompose returned 0x%x\n", status);
  136.     printf("pszStringBinding = %s\n", pszStringBinding);
  137.     if (status) {
  138.         exit(status);
  139.     }
  140.  
  141.     /* Set the binding handle that will be used to bind to the server. */
  142.     status = RpcBindingFromStringBinding(pszStringBinding,
  143.                                          &hDiscrim);
  144.     printf("RpcBindingFromStringBinding returned 0x%x\n", status);
  145.     if (status) {
  146.         exit(status);
  147.     }
  148.  
  149.     printf("Calling the remote procedure 'UnionParamProc'\n");
  150.  
  151.     RpcTryExcept {
  152.         DisplayUnionValue(&up, sDiscrim);  // display value before call
  153.         UnionParamProc(&up, sDiscrim);     // call the remote procedure
  154.  
  155.         UnionStructProc(&us);              // call the remote procedure
  156.         DisplayUnionValue(&up, sDiscrim);  // display value after call
  157.  
  158.         Shutdown();                    // Shut down the server
  159.     }
  160.     RpcExcept(1) {
  161.         printf("Runtime reported exception %ld\n", RpcExceptionCode() );
  162.     }
  163.     RpcEndExcept
  164.  
  165.     /*  The remote procedure call is complete.  Free the binding handle */
  166.     status = RpcBindingFree(&hDiscrim);
  167.     printf("RpcBindingFree returned 0x%x\n", status);
  168.     if (status) {
  169.         exit(status);
  170.     }
  171.  
  172.     exit(0);
  173.  
  174. }  // end main()
  175.  
  176.  
  177. /*********************************************************************/
  178. /*                 MIDL allocate and free                            */
  179. /*********************************************************************/
  180.  
  181. void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
  182. {
  183.     return(malloc(len));
  184. }
  185.  
  186. void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
  187. {
  188.     free(ptr);
  189. }
  190.  
  191. /* end file dunionc.c */
  192.