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

Windows编程

开发平台:

Visual C++

  1. /****************************************************************************
  2.                    Microsoft RPC Version 2.0
  3.            Copyright Microsoft Corp. 1992, 1993, 1994- 1996
  4.                        nhello Example
  5.     FILE:       nsserv.c
  6.     USAGE:      nsserv
  7.                          -m maxcalls
  8.                          -n mincalls
  9.                          -f flag for RpcServerListen wait
  10.                          -a nhello_sample_nsi_entry_name
  11.                          -t name_syntax_type
  12.     PURPOSE:    Server side of RPC distributed application nhello
  13.     FUNCTIONS:  main() - registers server as RPC server
  14.     COMMENTS:
  15. ****************************************************************************/
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include <tchar.h>
  20. #include "nhello.h"   // header file generated by MIDL compiler
  21. #include "service.h"
  22. VOID ServiceStart (DWORD dwArgc, LPTSTR *lpszArgv)
  23. {
  24.     RPC_STATUS status;
  25.     RPC_STATUS temp_status;
  26.     RPC_BINDING_VECTOR * pBindingVector = NULL;
  27.     unsigned char * pszEntryName    = "/.:/nhello_sample";
  28.     unsigned char * pszSecurity     = NULL;
  29.     unsigned int    cMinCalls       = 1;
  30.     unsigned int    cMaxCalls       = 20;
  31.     unsigned int    fDontWait       = 0;
  32.     unsigned int    fNameSyntaxType = RPC_C_NS_SYNTAX_DEFAULT;
  33.     BOOL bRegistered = FALSE;
  34.     BOOL bEndpoint = FALSE;
  35.     BOOL bExported = FALSE;
  36.     BOOL bProto = FALSE;
  37.     DWORD i;
  38.     /* allow the user to override settings with command line switches */
  39.     for (i = 1; i < dwArgc; i++) {
  40.         if ((*(lpszArgv[i]) == '-') || (*(lpszArgv[i]) == '/')) {
  41.             switch (tolower(*((lpszArgv[i])+1))) {
  42.             case 'm':
  43.                 cMaxCalls = (unsigned int) atoi(lpszArgv[++i]);
  44.                 break;
  45.             case 'n':
  46.                 cMinCalls = (unsigned int) atoi(lpszArgv[++i]);
  47.                 break;
  48.             case 'f':
  49.                 fDontWait = (unsigned int) atoi(lpszArgv[++i]);
  50.                 break;
  51.             case 'a':
  52.                 pszEntryName = lpszArgv[++i];
  53.                 break;
  54.             case 't':
  55.                 fNameSyntaxType = (unsigned int) atoi(lpszArgv[++i]);
  56.                 break;
  57.             }
  58.         }
  59.     }
  60.     // Prepare to Use All Protseqs
  61.     if (!ReportStatusToSCMgr(
  62.         SERVICE_START_PENDING, // service state
  63.         NO_ERROR,              // exit code
  64.         30000))                // wait hint  - UseAll can take quite a while
  65.         goto cleanup;
  66.     printf("CallingRpcServerUseAllProtseqs...n");
  67.     status = RpcServerUseAllProtseqs( cMaxCalls, pszSecurity );
  68.     printf("RpcServerUseAllProtseqs returned 0x%xn", status);
  69.     if (status)
  70.         goto cleanup;
  71.     // Prepare to Register Interface
  72.     if (!ReportStatusToSCMgr(
  73.         SERVICE_START_PENDING, // service state
  74.         NO_ERROR,              // exit code
  75.         30000))                 // wait hint
  76.         goto cleanup;
  77.     status = RpcServerRegisterIf(nhello_v1_0_s_ifspec, // interface to register
  78.                                  NULL,   // MgrTypeUuid
  79.                                  NULL);  // MgrEpv; null means use default
  80.     printf( TEXT("RpcServerRegisterIf returned 0x%xn"), status);
  81.     if (status)
  82.         goto cleanup;
  83.     else
  84.         bRegistered = TRUE;
  85.     // Prepare to Inquire Bindings
  86.     if (!ReportStatusToSCMgr(
  87.         SERVICE_START_PENDING, // service state
  88.         NO_ERROR,              // exit code
  89.         30000))                // wait hint  - Inq can take quite a while
  90.         goto cleanup;
  91.     status = RpcServerInqBindings(&pBindingVector);
  92.     printf( TEXT("RpcServerInqBindings returned 0x%xn"), status);
  93.     if (status)
  94.         goto cleanup;
  95.     // Prepare to Register Endpoint
  96.     if (!ReportStatusToSCMgr(
  97.         SERVICE_START_PENDING, // service state
  98.         NO_ERROR,              // exit code
  99.         45000))                // wait hint  - Inq can take quite a while
  100.         goto cleanup;
  101.     status = RpcEpRegister(nhello_v1_0_s_ifspec,
  102.                            pBindingVector,
  103.                            NULL,
  104.                            NULL);
  105.     printf( TEXT("RpcEpRegister returned 0x%xn"), status);
  106.     if (status)
  107.         goto cleanup;
  108.     else
  109.         bEndpoint = TRUE;
  110.     // Prepare to Export to NameService
  111.     if (!ReportStatusToSCMgr(
  112.         SERVICE_START_PENDING, // service state
  113.         NO_ERROR,              // exit code
  114.         45000))                // wait hint
  115.         goto cleanup;
  116.     status = RpcNsBindingExport(fNameSyntaxType,  // name syntax type
  117.                                 pszEntryName,     // nsi entry name
  118.                                 nhello_v1_0_s_ifspec,
  119.                                 pBindingVector,   // set in previous call
  120.                                 NULL);            // UUID vector
  121.     printf( TEXT("RpcNsBindingExport returned 0x%xn"), status);
  122.     if (status)
  123.         goto cleanup;
  124.     else
  125.         bExported = TRUE;
  126.     // Prepare to start listening.  At this point the service is initialized
  127.     if (!ReportStatusToSCMgr(
  128.         SERVICE_RUNNING, // service state
  129.         NO_ERROR,        // exit code
  130.         0))              // wait hint
  131.         goto cleanup;
  132.     printf( TEXT("Calling RpcServerListenn"));
  133.     status = RpcServerListen(cMinCalls,
  134.                              cMaxCalls,
  135.                              fDontWait );  // wait flag
  136.     printf( TEXT("RpcServerListen returned: 0x%xn"), status);
  137.     if (status) {
  138.         goto cleanup;
  139.     }
  140.     if (fDontWait) {
  141.         printf("Calling RpcMgmtWaitServerListenn");
  142.         status = RpcMgmtWaitServerListen();  //  wait operation
  143.         printf("RpcMgmtWaitServerListen returned: 0x%xn", status);
  144.     }
  145.   cleanup:
  146.     if ( bExported )
  147.     {
  148.         ReportStatusToSCMgr(
  149.             SERVICE_STOP_PENDING,  // service state
  150.             NO_ERROR,              // exit code
  151.             3000);                 // wait hint
  152.         temp_status = RpcNsBindingUnexport(RPC_C_NS_SYNTAX_DEFAULT,  // name syntax type
  153.                                            pszEntryName,     // nsi entry name
  154.                                            nhello_v1_0_s_ifspec,
  155.                                            NULL);            // UUID vector
  156.         printf( TEXT("RpcNsBindingUnexport returned 0x%xn"), temp_status);
  157.     }
  158.     if ( bEndpoint )
  159.     {
  160.         ReportStatusToSCMgr(
  161.             SERVICE_STOP_PENDING,  // service state
  162.             NO_ERROR,              // exit code
  163.             3000);                 // wait hint
  164.         temp_status = RpcEpUnregister(nhello_v1_0_s_ifspec,
  165.                                       pBindingVector,
  166.                                       NULL);
  167.         printf( TEXT("RpcEpUnregister returned 0x%xn"), temp_status);
  168.     }
  169.     if ( pBindingVector )
  170.     {
  171.         ReportStatusToSCMgr(
  172.             SERVICE_STOP_PENDING,  // service state
  173.             NO_ERROR,              // exit code
  174.             3000);                 // wait hint
  175.         temp_status = RpcBindingVectorFree(&pBindingVector);
  176.         printf( TEXT("RpcBindingVectorFree returned 0x%xn"), temp_status);
  177.     }
  178.     if ( bRegistered )
  179.     {
  180.         ReportStatusToSCMgr(
  181.             SERVICE_STOP_PENDING,  // service state
  182.             NO_ERROR,              // exit code
  183.             3000);                 // wait hint
  184.         temp_status = RpcServerUnregisterIf(nhello_v1_0_s_ifspec, // interface to register
  185.                                      NULL,   // MgrTypeUuid
  186.                                      1);     // wait for outstanding calls
  187.         printf( TEXT("RpcServerUnregisterIf returned 0x%xn"), temp_status);
  188.     }
  189.     ReportStatusToSCMgr(
  190.         SERVICE_STOP_PENDING,  // service state
  191.         NO_ERROR,              // exit code
  192.         3000);                 // wait hint
  193. }
  194. void ServiceStop( )
  195. {
  196.     RPC_STATUS status;
  197.     ReportStatusToSCMgr(
  198.         SERVICE_STOP_PENDING,  // service state
  199.         NO_ERROR,              // exit code
  200.         3000);                 // wait hint
  201.     status = RpcMgmtIsServerListening( NULL );
  202.     printf( TEXT("RpcMgmtIsServerListening returned 0x%xn"), status);
  203.     if ( status == RPC_S_OK )
  204.         RpcMgmtStopServerListening( NULL );
  205. }
  206. /*********************************************************************/
  207. /*                 MIDL allocate and free                            */
  208. /*********************************************************************/
  209. void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
  210. {
  211.     return(malloc(len));
  212. }
  213. void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
  214. {
  215.     free(ptr);
  216. }
  217. /* end file nsserv.c */