Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.1 KiB

  1. //*************************************************************
  2. //
  3. // Copyright (c) Microsoft Corporation 1998
  4. // All rights reserved
  5. //
  6. // apis.cxx
  7. //
  8. //*************************************************************
  9. #include "appmgmt.hxx"
  10. handle_t ghRpc = 0;
  11. DWORD
  12. Bind()
  13. {
  14. SC_HANDLE hSC;
  15. SC_HANDLE hAppSvc;
  16. USHORT* pwszStringBinding;
  17. SERVICE_STATUS ServiceStatus;
  18. DWORD Status;
  19. DWORD Retries;
  20. DWORD MaxRetries;
  21. BOOL bServiceStarted;
  22. BOOL bStatus;
  23. if ( ghRpc )
  24. return ERROR_SUCCESS;
  25. hSC = 0;
  26. hAppSvc = 0;
  27. Status = ERROR_SUCCESS;
  28. hSC = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE );
  29. if ( hSC )
  30. hAppSvc = OpenService( hSC, L"appmgmt", SERVICE_QUERY_STATUS | SERVICE_START );
  31. if ( ! hAppSvc )
  32. {
  33. Status = GetLastError();
  34. goto BindEnd;
  35. }
  36. bServiceStarted = FALSE;
  37. Retries = 0;
  38. MaxRetries = MAX_SERVICE_START_WAIT_TIME / SERVICE_RETRY_INTERVAL;
  39. do
  40. {
  41. bStatus = QueryServiceStatus( hAppSvc, &ServiceStatus );
  42. if ( ! bStatus )
  43. {
  44. Status = GetLastError();
  45. goto BindEnd;
  46. }
  47. switch ( ServiceStatus.dwCurrentState )
  48. {
  49. case SERVICE_STOPPED :
  50. bStatus = StartService( hAppSvc, NULL, NULL );
  51. if ( ! bStatus )
  52. {
  53. Status = GetLastError();
  54. goto BindEnd;
  55. }
  56. break;
  57. case SERVICE_START_PENDING :
  58. DWORD dwNewMaxRetries;
  59. dwNewMaxRetries = ServiceStatus.dwWaitHint / SERVICE_RETRY_INTERVAL;
  60. if ( dwNewMaxRetries < MaxRetries )
  61. {
  62. MaxRetries = dwNewMaxRetries;
  63. }
  64. break;
  65. case SERVICE_STOP_PENDING :
  66. break;
  67. case SERVICE_RUNNING :
  68. bServiceStarted = TRUE;
  69. break;
  70. default :
  71. ASSERT(0);
  72. Status = ERROR_INVALID_SERVICE_CONTROL;
  73. goto BindEnd;
  74. }
  75. if ( bServiceStarted )
  76. break;
  77. Sleep( SERVICE_RETRY_INTERVAL );
  78. Retries++;
  79. } while ( Retries <= MaxRetries ) ;
  80. if ( ! bServiceStarted )
  81. {
  82. Status = ERROR_SERVICE_REQUEST_TIMEOUT;
  83. goto BindEnd;
  84. }
  85. Status = RpcStringBindingCompose(
  86. NULL,
  87. (PUSHORT)L"ncalrpc",
  88. NULL,
  89. (PUSHORT)L"appmgmt",
  90. NULL,
  91. &pwszStringBinding );
  92. if ( ERROR_SUCCESS == Status )
  93. {
  94. if ( ! ghRpc )
  95. {
  96. Status = RpcBindingFromStringBinding(
  97. pwszStringBinding,
  98. &ghRpc );
  99. }
  100. RpcStringFree( &pwszStringBinding );
  101. }
  102. BindEnd:
  103. if ( hAppSvc )
  104. CloseServiceHandle( hAppSvc );
  105. if ( hSC )
  106. CloseServiceHandle( hSC );
  107. return Status;
  108. }