use of com.sun.jna.platform.win32.Winsvc.SC_HANDLE in project jna by java-native-access.
the class Advapi32Test method testOpenSCManager.
public void testOpenSCManager() {
SC_HANDLE handle = Advapi32.INSTANCE.OpenSCManager(null, null, Winsvc.SC_MANAGER_CONNECT);
assertNotNull(handle);
assertTrue(Advapi32.INSTANCE.CloseServiceHandle(handle));
assertNull(Advapi32.INSTANCE.OpenSCManager("invalidMachineName", null, Winsvc.SC_MANAGER_CONNECT));
int err = Kernel32.INSTANCE.GetLastError();
assertTrue("Unexpected error in OpenSCManager: " + err, err == W32Errors.RPC_S_SERVER_UNAVAILABLE || err == W32Errors.RPC_S_INVALID_NET_ADDR);
assertNull(Advapi32.INSTANCE.OpenSCManager(null, "invalidDatabase", Winsvc.SC_MANAGER_CONNECT));
assertEquals(W32Errors.ERROR_INVALID_NAME, Kernel32.INSTANCE.GetLastError());
}
use of com.sun.jna.platform.win32.Winsvc.SC_HANDLE in project jna by java-native-access.
the class Advapi32Test method testCloseServiceHandle.
public void testCloseServiceHandle() throws Exception {
SC_HANDLE handle = Advapi32.INSTANCE.OpenSCManager(null, null, Winsvc.SC_MANAGER_CONNECT);
assertNotNull(handle);
assertTrue(Advapi32.INSTANCE.CloseServiceHandle(handle));
assertFalse(Advapi32.INSTANCE.CloseServiceHandle(null));
assertEquals(W32Errors.ERROR_INVALID_HANDLE, Kernel32.INSTANCE.GetLastError());
}
use of com.sun.jna.platform.win32.Winsvc.SC_HANDLE in project jna by java-native-access.
the class Advapi32Test method testControlService.
public void testControlService() {
SC_HANDLE scmHandle = Advapi32.INSTANCE.OpenSCManager(null, null, Winsvc.SC_MANAGER_CONNECT);
assertNotNull(scmHandle);
SC_HANDLE serviceHandle = Advapi32.INSTANCE.OpenService(scmHandle, "eventlog", Winsvc.SERVICE_QUERY_CONFIG);
assertNotNull(serviceHandle);
Winsvc.SERVICE_STATUS serverStatus = new Winsvc.SERVICE_STATUS();
assertNotNull(serviceHandle);
assertFalse(Advapi32.INSTANCE.ControlService(serviceHandle, Winsvc.SERVICE_CONTROL_STOP, serverStatus));
assertEquals(W32Errors.ERROR_ACCESS_DENIED, Kernel32.INSTANCE.GetLastError());
assertTrue(Advapi32.INSTANCE.CloseServiceHandle(serviceHandle));
assertTrue(Advapi32.INSTANCE.CloseServiceHandle(scmHandle));
}
use of com.sun.jna.platform.win32.Winsvc.SC_HANDLE in project jna by java-native-access.
the class Advapi32Test method testOpenService.
public void testOpenService() {
assertNull(Advapi32.INSTANCE.OpenService(null, "eventlog", Winsvc.SERVICE_QUERY_CONFIG));
assertEquals(W32Errors.ERROR_INVALID_HANDLE, Kernel32.INSTANCE.GetLastError());
SC_HANDLE scmHandle = Advapi32.INSTANCE.OpenSCManager(null, null, Winsvc.SC_MANAGER_CONNECT);
assertNotNull(scmHandle);
SC_HANDLE serviceHandle = Advapi32.INSTANCE.OpenService(scmHandle, "eventlog", Winsvc.SERVICE_QUERY_CONFIG);
assertNotNull(serviceHandle);
assertTrue(Advapi32.INSTANCE.CloseServiceHandle(serviceHandle));
assertNull(Advapi32.INSTANCE.OpenService(scmHandle, "slashesArentValidChars/", Winsvc.SERVICE_QUERY_CONFIG));
assertEquals(W32Errors.ERROR_INVALID_NAME, Kernel32.INSTANCE.GetLastError());
assertNull(Advapi32.INSTANCE.OpenService(scmHandle, "serviceDoesNotExist", Winsvc.SERVICE_QUERY_CONFIG));
assertEquals(W32Errors.ERROR_SERVICE_DOES_NOT_EXIST, Kernel32.INSTANCE.GetLastError());
assertTrue(Advapi32.INSTANCE.CloseServiceHandle(scmHandle));
}
use of com.sun.jna.platform.win32.Winsvc.SC_HANDLE in project jna by java-native-access.
the class Win32Service method uninstall.
/**
* Uninstall the service.
*
* @throws java.lang.Exception
* @return true on success
*/
public boolean uninstall() {
Advapi32 advapi32;
SC_HANDLE serviceManager, service;
boolean success = false;
advapi32 = Advapi32.INSTANCE;
serviceManager = openServiceControlManager(null, Winsvc.SC_MANAGER_ALL_ACCESS);
if (serviceManager != null) {
service = advapi32.OpenService(serviceManager, serviceName, Winsvc.SERVICE_ALL_ACCESS);
if (service != null) {
success = advapi32.DeleteService(service);
advapi32.CloseServiceHandle(service);
}
advapi32.CloseServiceHandle(serviceManager);
}
return (success);
}
Aggregations