USRDEFC.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:5k
源码类别:

Windows编程

开发平台:

Visual C++

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