📄 service.cpp
字号:
_tprintf(TEXT("Unable to grant the necessary privileges to %s.\nInstallation failed. Error: %s.\n"), account, GetLastErrorText(szErr, 256)); fflush(stdout); return; } result = SetAccountRights(account, SE_BATCH_LOGON_NAME); if (result != ERROR_SUCCESS) { SetLastError(result); _tprintf(TEXT("Unable to grant the necessary privileges to %s.\nInstallation failed. Error: %s.\n"), account, GetLastErrorText(szErr, 256)); fflush(stdout); return; } result = SetAccountRights(account, SE_INCREASE_QUOTA_NAME); if (result != ERROR_SUCCESS) { SetLastError(result); _tprintf(TEXT("Unable to grant the necessary privileges to %s.\nInstallation failed. Error: %s.\n"), account, GetLastErrorText(szErr, 256)); fflush(stdout); return; } */ result = SetAccountRights(account, SE_TCB_NAME); if (result != ERROR_SUCCESS) { SetLastError(result); _tprintf(TEXT("Unable to grant the necessary privileges to %s.\nInstallation failed. Error: %s.\n"), account, GetLastErrorText(szErr, 256)); fflush(stdout); return; } /* result = SetAccountRights(account, SE_CHANGE_NOTIFY_NAME); if (result != ERROR_SUCCESS) { SetLastError(result); _tprintf(TEXT("Unable to grant the necessary privileges to %s.\nInstallation failed. Error: %s.\n"), account, GetLastErrorText(szErr, 256)); fflush(stdout); return; } */ } schSCManager = OpenSCManager( NULL, // machine (NULL == local) NULL, // database (NULL == default) SC_MANAGER_ALL_ACCESS // access required ); if ( schSCManager ) { DWORD type = SERVICE_WIN32_OWN_PROCESS; if (interact && account==NULL) type = type | SERVICE_INTERACTIVE_PROCESS; schService = CreateService( schSCManager, // SCManager database TEXT(SZSERVICENAME), // name of service TEXT(SZSERVICEDISPLAYNAME), // name to display SERVICE_ALL_ACCESS, // desired access type, SERVICE_AUTO_START, //SERVICE_ERROR_NORMAL, // error control type SERVICE_ERROR_IGNORE, szPath, // service's binary NULL, // no load ordering group NULL, // no tag identifier TEXT(SZDEPENDENCIES), // dependencies account, // LocalSystem account if account==NULL password); if ( schService ) { if (bSetupRestart) Setup_Service_restart( schService ); WriteMPDRegistry("mpdUserCapable", bMPDUserCapable ? "yes" : "no"); // Start the service if (StartService(schService, 0, NULL)) _tprintf(TEXT("%s installed.\n"), TEXT(SZSERVICEDISPLAYNAME) ); else _tprintf(TEXT("%s installed, but failed to start:\n%s.\n"), TEXT(SZSERVICEDISPLAYNAME), GetLastErrorText(szErr, 256) ); fflush(stdout); CloseServiceHandle(schService); } else { _tprintf(TEXT("CreateService failed:\n%s\n"), GetLastErrorText(szErr, 256)); fflush(stdout); } CloseServiceHandle(schSCManager); } else { _tprintf(TEXT("OpenSCManager failed:\n%s\n"), GetLastErrorText(szErr,256)); fflush(stdout); }}//// FUNCTION: CmdRemoveService(BOOL bErrorOnNotInstalled)//// PURPOSE: Stops and removes the service//// PARAMETERS:// none//// RETURN VALUE:// none//// COMMENTS://BOOL CmdRemoveService(BOOL bErrorOnNotInstalled){ BOOL bRetVal = FALSE; SC_HANDLE schService; SC_HANDLE schSCManager; schSCManager = OpenSCManager( NULL, // machine (NULL == local) NULL, // database (NULL == default) SC_MANAGER_ALL_ACCESS // access required ); if ( schSCManager ) { schService = OpenService(schSCManager, TEXT(SZSERVICENAME), SERVICE_ALL_ACCESS); if (schService) { // try to stop the service if ( ControlService( schService, SERVICE_CONTROL_STOP, &ssStatus ) ) { _tprintf(TEXT("Stopping %s."), TEXT(SZSERVICEDISPLAYNAME)); fflush(stdout); Sleep( 1000 ); while( QueryServiceStatus( schService, &ssStatus ) ) { if ( ssStatus.dwCurrentState == SERVICE_STOP_PENDING ) { _tprintf(TEXT(".")); fflush(stdout); Sleep( 250 ); } else break; } if ( ssStatus.dwCurrentState == SERVICE_STOPPED ) { _tprintf(TEXT("\n%s stopped.\n"), TEXT(SZSERVICEDISPLAYNAME) ); fflush(stdout); } else { _tprintf(TEXT("\n%s failed to stop.\n"), TEXT(SZSERVICEDISPLAYNAME) ); fflush(stdout); } } // Delete the registry entries for the service. RegDeleteKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\MPICH\\MPD"); // now remove the service if( DeleteService(schService) ) { _tprintf(TEXT("%s removed.\n"), TEXT(SZSERVICEDISPLAYNAME) ); fflush(stdout); bRetVal = TRUE; } else { _tprintf(TEXT("DeleteService failed:\n%s\n"), GetLastErrorText(szErr,256)); fflush(stdout); } CloseServiceHandle(schService); } else { if (bErrorOnNotInstalled) { _tprintf(TEXT("OpenService failed:\n%s\n"), GetLastErrorText(szErr,256)); fflush(stdout); } else { bRetVal = TRUE; } } CloseServiceHandle(schSCManager); } else { _tprintf(TEXT("OpenSCManager failed:\n%s\n"), GetLastErrorText(szErr,256)); fflush(stdout); } return bRetVal;}//// FUNCTION: CmdStopService()//// PURPOSE: Stops the service//// PARAMETERS:// none//// RETURN VALUE:// none//// COMMENTS://void CmdStopService(){ SC_HANDLE schService; SC_HANDLE schSCManager; schSCManager = OpenSCManager( NULL, // machine (NULL == local) NULL, // database (NULL == default) SC_MANAGER_ALL_ACCESS // access required ); if ( schSCManager ) { schService = OpenService(schSCManager, TEXT(SZSERVICENAME), SERVICE_ALL_ACCESS); if (schService) { // try to stop the service if ( ControlService( schService, SERVICE_CONTROL_STOP, &ssStatus ) ) { _tprintf(TEXT("Stopping %s."), TEXT(SZSERVICEDISPLAYNAME)); fflush(stdout); Sleep( 1000 ); while( QueryServiceStatus( schService, &ssStatus ) ) { if ( ssStatus.dwCurrentState == SERVICE_STOP_PENDING ) { _tprintf(TEXT(".")); fflush(stdout); Sleep( 250 ); } else break; } if ( ssStatus.dwCurrentState == SERVICE_STOPPED ) { _tprintf(TEXT("\n%s stopped.\n"), TEXT(SZSERVICEDISPLAYNAME) ); fflush(stdout); } else { _tprintf(TEXT("\n%s failed to stop.\n"), TEXT(SZSERVICEDISPLAYNAME) ); fflush(stdout); } } CloseServiceHandle(schService); } else { _tprintf(TEXT("OpenService failed:\n%s\n"), GetLastErrorText(szErr,256)); fflush(stdout); } CloseServiceHandle(schSCManager); } else { _tprintf(TEXT("OpenSCManager failed:\n%s\n"), GetLastErrorText(szErr,256)); fflush(stdout); }}//// FUNCTION: CmdStartService()//// PURPOSE: Starts the service//// PARAMETERS:// none//// RETURN VALUE:// none//// COMMENTS://void CmdStartService(){ SC_HANDLE schService; SC_HANDLE schSCManager; schSCManager = OpenSCManager( NULL, // machine (NULL == local) NULL, // database (NULL == default) SC_MANAGER_ALL_ACCESS // access required ); if ( schSCManager ) { schService = OpenService(schSCManager, TEXT(SZSERVICENAME), SERVICE_ALL_ACCESS); if ( schService ) { // Start the service if (StartService(schService, 0, NULL)) { _tprintf(TEXT("%s started.\n"), TEXT(SZSERVICEDISPLAYNAME) ); fflush(stdout); } else { _tprintf(TEXT("%s failed to start.\n%s.\n"), TEXT(SZSERVICEDISPLAYNAME), GetLastErrorText(szErr, 256) ); fflush(stdout); } CloseServiceHandle(schService); } else { _tprintf(TEXT("OpenService failed:\n%s\n"), GetLastErrorText(szErr,256)); fflush(stdout); } CloseServiceHandle(schSCManager); } else { _tprintf(TEXT("OpenSCManager failed:\n%s\n"), GetLastErrorText(szErr,256)); fflush(stdout); }}//// FUNCTION: GetLastErrorText//// PURPOSE: copies error message text to string//// PARAMETERS:// lpszBuf - destination buffer// dwSize - size of buffer//// RETURN VALUE:// destination buffer//// COMMENTS://LPTSTR GetLastErrorText( LPTSTR lpszBuf, DWORD dwSize ){ DWORD dwRet; LPTSTR lpszTemp = NULL; dwRet = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ARGUMENT_ARRAY, NULL, GetLastError(), LANG_NEUTRAL, (LPTSTR)&lpszTemp, 0, NULL ); // supplied buffer is not long enough if ( !dwRet || ( (long)dwSize < (long)dwRet+14 ) ) lpszBuf[0] = TEXT('\0'); else { lpszTemp[lstrlen(lpszTemp)-2] = TEXT('\0'); //remove cr and newline character _stprintf( lpszBuf, TEXT("%s (error %d)"), lpszTemp, GetLastError() ); } if ( lpszTemp ) LocalFree((HLOCAL) lpszTemp ); return lpszBuf;}/////////////////////////////////////////////////////////////////////// The following code is for running the service as a console app////// FUNCTION: ControlHandler ( DWORD dwCtrlType )//// PURPOSE: Handled console control events//// PARAMETERS:// dwCtrlType - type of control event//// RETURN VALUE:// True - handled// False - unhandled//// COMMENTS://BOOL g_bHandlerCalled = FALSE;BOOL WINAPI ControlHandler ( DWORD dwCtrlType ){ switch( dwCtrlType ) { case CTRL_BREAK_EVENT: // use Ctrl+C or Ctrl+Break to simulate case CTRL_C_EVENT: // SERVICE_CONTROL_STOP in debug mode if (g_bHandlerCalled == TRUE) { printf("ControlHandler: Exiting.\n");fflush(stdout); ExitProcess(0); } _tprintf(TEXT("Stopping %s.\n"), TEXT(SZSERVICEDISPLAYNAME)); fflush(stdout); ServiceStop(); g_bHandlerCalled = TRUE; return TRUE; break; } return FALSE;}//// FUNCTION: CmdDebugService(int argc, char ** argv)//// PURPOSE: Runs the service as a console application//// PARAMETERS:// argc - number of command line arguments// argv - array of command line arguments//// RETURN VALUE:// none//// COMMENTS://void CmdDebugService(int argc, char ** argv){ DWORD dwArgc; LPTSTR *lpszArgv; #ifdef UNICODE lpszArgv = CommandLineToArgvW(GetCommandLineW(), &(dwArgc) );#else dwArgc = (DWORD) argc; lpszArgv = argv;#endif _tprintf(TEXT("Starting %s.\n"), TEXT(SZSERVICEDISPLAYNAME)); fflush(stdout); bDebug = TRUE; SetConsoleCtrlHandler( ControlHandler, TRUE ); ServiceStart( dwArgc, lpszArgv );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -