public boolean start() { boolean success = false; SC_HANDLE serviceManager = openServiceControlManager(null, WinNT.GENERIC_EXECUTE); if (serviceManager != null) { SC_HANDLE service = ADVAPI_32.OpenService(serviceManager, serviceName, WinNT.GENERIC_EXECUTE); if (service != null) { success = ADVAPI_32.StartService(service, 0, null); ADVAPI_32.CloseServiceHandle(service); } ADVAPI_32.CloseServiceHandle(serviceManager); } return success; }
public boolean stop() { boolean success = false; SC_HANDLE serviceManager = openServiceControlManager(null, WinNT.GENERIC_EXECUTE); if (serviceManager != null) { SC_HANDLE service = Advapi32.INSTANCE.OpenService(serviceManager, serviceName, WinNT.GENERIC_EXECUTE); if (service != null) { SERVICE_STATUS serviceStatus = new SERVICE_STATUS(); success = Advapi32.INSTANCE.ControlService(service, Winsvc.SERVICE_CONTROL_STOP, serviceStatus); Advapi32.INSTANCE.CloseServiceHandle(service); } Advapi32.INSTANCE.CloseServiceHandle(serviceManager); } return success; }
private SC_HANDLE openServiceControlManager(String machine, int access) { return ADVAPI_32.OpenSCManager(machine, null, access); }
public boolean uninstall() { boolean success = false; SC_HANDLE serviceManager = openServiceControlManager(null, Winsvc.SC_MANAGER_ALL_ACCESS); if (serviceManager != null) { SC_HANDLE service = ADVAPI_32.OpenService(serviceManager, serviceName, Winsvc.SERVICE_ALL_ACCESS); if (service != null) { success = ADVAPI_32.DeleteService(service); ADVAPI_32.CloseServiceHandle(service); } ADVAPI_32.CloseServiceHandle(serviceManager); } return success; }
/** * Checks whether a registry key exists. * * @param root * HKEY_LOCAL_MACHINE, etc. * @param key * Path to the registry key. * @return True if the key exists. */ public static boolean registryKeyExists(HKEY root, String key) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ, phkKey); switch (rc) { case W32Errors.ERROR_SUCCESS: Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); return true; case W32Errors.ERROR_FILE_NOT_FOUND: return false; default: throw new Win32Exception(rc); } }
String value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { IntByReference lpcbData = new IntByReference(); IntByReference lpType = new IntByReference(); rc = Advapi32.INSTANCE.RegQueryValueEx(phkKey.getValue(), value, 0, lpType, (char[]) null, lpcbData); if (rc != W32Errors.ERROR_SUCCESS rc = Advapi32.INSTANCE.RegQueryValueEx(phkKey.getValue(), value, 0, lpType, data, lpcbData); if (rc != W32Errors.ERROR_SUCCESS rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc);
SC_HANDLE service = ADVAPI_32.CreateService(serviceManager, serviceName, displayName, Winsvc.SERVICE_ALL_ACCESS, WinNT.SERVICE_WIN32_OWN_PROCESS, WinNT.SERVICE_AUTO_START, WinNT.SERVICE_ERROR_NORMAL, success = ADVAPI_32.ChangeServiceConfig2(service, Winsvc.SERVICE_CONFIG_DESCRIPTION, desc); ADVAPI_32.CloseServiceHandle(service); ADVAPI_32.CloseServiceHandle(serviceManager);
/** * Create a registry key. * * @param hKey * Parent key. * @param keyName * Key name. * @param samDesiredExtra * Registry key security and access rights to be requested in addition to WinNT.KEY_READ. * (e.g WinNT.KEY_WOW64_32KEY or WinNT.KEY_WOW64_64KEY to force 32bit or 64bit registry access.) * @return True if the key was created, false otherwise. */ public static boolean registryCreateKey(WinReg.HKEY hKey, String keyName, int samDesiredExtra) { HKEYByReference phkResult = new HKEYByReference(); IntByReference lpdwDisposition = new IntByReference(); int rc = Advapi32.INSTANCE.RegCreateKeyEx(hKey, keyName, 0, null, WinNT.REG_OPTION_NON_VOLATILE, WinNT.KEY_READ | samDesiredExtra, null, phkResult, lpdwDisposition); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } rc = Advapi32.INSTANCE.RegCloseKey(phkResult.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } return WinNT.REG_CREATED_NEW_KEY == lpdwDisposition.getValue(); }
/** * Continue service. */ public void continueService() { waitForNonPendingState(); // If the service is already stopped - return if (queryStatus().dwCurrentState == Winsvc.SERVICE_RUNNING) { return; } if (! Advapi32.INSTANCE.ControlService(_handle, Winsvc.SERVICE_CONTROL_CONTINUE, new Winsvc.SERVICE_STATUS())) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } waitForNonPendingState(); if (queryStatus().dwCurrentState != Winsvc.SERVICE_RUNNING) { throw new RuntimeException("Unable to continue the service"); } }
/** * Closes the previously opened Service Manager. */ @Override public void close() { if (_handle != null) { if (! Advapi32.INSTANCE.CloseServiceHandle(_handle)) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } _handle = null; } }
/** * Close the registry key * * @param hKey * Registry key. */ public static void registryCloseKey(HKEY hKey) { int rc = Advapi32.INSTANCE.RegCloseKey(hKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } }
public void startService() { waitForNonPendingState(); // If the service is already running - return if (queryStatus().dwCurrentState == Winsvc.SERVICE_RUNNING) { return; } if (! Advapi32.INSTANCE.StartService(_handle, 0, null)) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } waitForNonPendingState(); if (queryStatus().dwCurrentState != Winsvc.SERVICE_RUNNING) { throw new RuntimeException("Unable to start the service"); } }
/** * Opens a Service. * @param serviceName * Service name. * @param permissions * Permissions. * @return * Returns an opened service. */ public W32Service openService(String serviceName, int permissions) { SC_HANDLE serviceHandle = Advapi32.INSTANCE.OpenService( _handle, serviceName, permissions); if (serviceHandle == null) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } return new W32Service(serviceHandle); }
/** * Get a registry key, the caller is responsible to close the key after use. * * @param root * Root key. * @param keyPath * Path to a registry key. * * @param samDesired * Access level (e.g. WinNT.KEY_READ) * * @return HKEYByReference. */ public static HKEYByReference registryGetKey(HKEY root, String keyPath, int samDesired) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, samDesired, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } return phkKey; }
if (! Advapi32.INSTANCE.ControlService(_handle, SERVICE_CONTROL_STOP, status)) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); throw new RuntimeException(e); if(! Advapi32.INSTANCE.QueryServiceStatus(_handle, status)) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
/** * Set the failure action flag of the specified service. Corresponds to * <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms681988.aspx">ChangeServiceConfig2</a> * with parameter dwInfoLevel set to SERVICE_CONFIG_FAILURE_ACTIONS_FLAG. */ public void setFailureActionsFlag(boolean flagValue) { SERVICE_FAILURE_ACTIONS_FLAG flag = new SERVICE_FAILURE_ACTIONS_FLAG(); flag.fFailureActionsOnNonCrashFailures = flagValue ? 1 : 0; if (!Advapi32.INSTANCE.ChangeServiceConfig2(_handle, Winsvc.SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, flag)) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } }
/** * Call close() in the case when the caller needs to abandon the * iterator before the iteration completes. */ public void close() { _done = true; if (_h != null) { if (!Advapi32.INSTANCE.CloseEventLog(_h)) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } _h = null; } }
/** * Convert a security identifier (SID) to a string format suitable for * display, storage, or transmission. * * @param sid * SID bytes. * @return String SID. */ public static String convertSidToStringSid(PSID sid) { PointerByReference stringSid = new PointerByReference(); if (!Advapi32.INSTANCE.ConvertSidToStringSid(sid, stringSid)) { throw new Win32Exception(Kernel32.INSTANCE.GetLastError()); } Pointer ptr = stringSid.getValue(); try { return ptr.getWideString(0); } finally { Kernel32Util.freeLocalMemory(ptr); } }
/** * Checks whether a registry key exists. * * @param root * HKEY_LOCAL_MACHINE, etc. * @param key * Path to the registry key. * @param samDesiredExtra * Registry key security and access rights to be requested in addition to WinNT.KEY_READ. * (e.g WinNT.KEY_WOW64_32KEY or WinNT.KEY_WOW64_64KEY to force 32bit or 64bit registry access.) * @return True if the key exists. */ public static boolean registryKeyExists(HKEY root, String key, int samDesiredExtra) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | samDesiredExtra, phkKey); switch (rc) { case W32Errors.ERROR_SUCCESS: Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); return true; case W32Errors.ERROR_FILE_NOT_FOUND: return false; default: throw new Win32Exception(rc); } }
String value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ, phkKey); try { rc = Advapi32.INSTANCE.RegQueryValueEx(phkKey.getValue(), value, 0, lpType, (char[]) null, lpcbData); switch (rc) { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc);