Search in sources :

Example 11 with SC_HANDLE

use of com.sun.jna.platform.win32.Winsvc.SC_HANDLE in project jna by java-native-access.

the class Win32Service method openServiceControlManager.

/**
   * Get a handle to the ServiceControlManager.
   *
   * @param machine name of the machine or null for localhost
   * @param access access flags
   * @return handle to ServiceControlManager or null when failed
   */
private SC_HANDLE openServiceControlManager(String machine, int access) {
    SC_HANDLE handle = null;
    Advapi32 advapi32;
    advapi32 = Advapi32.INSTANCE;
    handle = advapi32.OpenSCManager(machine, null, access);
    return (handle);
}
Also used : SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE)

Example 12 with SC_HANDLE

use of com.sun.jna.platform.win32.Winsvc.SC_HANDLE in project jna by java-native-access.

the class Win32Service method stop.

/**
   * Ask the ServiceControlManager to stop the service.
   * @return true on success
   */
public boolean stop() throws Exception {
    Advapi32 advapi32;
    SC_HANDLE serviceManager, service;
    SERVICE_STATUS serviceStatus;
    boolean success = false;
    advapi32 = Advapi32.INSTANCE;
    serviceManager = openServiceControlManager(null, WinNT.GENERIC_EXECUTE);
    if (serviceManager != null) {
        service = advapi32.OpenService(serviceManager, serviceName, WinNT.GENERIC_EXECUTE);
        if (service != null) {
            serviceStatus = new SERVICE_STATUS();
            success = advapi32.ControlService(service, Winsvc.SERVICE_CONTROL_STOP, serviceStatus);
            advapi32.CloseServiceHandle(service);
        }
        advapi32.CloseServiceHandle(serviceManager);
    }
    return (success);
}
Also used : SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) SERVICE_STATUS(com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS)

Example 13 with SC_HANDLE

use of com.sun.jna.platform.win32.Winsvc.SC_HANDLE in project jna by java-native-access.

the class Advapi32Test method testStartService.

public void testStartService() {
    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);
    assertFalse(Advapi32.INSTANCE.StartService(serviceHandle, 0, null));
    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 14 with SC_HANDLE

use of com.sun.jna.platform.win32.Winsvc.SC_HANDLE in project jna by java-native-access.

the class Advapi32Test method testQueryServiceStatusEx.

public void testQueryServiceStatusEx() {
    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_STATUS);
    assertNotNull(serviceHandle);
    IntByReference pcbBytesNeeded = new IntByReference();
    assertFalse(Advapi32.INSTANCE.QueryServiceStatusEx(serviceHandle, SC_STATUS_TYPE.SC_STATUS_PROCESS_INFO, null, 0, pcbBytesNeeded));
    assertEquals(W32Errors.ERROR_INSUFFICIENT_BUFFER, Kernel32.INSTANCE.GetLastError());
    assertTrue(pcbBytesNeeded.getValue() > 0);
    SERVICE_STATUS_PROCESS status = new SERVICE_STATUS_PROCESS(pcbBytesNeeded.getValue());
    assertTrue(Advapi32.INSTANCE.QueryServiceStatusEx(serviceHandle, SC_STATUS_TYPE.SC_STATUS_PROCESS_INFO, status, status.size(), pcbBytesNeeded));
    assertTrue(status.dwCurrentState == Winsvc.SERVICE_STOPPED || status.dwCurrentState == Winsvc.SERVICE_RUNNING);
    assertTrue(Advapi32.INSTANCE.CloseServiceHandle(serviceHandle));
    assertTrue(Advapi32.INSTANCE.CloseServiceHandle(scmHandle));
}
Also used : SERVICE_STATUS_PROCESS(com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS_PROCESS) IntByReference(com.sun.jna.ptr.IntByReference) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE)

Example 15 with SC_HANDLE

use of com.sun.jna.platform.win32.Winsvc.SC_HANDLE in project symmetric-ds by JumpMind.

the class WindowsService method uninstall.

@Override
public void uninstall() {
    if (isRunning()) {
        throw new WrapperException(Constants.RC_NO_INSTALL_WHEN_RUNNING, 0, "Server must be stopped before uninstalling");
    }
    Advapi32Ex advapi = Advapi32Ex.INSTANCE;
    SC_HANDLE manager = openServiceManager();
    SC_HANDLE service = advapi.OpenService(manager, config.getName(), Winsvc.SERVICE_ALL_ACCESS);
    try {
        if (service != null) {
            System.out.println("Uninstalling " + config.getName() + " ...");
            if (!advapi.DeleteService(service)) {
                throwException("DeleteService");
            }
        } else {
            throw new WrapperException(Constants.RC_NOT_INSTALLED, 0, "Service " + config.getName() + " is not installed");
        }
    } finally {
        closeServiceHandle(service);
        closeServiceHandle(manager);
    }
    int seconds = 0;
    while (seconds <= 30) {
        if (!isInstalled()) {
            break;
        }
        System.out.print(".");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        seconds++;
    }
    if (seconds > 0) {
        System.out.println("");
    }
    if (isInstalled()) {
        System.out.println("Service manager did not complete");
    } else {
        System.out.println("Done");
    }
}
Also used : SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) Advapi32Ex(org.jumpmind.symmetric.wrapper.jna.Advapi32Ex)

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