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 / handles / usrdef / usrdefc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-11  |  5.0 KB  |  155 lines

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