static bool enableService(int enable) { SC_HANDLE svc, mgr; int flag; mgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (! mgr) { mprUserError("Can't open service manager"); return 0; } svc = OpenService(mgr, app->serviceName, SERVICE_ALL_ACCESS); if (svc == NULL) { if (enable) { mprUserError("Can't access service"); } CloseServiceHandle(mgr); return 0; } flag = (enable) ? SERVICE_AUTO_START : SERVICE_DISABLED; if (!ChangeServiceConfig(svc, SERVICE_NO_CHANGE, flag, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) { mprUserError("Can't change service: 0x%x == %d", GetLastError(), GetLastError()); CloseServiceHandle(svc); CloseServiceHandle(mgr); return 0; } CloseServiceHandle(svc); CloseServiceHandle(mgr); return 1; }
static BOOL enable_service(void){ SC_HANDLE scm; SC_HANDLE service; BOOL ret; if(!open_service_config(&scm,&service)) return FALSE; ret = ChangeServiceConfig(service, SERVICE_NO_CHANGE, SERVICE_AUTO_START, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL); if(!ret){ last_error = GetLastError(); } CloseServiceHandle(service); CloseServiceHandle(scm); return ret; }
static BOOL set_interactive(BOOL interactive){ SC_HANDLE scm; SC_HANDLE service; BOOL ret; if(!open_service_config(&scm,&service)) return FALSE; ret = ChangeServiceConfig(service, SERVICE_WIN32_OWN_PROCESS | ((interactive) ? SERVICE_INTERACTIVE_PROCESS : 0), SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL); if(!ret){ last_error = GetLastError(); } CloseServiceHandle(service); CloseServiceHandle(scm); return ret; }
static void set_service_start_type(SC_HANDLE hSCManager, DWORD start_type) { SC_HANDLE hService; printf("%s service: %-40s ... " ,start_type==SERVICE_DISABLED ? "Disabling" : "Enabling", TITLE); hService = OpenService(hSCManager, NAME, SERVICE_ALL_ACCESS); if(hService==NULL) { printf("\n!ERROR %d opening service: %s\n",GetLastError(),NAME); return; } if(!ChangeServiceConfig( hService, // handle to service SERVICE_NO_CHANGE, // type of service start_type, // when to start service SERVICE_NO_CHANGE, // severity if service fails to start NULL, // pointer to service binary file name NULL, // pointer to load ordering group name NULL, // pointer to variable to get tag identifier NULL, // pointer to array of dependency names NULL, // pointer to account name of service NULL, // pointer to password for service account NULL // pointer to display name )) printf("\n!ERROR %d changing service config for: %s\n",GetLastError(),NAME); else printf("Successful\n"); CloseServiceHandle(hService); }
/* * Class: sage_WindowsServiceControl * Method: setServiceUser0 * Signature: (JLjava/lang/String;Ljava/lang/String;)Z */ JNIEXPORT jboolean JNICALL Java_sage_WindowsServiceControl_setServiceUser0 (JNIEnv *env, jobject jo, jlong ptr, jstring juser, jstring jpass) { WinServiceInfo* svcInfo = (WinServiceInfo*) ptr; if (!svcInfo) return 0; const char* cuser = env->GetStringUTFChars(juser, NULL); const char* cpass = env->GetStringUTFChars(jpass, NULL); jboolean rv = ChangeServiceConfig(svcInfo->schService, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, cuser,/*acct*/ cpass/*passwd*/, "SageTV"); if (rv) { // Check to make sure this account has logon as service rights and if not, then grant them. // If we can't grant them, then popup a warning message about not running this as an administrator // Strip the .\ from the beginning of the username if it's there char* myUser = (char*)cuser; if (myUser[0] == '.' && myUser[1] == '\\') myUser = &(myUser[2]); if (!CheckSingleUserPriv(myUser, L"SeServiceLogonRight", NULL)) { if (!SetPrivilegeOnAccount(myUser, L"SeServiceLogonRight", TRUE)) { // Failure....but there shouldn't be one since we're logged on as administrator or we wouldn't // have gotten here. } } } env->ReleaseStringUTFChars(juser, cuser); env->ReleaseStringUTFChars(jpass, cpass); return rv; }
void enableService(const char* szName) { gcWString wName(szName); SC_HANDLE scm, Service; //open connection to SCM scm = OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT); if (!scm) throw gcException(ERR_NULLSCMANAGER, GetLastError(), "Failed to open the Service Control Manager"); //open service Service = OpenService(scm, wName.c_str(), SERVICE_CHANGE_CONFIG|SERVICE_QUERY_STATUS); if (!Service) { CloseServiceHandle(scm); throw gcException(ERR_NULLSERVICE, GetLastError(), gcString("Failed to open service: {0}", szName)); } BOOL res = ChangeServiceConfig(Service, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, SERVICE_NO_CHANGE, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr); CloseServiceHandle(scm); CloseServiceHandle(Service); if (!res) throw gcException(ERR_SERVICE, GetLastError(), gcString("Failed to to renable service: {0}", szName)); }
HRESULT CHTFileTransferSystemServiceModule::RegisterAppId(bool bService) throw() { HRESULT hr = S_OK; BOOL res = CAtlServiceModuleT<CHTFileTransferSystemServiceModule, IDS_SERVICENAME>::RegisterAppId(bService); if(bService) { if(IsInstalled()) { SC_HANDLE hSCM = ::OpenSCManagerW(NULL, NULL, SERVICE_CHANGE_CONFIG); SC_HANDLE hService = NULL; if (hSCM == NULL) hr = AtlHresultFromLastError(); else { hService = ::OpenService(hSCM, m_szServiceName, SERVICE_CHANGE_CONFIG); if(hService != NULL) { const int m_szServiceNameLen = 4096; const int m_szServiceDescriptionLen = 2000; CAString strServiceDescription = L"HTFileTransferSystemService"; SERVICE_DESCRIPTION sdBuf = {strServiceDescription.GetBuffer(0)}; strServiceDescription.ReleaseBuffer(); res = ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &sdBuf); if (res) { res = ChangeServiceConfig( hService, // handle of service SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, // service type: no change SERVICE_AUTO_START, // service start type SERVICE_NO_CHANGE, // error control: no change NULL, // binary path: no change NULL, // load order group: no change NULL, // tag ID: no change NULL, // dependencies: no change NULL, // account name: no change NULL, // password: no change NULL); if (res != 0) { hr = AtlHresultFromLastError(); } } else { hr = AtlHresultFromLastError(); } ::CloseServiceHandle(hService); } else { hr = AtlHresultFromLastError(); } ::CloseServiceHandle(hSCM); } } } return hr; }
/* * Class: sage_WindowsServiceControl * Method: setServiceAutostart0 * Signature: (JZ)Z */ JNIEXPORT jboolean JNICALL Java_sage_WindowsServiceControl_setServiceAutostart0 (JNIEnv *env, jobject jo, jlong svcPtr, jboolean autostart) { WinServiceInfo* svcInfo = (WinServiceInfo*) svcPtr; if (!svcInfo) return 0; return ChangeServiceConfig(svcInfo->schService, SERVICE_NO_CHANGE, autostart ? SERVICE_AUTO_START : SERVICE_DISABLED, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL,/*acct*/ NULL/*passwd*/, "SageTV"); }
bool ServiceManager::_ReconfigureService(SC_HANDLE hSCManager, const String &ServiceName) //---------------------------------------------------------------------------// // DESCRIPTION: // Updates an existing hMailServer service. //---------------------------------------------------------------------------// { // Retrieve the handle for the service. SC_HANDLE hService = OpenService( hSCManager, ServiceName, SERVICE_QUERY_CONFIG | SERVICE_CHANGE_CONFIG); SC_LOCK sclLock = LockServiceDatabase(hSCManager); if (sclLock == NULL) { ErrorManager::Instance()->ReportError(ErrorManager::Medium, 5056, "ServiceManager::_ReconfigureService", "Failed to obtain lock on service database."); CloseServiceHandle(hService); return false; } // Update the path to the executable String sPath; sPath = Application::GetExecutableName(); sPath += " RunAsService"; if (ChangeServiceConfig( hService, // handle of service SERVICE_NO_CHANGE , // service type: no change SERVICE_NO_CHANGE, // service start type no change SERVICE_NO_CHANGE, // error control: no change sPath, // binary path changed NULL, // load order group: no change NULL, // tag ID: no change NULL, // dependencies: no change NULL, // account name: no change NULL, // password: no change NULL) == 0) // display name: no change { String sErrorMessage; sErrorMessage.Format(_T("ChangeServiceConfig failed. (%d)"), GetLastError()); ErrorManager::Instance()->ReportError(ErrorManager::Medium, 5057, "ServiceManager::_ReconfigureService", sErrorMessage); return false; } // Unlock and release. CloseServiceHandle(hService); UnlockServiceDatabase(sclLock); CloseServiceHandle (hSCManager); return true; }
// // Purpose: // Enables the service. // // Parameters: // None // // Return value: // None // VOID __stdcall DoEnableSvc() { SC_HANDLE schSCManager; SC_HANDLE schService; // Get a handle to the SCM database. schSCManager = OpenSCManager( NULL, // local computer NULL, // ServicesActive database SC_MANAGER_ALL_ACCESS); // full access rights if (NULL == schSCManager) { printf("OpenSCManager failed (%d)\n", GetLastError()); return; } // Get a handle to the service. schService = OpenService( schSCManager, // SCM database szSvcName, // name of service SERVICE_CHANGE_CONFIG); // need change config access if (schService == NULL) { printf("OpenService failed (%d)\n", GetLastError()); CloseServiceHandle(schSCManager); return; } // Change the service start type. if (! ChangeServiceConfig( schService, // handle of service SERVICE_NO_CHANGE, // service type: no change SERVICE_DEMAND_START, // service start type SERVICE_NO_CHANGE, // error control: no change NULL, // binary path: no change NULL, // load order group: no change NULL, // tag ID: no change NULL, // dependencies: no change NULL, // account name: no change NULL, // password: no change NULL) ) // display name: no change { printf("ChangeServiceConfig failed (%d)\n", GetLastError()); } else printf("Service enabled successfully.\n"); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); }
static int native_set_dependonservice(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) { SC_HANDLE service_handle = (SC_HANDLE) param; if (! service_handle) return -1; /* Get existing group dependencies because we must set both types together. */ TCHAR *buffer; unsigned long buflen; if (get_service_dependencies(service_name, service_handle, &buffer, &buflen, DEPENDENCY_GROUPS)) return -1; if (! value || ! value->string || ! value->string[0]) { if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, buffer, 0, 0, 0)) { print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError())); if (buffer) HeapFree(GetProcessHeap(), 0, buffer); return -1; } if (buffer) HeapFree(GetProcessHeap(), 0, buffer); return 0; } unsigned long len = (unsigned long) _tcslen(value->string) + 1; TCHAR *unformatted = 0; unsigned long newlen; if (unformat_double_null(value->string, len, &unformatted, &newlen)) { if (buffer) HeapFree(GetProcessHeap(), 0, buffer); return -1; } TCHAR *dependencies; if (buflen > 2) { dependencies = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (newlen + buflen) * sizeof(TCHAR)); if (! dependencies) { print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("dependencies"), _T("native_set_dependonservice")); if (unformatted) HeapFree(GetProcessHeap(), 0, unformatted); if (buffer) HeapFree(GetProcessHeap(), 0, buffer); return -1; } memmove(dependencies, buffer, buflen * sizeof(TCHAR)); memmove(dependencies + buflen - 1, unformatted, newlen * sizeof(TCHAR)); } else dependencies = unformatted; int ret = 1; if (set_service_dependencies(service_name, service_handle, dependencies)) ret = -1; if (dependencies != unformatted) HeapFree(GetProcessHeap(), 0, dependencies); if (unformatted) HeapFree(GetProcessHeap(), 0, unformatted); if (buffer) HeapFree(GetProcessHeap(), 0, buffer); return ret; }
int native_set_startup(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) { SC_HANDLE service_handle = (SC_HANDLE) param; if (! service_handle) return -1; /* It makes no sense to try to reset the startup type. */ if (! value || ! value->string) { print_message(stderr, NSSM_MESSAGE_NO_DEFAULT_VALUE, name); return -1; } /* Map NSSM_STARTUP_* constant to Windows SERVICE_*_START constant. */ int service_startup = -1; int i; for (i = 0; startup_strings[i]; i++) { if (str_equiv(value->string, startup_strings[i])) { service_startup = i; break; } } if (service_startup < 0) { print_message(stderr, NSSM_MESSAGE_INVALID_SERVICE_STARTUP, value->string); for (i = 0; startup_strings[i]; i++) _ftprintf(stderr, _T("%s\n"), startup_strings[i]); return -1; } unsigned long startup; switch (service_startup) { case NSSM_STARTUP_MANUAL: startup = SERVICE_DEMAND_START; break; case NSSM_STARTUP_DISABLED: startup = SERVICE_DISABLED; break; default: startup = SERVICE_AUTO_START; } if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, startup, SERVICE_NO_CHANGE, 0, 0, 0, 0, 0, 0, 0)) { print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError())); return -1; } SERVICE_DELAYED_AUTO_START_INFO delayed; ZeroMemory(&delayed, sizeof(delayed)); if (service_startup == NSSM_STARTUP_DELAYED) delayed.fDelayedAutostart = 1; else delayed.fDelayedAutostart = 0; if (! ChangeServiceConfig2(service_handle, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, &delayed)) { unsigned long error = GetLastError(); /* Pre-Vista we expect to fail with ERROR_INVALID_LEVEL */ if (error != ERROR_INVALID_LEVEL) { log_event(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_SERVICE_CONFIG_DELAYED_AUTO_START_INFO_FAILED, service_name, error_string(error), 0); } } return 1; }
BOOL SetServiceConfig(LPQUERY_SERVICE_CONFIG pServiceConfig, LPTSTR lpServiceName, LPTSTR lpPassword) { SC_HANDLE hSCManager; SC_HANDLE hSc; SC_LOCK scLock; BOOL bRet = FALSE; hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_LOCK); if (hSCManager) { scLock = LockServiceDatabase(hSCManager); if (scLock) { hSc = OpenService(hSCManager, lpServiceName, SERVICE_CHANGE_CONFIG); if (hSc) { if (ChangeServiceConfig(hSc, pServiceConfig->dwServiceType, pServiceConfig->dwStartType, pServiceConfig->dwErrorControl, pServiceConfig->lpBinaryPathName, pServiceConfig->lpLoadOrderGroup, pServiceConfig->dwTagId ? &pServiceConfig->dwTagId : NULL, pServiceConfig->lpDependencies, pServiceConfig->lpServiceStartName, lpPassword, pServiceConfig->lpDisplayName)) { bRet = TRUE; } CloseServiceHandle(hSc); } UnlockServiceDatabase(scLock); } CloseServiceHandle(hSCManager); } if (!bRet) GetError(); return bRet; }
int _setServiceConfig(LPCTSTR lpServiceName, DWORD dwServiceType, DWORD dwStartType, LPCTSTR lpBinaryPathName, LPCTSTR lpServiceStartName, LPCTSTR lpPassword, LPCTSTR lpDisplayName){ int returnValue = 0; SC_HANDLE schSCManager; SC_HANDLE schService; schSCManager = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT); if(!schSCManager){ returnValue = GetLastError(); }else{ schService = OpenService( schSCManager, lpServiceName, SERVICE_CHANGE_CONFIG); if(!schService){ returnValue = GetLastError(); }else{ if(!ChangeServiceConfig(schService, dwServiceType, dwStartType, SERVICE_NO_CHANGE, NULL, //lpBinaryPathName NULL, //lpLoadOrderGroup NULL, //lpdwTagId NULL, //lpDependencies lpServiceStartName, lpPassword, lpDisplayName)){ returnValue = GetLastError(); } CloseServiceHandle(schService); } CloseServiceHandle(schSCManager); } return returnValue; }
void ChangeServiceLogon(char * username,char * passwd,char * host,char * service) { SC_HANDLE hManager; SC_HANDLE hService; int queryret,chret; QUERY_SERVICE_CONFIG * pServiceConfig; DWORD scLen = 0; DWORD scNeedLen; DWORD err; hManager = OpenActiveManager(host); hService = OpenNamedService(hManager,service); queryret = QueryServiceConfig(hService,pServiceConfig,scLen,&scNeedLen); err = GetLastError(); if (err != ERROR_INSUFFICIENT_BUFFER && queryret == 0 ){ fprintf(stderr,"QueryServiceConfig Error\n"); exit(EXIT_FAILURE); } pServiceConfig = malloc(scNeedLen); if(pServiceConfig == NULL){ memallocerr(); } scLen = scNeedLen; queryret = QueryServiceConfig(hService,pServiceConfig,scLen,&scNeedLen); /*prepare to call ChangeServiceConfig*/ chret = ChangeServiceConfig(hService, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, username, passwd, NULL); if (chret == 0){ err = GetLastError(); fprintf(stderr,"ChangeServiceConfig Windows error is: %d\n",err); exit(EXIT_FAILURE); } }
BOOL CNTScmService::ChangeConfig(DWORD dwServiceType, DWORD dwStartType, DWORD dwErrorControl, LPCTSTR lpBinaryPathName, LPCTSTR lpLoadOrderGroup, LPDWORD lpdwTagId, LPCTSTR lpDependencies, LPCTSTR lpServiceStartName, LPCTSTR lpPassword, LPCTSTR lpDisplayName) const { //Validate our parameters ATLASSUME(m_hService != NULL); return ChangeServiceConfig(m_hService, dwServiceType, dwStartType, dwErrorControl, lpBinaryPathName, lpLoadOrderGroup, lpdwTagId, lpDependencies, lpServiceStartName, lpPassword, lpDisplayName); }
/** * Creates the service. * * @returns 0 on success. * @returns -1 on failure. */ static int suplibOsUpdateService(void) { /* * Assume it didn't exist, so we'll create the service. */ SC_HANDLE hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG); DWORD LastError = GetLastError(); NOREF(LastError); AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed LastError=%Rwa\n", LastError)); if (hSMgr) { SC_HANDLE hService = OpenService(hSMgr, SERVICE_NAME, SERVICE_CHANGE_CONFIG); if (hService) { char szDriver[RTPATH_MAX]; int rc = RTPathExecDir(szDriver, sizeof(szDriver) - sizeof("\\VBoxDrv.sys")); if (RT_SUCCESS(rc)) { strcat(szDriver, "\\VBoxDrv.sys"); SC_LOCK hLock = LockServiceDatabase(hSMgr); if (ChangeServiceConfig(hService, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, szDriver, NULL, NULL, NULL, NULL, NULL, NULL)) { UnlockServiceDatabase(hLock); CloseServiceHandle(hService); CloseServiceHandle(hSMgr); return 0; } else { DWORD LastError = GetLastError(); NOREF(LastError); AssertMsgFailed(("ChangeServiceConfig failed LastError=%Rwa\n", LastError)); } } UnlockServiceDatabase(hLock); CloseServiceHandle(hService); } else { DWORD LastError = GetLastError(); NOREF(LastError); AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError)); } CloseServiceHandle(hSMgr); } return -1; }
void CBINDInstallDlg::UpdateService(CString StartName) { SC_HANDLE hSCManager; SC_HANDLE hService; if(m_toolsOnly) return; SetCurrent(IDS_OPEN_SCM); hSCManager= OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (!hSCManager) { MsgBox(IDS_ERR_OPEN_SCM, GetErrMessage()); return; } DWORD dwStart = SERVICE_DEMAND_START; if (m_autoStart) dwStart = SERVICE_AUTO_START; DWORD dwServiceType = SERVICE_WIN32_OWN_PROCESS; CString namedLoc; namedLoc.Format("%s\\bin\\named.exe", m_targetDir); SetCurrent(IDS_OPEN_SERVICE); hService = OpenService(hSCManager, BIND_SERVICE_NAME, SERVICE_CHANGE_CONFIG); if (!hService) { MsgBox(IDS_ERR_OPEN_SERVICE, GetErrMessage()); if (hSCManager) CloseServiceHandle(hSCManager); return; } else { if (ChangeServiceConfig(hService, dwServiceType, dwStart, SERVICE_ERROR_NORMAL, namedLoc, NULL, NULL, NULL, StartName, m_accountPassword, BIND_DISPLAY_NAME) != TRUE) { DWORD err = GetLastError(); MsgBox(IDS_ERR_UPDATE_SERVICE, GetErrMessage()); } } if (hService) CloseServiceHandle(hService); if (hSCManager) CloseServiceHandle(hSCManager); SetItemStatus(IDC_REG_SERVICE); }
TStatus CreateService() { CAutoSCHandle hSCManager = ::OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE); SW_WINBOOL_RET(hSCManager.IsValid()); CAutoSCHandle hService = ::OpenService(hSCManager, c_sServiceName, SERVICE_CHANGE_CONFIG); std::wstring sPath; GetPath(sPath, PATH_TYPE_EXE_NAME, SW_BIT_32); TChar buf[0x1000]; swprintf_s(buf, L"%s %s", sPath.c_str(), c_sArgService); if (hService.IsInvalid()) { hService = ::CreateService( hSCManager, c_sServiceName, c_sServiceName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, buf, NULL, NULL, NULL, NULL, NULL); SW_WINBOOL_RET(hService.IsValid()); } else { SW_WINBOOL_RET(ChangeServiceConfig( hService, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, buf, NULL, NULL, NULL, NULL, NULL, c_sServiceName)); } SW_RETURN_SUCCESS; }
bool CStartAggregate::CreateNtService(LPCWSTR szServiceName, LPCWSTR szPath) { BOOL bRet = FALSE; SC_HANDLE schSCManager = _OpenSCManager(); if ( schSCManager == NULL ) return false; SC_HANDLE schService = OpenService(schSCManager, szServiceName, SERVICE_ALL_ACCESS); if(schService) { _StopServer(schService); bRet = ChangeServiceConfig( schService,//服务控制管理器 数据库 SERVICE_WIN32_OWN_PROCESS,//服务类型 SERVICE_AUTO_START,//启动类型,随系统自动//加载 SERVICE_ERROR_NORMAL, szPath, NULL, NULL, TEXT(""), NULL,//本地系统帐号 NULL,// 没有口令 L""); } else { schService=CreateService( schSCManager,//服务控制管理器 数据库 szServiceName,//后台服务名称 szServiceName,//在服务控制面板中显示的//服务名称 SERVICE_ALL_ACCESS,//访问权限 SERVICE_WIN32_OWN_PROCESS,//服务类型 SERVICE_AUTO_START,//启动类型,随系统自动//加载 SERVICE_ERROR_NORMAL, szPath, NULL, NULL, TEXT(""), NULL,//本地系统帐号 NULL);// 没有口令 if(schService) bRet = TRUE; } CloseServiceHandle(schService); CloseServiceHandle(schSCManager); if(bRet) return true; return false; }
/** * Changes the USB driver service to specified driver path. * * @returns 0 on success. * @returns < 0 on failure. */ int usblibOsChangeService(const char *pszDriverPath) { SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG); DWORD dwLastError = GetLastError(); int rc = RTErrConvertFromWin32(dwLastError); AssertPtr(pszDriverPath); AssertMsg(hSMgrCreate, ("OpenSCManager(,,create) failed rc=%d\n", dwLastError)); if (hSMgrCreate) { SC_HANDLE hService = OpenService(hSMgrCreate, SERVICE_NAME, GENERIC_ALL); DWORD dwLastError = GetLastError(); if (hService == NULL) { AssertMsg(hService, ("OpenService failed! LastError=%Rwa, pszDriver=%s\n", dwLastError, pszDriverPath)); rc = RTErrConvertFromWin32(dwLastError); } else { /* We only gonna change the driver image path, the rest remains like it already is */ if (ChangeServiceConfig(hService, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, pszDriverPath, NULL, NULL, NULL, NULL, NULL, NULL)) { RTPrintf("Changed service config to new driver path: %s\n", pszDriverPath); } else { AssertMsg(hService, ("ChangeServiceConfig failed! LastError=%Rwa, pszDriver=%s\n", dwLastError, pszDriverPath)); rc = RTErrConvertFromWin32(dwLastError); } if (hService != NULL) CloseServiceHandle(hService); } CloseServiceHandle(hSMgrCreate); } return rc; }
void CService::start(void) { ChangeServiceConfig( schService, SERVICE_NO_CHANGE, SERVICE_AUTO_START, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL ); StartService(schService, 0, NULL); }
BOOL enzyme::kernel::Service::start(SC_HANDLE scm, const TCHAR* path) { mSCHandle = OpenService(scm, bsname, SERVICE_ALL_ACCESS); if(mSCHandle != NULL) ChangeServiceConfig(mSCHandle, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, path, NULL, NULL, NULL, NULL, NULL, NULL); else mSCHandle = CreateService(scm, bsname, bsname, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, path, NULL, NULL, NULL, NULL, NULL); if(mSCHandle == NULL) { CloseServiceHandle(scm); throw std::runtime_error("Kernel Driver: " + lasterror()); } return StartService(mSCHandle, 1, &bsname); }
int native_set_type(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) { SC_HANDLE service_handle = (SC_HANDLE) param; if (! service_handle) return -1; /* It makes no sense to try to reset the service type. */ if (! value || ! value->string) { print_message(stderr, NSSM_MESSAGE_NO_DEFAULT_VALUE, name); return -1; } /* We can only manage services of type SERVICE_WIN32_OWN_PROCESS and SERVICE_INTERACTIVE_PROCESS. */ unsigned long type = SERVICE_WIN32_OWN_PROCESS; if (str_equiv(value->string, NSSM_INTERACTIVE_PROCESS)) type |= SERVICE_INTERACTIVE_PROCESS; else if (! str_equiv(value->string, NSSM_WIN32_OWN_PROCESS)) { print_message(stderr, NSSM_MESSAGE_INVALID_SERVICE_TYPE, value->string); _ftprintf(stderr, _T("%s\n"), NSSM_WIN32_OWN_PROCESS); _ftprintf(stderr, _T("%s\n"), NSSM_INTERACTIVE_PROCESS); return -1; } /* ChangeServiceConfig() will fail if the service runs under an account other than LOCALSYSTEM and we try to make it interactive. */ if (type & SERVICE_INTERACTIVE_PROCESS) { QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle); if (! qsc) return -1; if (! str_equiv(qsc->lpServiceStartName, NSSM_LOCALSYSTEM_ACCOUNT)) { HeapFree(GetProcessHeap(), 0, qsc); print_message(stderr, NSSM_MESSAGE_INTERACTIVE_NOT_LOCALSYSTEM, value->string, service_name, NSSM_LOCALSYSTEM_ACCOUNT); return -1; } HeapFree(GetProcessHeap(), 0, qsc); } if (! ChangeServiceConfig(service_handle, type, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, 0, 0, 0, 0)) { print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError())); return -1; } return 1; }
void ServiceManager::MakeDependentOn(const String &ServiceName) //---------------------------------------------------------------------------// // DESCRIPTION: // Makes hMailServer dependent on RPCSS and ServiceName //---------------------------------------------------------------------------// { // Retrieve the handle for the local service manager. SC_HANDLE hSCManager; hSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CREATE_SERVICE | SC_MANAGER_LOCK); // Retrieve the handle for the service. SC_HANDLE hService = OpenService( hSCManager, _T("hMailServer"), SERVICE_CHANGE_CONFIG ); SC_LOCK sclLock = LockServiceDatabase(hSCManager); if (sclLock == NULL) { CloseServiceHandle(hService); return; } int iLength = ServiceName.GetLength() + 8; TCHAR * lpDependent = new TCHAR[iLength]; memset(lpDependent, 0, iLength * sizeof(TCHAR)); _tcscpy(lpDependent, _T("RPCSS")); _tcscpy(lpDependent+6, ServiceName); int iRet = ChangeServiceConfig( hService, // handle of service SERVICE_NO_CHANGE, // service type: no change SERVICE_NO_CHANGE, // service start type no change SERVICE_NO_CHANGE, // error control: no change NULL, // binary path changed NULL, // load order group: no change NULL, // tag ID: no change lpDependent, // dependencies: no change NULL, // account name: no change NULL, // password: no change NULL); // display name: no change CloseServiceHandle(hService); UnlockServiceDatabase(sclLock); delete [] lpDependent; }
int native_set_imagepath(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) { SC_HANDLE service_handle = (SC_HANDLE) param; if (! service_handle) return -1; /* It makes no sense to try to reset the image path. */ if (! value || ! value->string) { print_message(stderr, NSSM_MESSAGE_NO_DEFAULT_VALUE, name); return -1; } if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, value->string, 0, 0, 0, 0, 0, 0)) { print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError())); return -1; } return 1; }
DWORD CWin32Svr::ChangeConfig() { SC_HANDLE schSCManager = OpenSCManager( NULL, // machine (NULL == local) NULL, // database (NULL == default) SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE | SERVICE_CHANGE_CONFIG // access required ); if (schSCManager == NULL) return GetLastError(); SC_HANDLE schService = OpenService( schSCManager, m_szSvrName, SERVICE_ALL_ACCESS); if (schService == NULL) { DWORD dwErr = GetLastError(); CloseServiceHandle(schSCManager); return dwErr; } SERVICE_FAILURE_ACTIONS action = {0}; action.dwResetPeriod = 30; action.cActions = 3; action.lpsaActions = new SC_ACTION[action.cActions]; for (DWORD idx=0; idx<action.cActions; idx++) { action.lpsaActions[idx].Type = SC_ACTION_RESTART; action.lpsaActions[idx].Delay = 30 * 1000; } ChangeServiceConfig2(schService, SERVICE_CONFIG_FAILURE_ACTIONS, &action); //修改服务的配置。 ChangeServiceConfig(schService, SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, m_szSvrPath, NULL, NULL, NULL, NULL, NULL, m_szSvrName); // //保证服务运行起来。 // StartService(schService, 0, NULL); delete []action.lpsaActions; CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return ERROR_SUCCESS; }
BOOL CWin32Svr::CheckService(LPCTSTR lpszSvrName, LPCTSTR lpszSvrDispName, LPCTSTR lpszSvrExePath) { if (lpszSvrName == NULL || lpszSvrDispName == NULL) { return FALSE; } TCHAR szExePath[512] = {0}; if (lpszSvrExePath == NULL) { if (0 == GetModuleFileName(NULL, szExePath, _countof(szExePath) )) return FALSE; lpszSvrExePath = szExePath; } SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE | SERVICE_CHANGE_CONFIG); if (schSCManager == NULL) { return FALSE; } //如果没有安装服务,则安装服务 SC_HANDLE schService = OpenService(schSCManager, lpszSvrName, SERVICE_ALL_ACCESS); if (schService == NULL) { if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST) { schService = CreateService(schSCManager, lpszSvrName, lpszSvrDispName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, lpszSvrExePath, NULL, NULL, NULL, NULL, NULL); } if (schService == NULL) { CloseServiceHandle(schSCManager); return FALSE; } } else { //修改服务的配置。 ChangeServiceConfig(schService, SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, lpszSvrExePath, NULL, NULL, NULL, NULL, NULL, lpszSvrName); } CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return TRUE; }
bool Module::ChangeServiceStartType( _In_ SC_HANDLE serviceHandle, _In_ DWORD startType) { return !!ChangeServiceConfig( serviceHandle, SERVICE_NO_CHANGE, startType, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL); }
BOOL ChangeConfig() { SC_HANDLE service; SC_HANDLE scm; BOOL res; SC_LOCK lock; scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS | GENERIC_WRITE); if (!scm) { ErrorHandler("OpenSCManager", GetLastError()); } lock = LockServiceDatabase(scm); if (lock == 0) { ErrorHandler("LockServiceDatabase", GetLastError()); } service = OpenService(scm, ServiceName, SERVICE_ALL_ACCESS); if (!service) { ErrorHandler("OpenService", GetLastError()); } res = ChangeServiceConfig( service, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL); if (!res) { UnlockServiceDatabase(lock); ErrorHandler("ChangeServiceConfig", GetLastError()); } res = UnlockServiceDatabase(lock); if (!res) { ErrorHandler("UnlockServiceDatabase", GetLastError()); } CloseServiceHandle(service); CloseServiceHandle(scm); return TRUE; }