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 / hello / helloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-11  |  5.1 KB  |  154 lines

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