CXHNDLC.C
Upload User: bangxh
Upload Date: 2007-01-31
Package Size: 42235k
Code Size: 5k
Category:

Windows Develop

Development Platform:

Visual C++

  1. /****************************************************************************
  2.                    Microsoft RPC Version 2.0
  3.            Copyright Microsoft Corp. 1992, 1993, 1994- 1996
  4.                       cxhndl Example
  5.     FILE:       cxhndlc.c
  6.     USAGE:      cxhndlc  -n network_address
  7.                          -p protocol_sequence
  8.                          -e endpoint
  9.                          -o options
  10.                          -f filename
  11.     PURPOSE:    Client side of RPC distributed application
  12.     FUNCTIONS:  main() - binds to server and calls remote procedure
  13.     COMMENTS:   This distributed application uses a context handle.
  14. ****************************************************************************/
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include "cxhndl.h"    // header file generated by MIDL compiler
  18. #define PURPOSE 
  19. "This Microsoft RPC Version 2.0 sample program demonstratesn
  20. the use of the [context_handle] attribute. For more informationn
  21. about attributes and RPC API functions, see the RPC programmingn
  22. guide and reference.nn"
  23. void Usage(char * pszProgramName)
  24. {
  25.     fprintf(stderr, "%s", PURPOSE);
  26.     fprintf(stderr, "Usage:  %sn", pszProgramName);
  27.     fprintf(stderr, " -p protocol_sequencen");
  28.     fprintf(stderr, " -n network_addressn");
  29.     fprintf(stderr, " -e endpointn");
  30.     fprintf(stderr, " -o optionsn");
  31.     fprintf(stderr, " -f filenamen");
  32.     exit(1);
  33. }
  34. void _CRTAPI1 main(int argc, char **argv)
  35. {
  36.     RPC_STATUS status;
  37.     PCONTEXT_HANDLE_TYPE phContext = NULL;
  38.     unsigned char * pbBuf = NULL;
  39.     short cbRead;    // count of bytes read
  40.     unsigned char * pszUuid             = NULL;
  41.     unsigned char * pszProtocolSequence = "ncacn_np";
  42.     unsigned char * pszNetworkAddress   = NULL;
  43.     unsigned char * pszEndpoint         = "\pipe\cxhndl";
  44.     unsigned char * pszOptions          = NULL;
  45.     unsigned char * pszStringBinding    = NULL;
  46.     unsigned char * pszFileName         = "readme.txt";
  47.     int i;
  48.     /* allow the user to override settings with command line switches */
  49.     for (i = 1; i < argc; i++) {
  50.         if ((*argv[i] == '-') || (*argv[i] == '/')) {
  51.             switch (tolower(*(argv[i]+1))) {
  52.             case 'p':  // protocol sequence
  53.                 pszProtocolSequence = argv[++i];
  54.                 break;
  55.             case 'n':  // network address
  56.                 pszNetworkAddress = argv[++i];
  57.                 break;
  58.             case 'e':
  59.                 pszEndpoint = argv[++i];
  60.                 break;
  61.             case 'o':
  62.                 pszOptions = argv[++i];
  63.                 break;
  64.             case 'f':
  65.                 pszFileName = argv[++i];
  66.                 break;
  67.             case 'h':
  68.             case '?':
  69.             default:
  70.                 Usage(argv[0]);
  71.             }
  72.         }
  73.         else
  74.             Usage(argv[0]);
  75.     }
  76.     pbBuf = (unsigned char *)
  77.             midl_user_allocate(BUFSIZE * sizeof(unsigned char));
  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.                                      pszEndpoint,
  84.                                      pszOptions,
  85.                                      &pszStringBinding);
  86.     printf("RpcStringBindingCompose returned 0x%xn", status);
  87.     printf("pszStringBinding = %sn", pszStringBinding);
  88.     if (status) {
  89.         exit(status);
  90.     }
  91.     /* Set the binding handle that will be used to bind to the server. */
  92.     status = RpcBindingFromStringBinding(pszStringBinding,
  93.                                          &hStarter);
  94.     printf("RpcBindingFromStringBinding returned 0x%xn", status);
  95.     if (status) {
  96.         exit(status);
  97.     }
  98.     printf("Calling the remote procedure RemoteOpenn");
  99.     if (RemoteOpen(&phContext, pszFileName) < 0) {
  100.         printf("Unable to open %sn", pszFileName);
  101.         Shutdown();
  102.         exit(2);
  103.     }
  104.     /* Now the context handle also manages the binding. */
  105.     status = RpcBindingFree(&hStarter);
  106.     printf("RpcBindingFree returned 0x%xn", status);
  107.     if (status) {
  108.         exit(status);
  109.     }
  110.     /*  Free the string binding */
  111.     status = RpcStringFree(&pszStringBinding);
  112.     printf("RpcStringFree returned 0x%xn", status);
  113.     if (status) {
  114.         exit(status);
  115.     }
  116.     printf("Calling the remote procedure RemoteReadn");
  117.     while (RemoteRead(phContext, pbBuf, &cbRead) > 0) {
  118.         for (i = 0; i < cbRead; i++)
  119.             putchar(*(pbBuf+i));
  120.     }
  121.     printf("Calling the remote procedure RemoteClosen");
  122.     if (RemoteClose(&phContext) < 0 ) {
  123.         printf("Close failed on %sn", pszFileName);
  124.         exit(2);
  125.     }
  126.     exit(0);
  127. }  // end main()
  128. /*********************************************************************/
  129. /*                 MIDL allocate and free                            */
  130. /*********************************************************************/
  131. void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
  132. {
  133.     return(malloc(len));
  134. }
  135. void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
  136. {
  137.     free(ptr);
  138. }
  139. /* end cxhndlc.c */