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 / callback / callc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-11  |  5.2 KB  |  165 lines

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