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 / ns / nhello / nhelloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-11  |  5.3 KB  |  178 lines

  1. /****************************************************************************
  2.                    Microsoft RPC Version 2.0
  3.            Copyright Microsoft Corp. 1992, 1993, 1994- 1996
  4.                        nhello Example
  5.  
  6.     FILE:       nhelloc.c
  7.  
  8.     USAGE:      nhelloc  -s string
  9.                          -n name_service_entry_name
  10.                          -t name_syntax_type
  11.  
  12.     PURPOSE:    Client side of RPC distributed application
  13.  
  14.     FUNCTIONS:  main() - binds to server and calls remote procedure
  15.  
  16.     COMMENTS:
  17.  
  18. ****************************************************************************/
  19.  
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include "nhello.h"    // header file generated by MIDL compiler
  24.  
  25. #define PURPOSE \
  26. "This Microsoft RPC Version 2.0 sample program demonstrates\n\
  27. the use of the name service. For more information about these\n\
  28. RPC API functions, see the RPC programming guide and reference.\n\n"
  29.  
  30. void Usage(char * pszProgramName)
  31. {
  32.     fprintf(stderr, "%s", PURPOSE);
  33.     fprintf(stderr, "Usage:  %s\n", pszProgramName);
  34.     fprintf(stderr, " -s string\n");
  35.     fprintf(stderr, " -n name_service_entry_name\n");
  36.     fprintf(stderr, " -t name_syntax_type\n");
  37.     exit(1);
  38. }
  39.  
  40. void _CRTAPI1 main(int argc, char **argv)
  41. {
  42.     unsigned char * pszString     = "hello, world";
  43.     unsigned char * pszEntryName  = "/.:/nhello_sample";
  44.     unsigned char * pszStrBinding = NULL;
  45.     RPC_NS_HANDLE hnsHello;
  46.     RPC_BINDING_HANDLE hHello;
  47.     unsigned long fNameSyntaxType = RPC_C_NS_SYNTAX_DEFAULT;
  48.     RPC_STATUS status;
  49.     unsigned long ulCode;
  50.     short fSuccess = 0;
  51.     short fContinue = 1;
  52.     short i;
  53.  
  54.     /* allow the user to override settings with command line switches */
  55.     for (i = 1; i < argc; i++) {
  56.         if ((*argv[i] == '-') || (*argv[i] == '/')) {
  57.             switch (tolower(*(argv[i]+1))) {
  58.             case 'n':
  59.                 pszEntryName = argv[++i];
  60.                 break;
  61.             case 't':
  62.                 fNameSyntaxType = (unsigned int) atoi(argv[++i]);
  63.                 break;
  64.             case 's':
  65.                 pszString = 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.  
  77.     RpcTryExcept {
  78.         status = RpcNsBindingImportBegin(fNameSyntaxType,
  79.                                          pszEntryName,
  80.                                          nhello_v1_0_c_ifspec,
  81.                                          NULL,
  82.                                          &hnsHello);
  83.         printf("RpcNsBindingImportBegin returned 0x%x\n", status);
  84.     }
  85.     RpcExcept(1) {
  86.         ulCode = RpcExceptionCode();
  87.         printf("RPC Runtime raised exception 0x%x\n", ulCode);
  88.         fContinue = 0;
  89.     }
  90.     RpcEndExcept
  91.  
  92.     if ( status != RPC_S_OK )
  93.         fContinue = 0;
  94.  
  95.     /* The loop is present because the name service may contain "stale" */
  96.     /* and unusable binding handlers.  This is part of the DCE design.  */
  97.     while( fContinue )
  98.     {
  99.         status = RpcNsBindingImportNext(hnsHello,
  100.                                         &hHello);
  101.         printf("RpcNsBindingImportNext returned 0x%x\n", status);
  102.  
  103.         if ( (status == RPC_S_NO_MORE_BINDINGS) ||
  104.              (status == RPC_S_NAME_SERVICE_UNAVAILABLE ) )
  105.             break;
  106.  
  107.         if (status != RPC_S_OK)
  108.             continue;
  109.  
  110.         RpcBindingToStringBinding( hHello, &pszStrBinding );
  111.         printf("StringBinding: %s\n", pszStrBinding );
  112.         RpcStringFree(&pszStrBinding);
  113.  
  114.         RpcTryExcept {
  115.             printf("Calling remote procedure HelloProc with string %s\n",
  116.                    pszString);
  117.             HelloProc(hHello, pszString);
  118.             fContinue = 0;
  119.             fSuccess = 1;
  120.         }
  121.         RpcExcept(1) {
  122.             ulCode = RpcExceptionCode();
  123.             printf("RPC Runtime raised exception 0x%x\n", ulCode);
  124.             status = RpcBindingFree(&hHello);
  125.             printf("RpcBindingFree returned 0x%x\n", status);
  126.             fContinue = 1;
  127.         }
  128.         RpcEndExcept
  129.  
  130.     }
  131.  
  132.     RpcTryExcept {
  133.         status = RpcNsBindingImportDone(&hnsHello);
  134.         printf("RpcNsBindingImportDone returned 0x%x\n", status);
  135.     }
  136.     RpcExcept(1) {
  137.         ulCode = RpcExceptionCode();
  138.         printf("RPC Runtime raised exception 0x%x\n", ulCode);
  139.         fContinue = 0;
  140.     }
  141.     RpcEndExcept
  142.  
  143.     if ( fSuccess )
  144.     {
  145.         RpcTryExcept {
  146.             Shutdown(hHello);  // Shutdown is a remote procedure
  147.         }
  148.         RpcExcept(1) {
  149.             ulCode = RpcExceptionCode();
  150.             printf("RPC runtime raised exception 0x%x\n", ulCode);
  151.         }
  152.         RpcEndExcept
  153.  
  154.         status = RpcBindingFree(&hHello);
  155.         printf("RpcBindingFree returned 0x%x\n", status);
  156.     }
  157.  
  158.     exit(0);
  159.  
  160. }  // end main()
  161.  
  162.  
  163. /*********************************************************************/
  164. /*                 MIDL allocate and free                            */
  165. /*********************************************************************/
  166.  
  167. void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
  168. {
  169.     return(malloc(len));
  170. }
  171.  
  172. void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
  173. {
  174.     free(ptr);
  175. }
  176.  
  177. /* end file nhelloc.c */
  178.