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 / dynept / dyneptc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-11  |  4.9 KB  |  148 lines

  1. /****************************************************************************
  2.                    Microsoft RPC Version 2.0
  3.            Copyright Microsoft Corp. 1992, 1993, 1994- 1996
  4.                        Dynept Example
  5.  
  6.     FILE:       dyneptc.c
  7.  
  8.     USAGE:      dyneptc  -n network_address
  9.                          -p protocol_sequence
  10.                          -o options
  11.                          -s string_displayed_on_server
  12.  
  13.     PURPOSE:    Client side of RPC distributed application
  14.  
  15.     FUNCTIONS:  main() - binds to server and calls remote procedure
  16.  
  17.     COMMENTS:   This version of the distributed application that
  18.                 prints "What a dynamic, world" (or other string) on
  19.                 the server features a client that manages its connection
  20.                 to the server. It uses the binding handle dynept_IfHandle,
  21.                 defined in the file dynept.h.
  22.  
  23. ****************************************************************************/
  24.  
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <ctype.h>
  28. #include "dynept.h"    // header file generated by MIDL compiler
  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, " -o options\n");
  36.     fprintf(stderr, " -s string\n");
  37.     exit(1);
  38. }
  39.  
  40. void _CRTAPI1 main(int argc, char **argv)
  41. {
  42.     RPC_STATUS status;
  43.     unsigned char * pszUuid             = NULL;
  44.     unsigned char * pszProtocolSequence = "ncacn_np";
  45.     unsigned char * pszNetworkAddress   = NULL;
  46.     unsigned char * pszOptions          = NULL;
  47.     unsigned char * pszStringBinding    = NULL;
  48.     unsigned char * pszString           = "What a dynamic world";
  49.     unsigned long ulCode;
  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 'o':
  63.                 pszOptions = argv[++i];
  64.                 break;
  65.             case 's':
  66.                 pszString = argv[++i];
  67.                 break;
  68.             case 'h':
  69.             case '?':
  70.             default:
  71.                 Usage(argv[0]);
  72.             }
  73.         }
  74.         else
  75.             Usage(argv[0]);
  76.     }
  77.  
  78.     /* Use a convenience function to concatenate the elements of */
  79.     /* the string binding into the proper sequence.              */
  80.     status = RpcStringBindingCompose(pszUuid,
  81.                                      pszProtocolSequence,
  82.                                      pszNetworkAddress,
  83.                                      NULL,
  84.                                      pszOptions,
  85.                                      &pszStringBinding);
  86.     printf("RpcStringBindingCompose returned 0x%x\n", status);
  87.     printf("pszStringBinding = %s\n", pszStringBinding);
  88.     if (status) {
  89.         exit(status);
  90.     }
  91.  
  92.     /* Set the binding handle that will be used to bind to the server. */
  93.     status = RpcBindingFromStringBinding(pszStringBinding,
  94.                                          &dynept_IfHandle);
  95.     printf("RpcBindingFromStringBinding returned 0x%x\n", status);
  96.     if (status) {
  97.         exit(status);
  98.     }
  99.  
  100.     printf("Calling the remote procedure 'HelloProc'\n");
  101.     printf("Print the string '%s' on the server\n", pszString);
  102.  
  103.     RpcTryExcept {
  104.         HelloProc(pszString);  // make call with user message
  105.         printf("Calling the remote procedure 'Shutdown'\n");
  106.         Shutdown();  // shut down the server side
  107.     }
  108.     RpcExcept(1) {
  109.         ulCode = RpcExceptionCode();
  110.         printf("Runtime reported exception 0x%lx = %ld\n", ulCode, ulCode);
  111.     }
  112.     RpcEndExcept
  113.  
  114.     /*  The calls to the remote procedures are complete. */
  115.     /*  Free the string and the binding handle           */
  116.     status = RpcStringFree(&pszStringBinding);  // remote calls done; unbind
  117.     printf("RpcStringFree returned 0x%x\n", status);
  118.     if (status) {
  119.         exit(status);
  120.     }
  121.  
  122.     status = RpcBindingFree(&dynept_IfHandle);  // remote calls done; unbind
  123.     printf("RpcBindingFree returned 0x%x\n", status);
  124.     if (status) {
  125.         exit(status);
  126.     }
  127.  
  128.     exit(0);
  129.  
  130. }  // end main()
  131.  
  132.  
  133. /*********************************************************************/
  134. /*                 MIDL allocate and free                            */
  135. /*********************************************************************/
  136.  
  137. void  __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
  138. {
  139.     return(malloc(len));
  140. }
  141.  
  142. void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
  143. {
  144.     free(ptr);
  145. }
  146.  
  147. /* end file dyneptc.c */
  148.