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 / nhellos.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-11  |  7.4 KB  |  243 lines

  1. /****************************************************************************
  2.                    Microsoft RPC Version 2.0
  3.            Copyright Microsoft Corp. 1992, 1993, 1994- 1996
  4.                          nhello Example
  5.  
  6.     FILE:       nhellos.c
  7.  
  8.     USAGE:      nhellos
  9.                         -m maxcalls
  10.                         -n mincalls
  11.                         -f flag for RpcServerListen wait
  12.                         -a nhello_sample_nsi_entry_name
  13.                         -t name_syntax_type
  14.  
  15.     PURPOSE:    Server side of RPC distributed application nhello
  16.  
  17.     FUNCTIONS:  main() - registers server as RPC server
  18.  
  19.     COMMENTS:
  20.  
  21. ****************************************************************************/
  22. #include <windows.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include "nhello.h"   // header file generated by MIDL compiler
  27.  
  28. BOOL WINAPI ControlHandler ( DWORD dwCtrlType );
  29.  
  30. void Usage(char * pszProgramName)
  31. {
  32.     fprintf(stderr, "Usage:  %s\n", pszProgramName);
  33.     fprintf(stderr, " -m maxcalls\n");
  34.     fprintf(stderr, " -n mincalls\n");
  35.     fprintf(stderr, " -f flag for RpcServerListen wait\n");
  36.     fprintf(stderr, " -a nhello_sample_nsi_entry_name\n");
  37.     fprintf(stderr, " -t name_syntax_type\n");
  38.     exit(1);
  39. }
  40.  
  41. void _CRTAPI1 main(int argc, char * argv[])
  42. {
  43.     RPC_STATUS status;
  44.     RPC_BINDING_VECTOR * pBindingVector = NULL;
  45.     unsigned char * pszEntryName    = "/.:/nhello_sample";
  46.     unsigned char * pszSecurity     = NULL;
  47.     unsigned int    cMinCalls       = 1;
  48.     unsigned int    cMaxCalls       = 20;
  49.     unsigned int    fDontWait       = 0;
  50.     unsigned int    fNameSyntaxType = RPC_C_NS_SYNTAX_DEFAULT;
  51.     unsigned int    fRegistered     = 0;
  52.     unsigned int    fEndpoint       = 0;
  53.     unsigned int    fExported       = 0;
  54.     int i;
  55.  
  56.     /* allow the user to override settings with command line switches */
  57.     for (i = 1; i < argc; i++) {
  58.         if ((*argv[i] == '-') || (*argv[i] == '/')) {
  59.             switch (tolower(*(argv[i]+1))) {
  60.             case 'm':
  61.                 cMaxCalls = (unsigned int) atoi(argv[++i]);
  62.                 break;
  63.             case 'n':
  64.                 cMinCalls = (unsigned int) atoi(argv[++i]);
  65.                 break;
  66.             case 'f':
  67.                 fDontWait = (unsigned int) atoi(argv[++i]);
  68.                 break;
  69.             case 'a':
  70.                 pszEntryName = argv[++i];
  71.                 break;
  72.             case 't':
  73.                 fNameSyntaxType = (unsigned int) atoi(argv[++i]);
  74.                 break;
  75.             case 'h':
  76.             case '?':
  77.             default:
  78.                 Usage(argv[0]);
  79.             }
  80.         }
  81.         else
  82.             Usage(argv[0]);
  83.     }
  84.  
  85.     SetConsoleCtrlHandler( ControlHandler, TRUE );
  86.  
  87.     printf("CallingRpcServerUseAllProtseqs...\n");
  88.  
  89.     status = RpcServerUseAllProtseqs(cMaxCalls,     // max concurrent calls
  90.                                      pszSecurity);  // Security descriptor
  91.     printf("RpcServerUseAllProtseqs returned 0x%x\n", status);
  92.     if (status) {
  93.         goto cleanup;
  94.     }
  95.  
  96.     status = RpcServerRegisterIf(nhello_v1_0_s_ifspec, // interface to register
  97.                                  NULL,   // MgrTypeUuid
  98.                                  NULL);  // MgrEpv; null means use default
  99.     printf("RpcServerRegisterIf returned 0x%x\n", status);
  100.     if (status) {
  101.         goto cleanup;
  102.     }
  103.     else
  104.         fRegistered = 1;
  105.  
  106.     status = RpcServerInqBindings(&pBindingVector);
  107.     printf("RpcServerInqBindings returned 0x%x\n", status);
  108.     if (status) {
  109.         goto cleanup;
  110.     }
  111.  
  112.     status = RpcEpRegister(nhello_v1_0_s_ifspec,
  113.                            pBindingVector,
  114.                            NULL,
  115.                            "");
  116.     printf("RpcEpRegister returned 0x%x\n", status);
  117.     if (status) {
  118.         goto cleanup;
  119.     }
  120.     else
  121.         fEndpoint = 1;
  122.  
  123.     status = RpcNsBindingExport(fNameSyntaxType,  // name syntax type
  124.                                 pszEntryName,     // nsi entry name
  125.                                 nhello_v1_0_s_ifspec,
  126.                                 pBindingVector,   // set in previous call
  127.                                 NULL);            // UUID vector
  128.     printf("RpcNsBindingExport returned 0x%x\n", status);
  129.     if (status) {
  130.         goto cleanup;
  131.     }
  132.     else
  133.         fExported = 1;
  134.  
  135.     printf("Calling RpcServerListen\n");
  136.     status = RpcServerListen(cMinCalls,
  137.                              cMaxCalls,
  138.                              fDontWait);  // wait flag
  139.     printf("RpcServerListen returned: 0x%x\n", status);
  140.     if (status) {
  141.         goto cleanup;
  142.     }
  143.  
  144.     if (fDontWait) {
  145.         printf("Calling RpcMgmtWaitServerListen\n");
  146.         status = RpcMgmtWaitServerListen();  //  wait operation
  147.         printf("RpcMgmtWaitServerListen returned: 0x%x\n", status);
  148.     }
  149.  
  150.   cleanup:
  151.  
  152.     if ( fExported )
  153.     {
  154.         status = RpcNsBindingUnexport(RPC_C_NS_SYNTAX_DEFAULT,  // name syntax type
  155.                                        pszEntryName,            // nsi entry name
  156.                                        nhello_v1_0_s_ifspec,
  157.                                        NULL);                   // UUID vector
  158.         printf( "RpcNsBindingUnexport returned 0x%x\n", status);
  159.     }
  160.  
  161.  
  162.     if ( fEndpoint )
  163.     {
  164.         status = RpcEpUnregister(nhello_v1_0_s_ifspec,
  165.                                   pBindingVector,
  166.                                   NULL);
  167.         printf( "RpcEpUnregister returned 0x%x\n", status);
  168.     }
  169.  
  170.     if ( pBindingVector )
  171.     {
  172.         status = RpcBindingVectorFree(&pBindingVector);
  173.         printf( "RpcBindingVectorFree returned 0x%x\n", status);
  174.     }
  175.  
  176.     if ( fRegistered )
  177.     {
  178.         status = RpcServerUnregisterIf(nhello_v1_0_s_ifspec, // interface to register
  179.                                        NULL,   // MgrTypeUuid
  180.                                        1);     // wait for outstanding calls
  181.         printf( "RpcServerUnregisterIf returned 0x%x\n", status);
  182.     }
  183.  
  184.  
  185. }  // end main()
  186.  
  187.  
  188. //
  189. //  FUNCTION: ControlHandler ( DWORD dwCtrlType )
  190. //
  191. //  PURPOSE: Handled console control events
  192. //
  193. //  PARAMETERS:
  194. //    dwCtrlType - type of control event
  195. //
  196. //  RETURN VALUE:
  197. //    True - handled
  198. //    False - unhandled
  199. //
  200. //  COMMENTS:
  201. //
  202. BOOL WINAPI ControlHandler ( DWORD dwCtrlType )
  203. {
  204.     RPC_STATUS status;
  205.  
  206.     switch( dwCtrlType )
  207.     {
  208.         case CTRL_BREAK_EVENT:  // use Ctrl+C or Ctrl+Break to call shutdown
  209.         case CTRL_C_EVENT:
  210.  
  211.             printf("Calling RpcMgmtIsServerListening\n");
  212.             status = RpcMgmtIsServerListening(NULL);
  213.             printf("RpcMgmtIsServerListening returned: 0x%x\n", status);
  214.  
  215.             if ( status == RPC_S_OK )
  216.             {
  217.                 printf("Calling RpcMgmtStopServerListening\n");
  218.                 status = RpcMgmtStopServerListening(NULL);
  219.                 printf("RpcMgmtStopServerListening returned: 0x%x\n", status);
  220.             }
  221.             break;
  222.  
  223.     }
  224.     return FALSE;
  225. }
  226.  
  227.  
  228. /*********************************************************************/
  229. /*                 MIDL allocate and free                            */
  230. /*********************************************************************/
  231.  
  232. void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
  233. {
  234.     return(malloc(len));
  235. }
  236.  
  237. void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
  238. {
  239.     free(ptr);
  240. }
  241.  
  242. /* end file nhellos.c */
  243.