Search in sources :

Example 1 with SC_HANDLE

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());
}
Also used : SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE)

Example 2 with SC_HANDLE

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());
}
Also used : SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE)

Example 3 with SC_HANDLE

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));
}
Also used : SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE)

Example 4 with SC_HANDLE

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));
}
Also used : SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE)

Example 5 with SC_HANDLE

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);
}
Also used : SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE)

Aggregations

SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)15 Advapi32 (com.sun.jna.platform.win32.Advapi32)3 SERVICE_STATUS_PROCESS (com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS_PROCESS)3 IntByReference (com.sun.jna.ptr.IntByReference)3 Advapi32Ex (org.jumpmind.symmetric.wrapper.jna.Advapi32Ex)3 WString (com.sun.jna.WString)1 Winsvc (com.sun.jna.platform.win32.Winsvc)1 SERVICE_STATUS (com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS)1 WinsvcEx (org.jumpmind.symmetric.wrapper.jna.WinsvcEx)1