Search in sources :

Example 6 with SC_HANDLE

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

the class WindowsService method openServiceManager.

protected SC_HANDLE openServiceManager() {
    Advapi32 advapi = Advapi32.INSTANCE;
    SC_HANDLE handle = advapi.OpenSCManager(null, null, Winsvc.SC_MANAGER_ALL_ACCESS);
    if (handle == null) {
        throwException("OpenSCManager");
    }
    return handle;
}
Also used : SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) Advapi32(com.sun.jna.platform.win32.Advapi32)

Example 7 with SC_HANDLE

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

the class WindowsService method waitForService.

protected Winsvc.SERVICE_STATUS_PROCESS waitForService(SC_HANDLE manager, SC_HANDLE service) {
    int seconds = 0;
    Advapi32Ex advapi = Advapi32Ex.INSTANCE;
    IntByReference bytesNeeded = new IntByReference();
    advapi.QueryServiceStatusEx(service, SC_STATUS_TYPE.SC_STATUS_PROCESS_INFO, null, 0, bytesNeeded);
    Winsvc.SERVICE_STATUS_PROCESS status = new Winsvc.SERVICE_STATUS_PROCESS(bytesNeeded.getValue());
    while (seconds <= 5) {
        System.out.print(".");
        if (!advapi.QueryServiceStatusEx(service, SC_STATUS_TYPE.SC_STATUS_PROCESS_INFO, status, status.size(), bytesNeeded)) {
            throwException("QueryServiceStatusEx");
        }
        if (status.dwCurrentState == Winsvc.SERVICE_STOPPED) {
            break;
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        seconds++;
    }
    System.out.println("");
    return status;
}
Also used : SERVICE_STATUS_PROCESS(com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS_PROCESS) IntByReference(com.sun.jna.ptr.IntByReference) Winsvc(com.sun.jna.platform.win32.Winsvc) SERVICE_STATUS_PROCESS(com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS_PROCESS) Advapi32Ex(org.jumpmind.symmetric.wrapper.jna.Advapi32Ex)

Example 8 with SC_HANDLE

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

the class WindowsService method isRunning.

@Override
public boolean isRunning() {
    Advapi32 advapi = Advapi32.INSTANCE;
    SC_HANDLE manager = advapi.OpenSCManager(null, null, Winsvc.SC_MANAGER_ENUMERATE_SERVICE);
    if (manager == null) {
        throwException("OpenSCManager");
    } else {
        SC_HANDLE service = advapi.OpenService(manager, config.getName(), Winsvc.SERVICE_QUERY_STATUS);
        if (service != null) {
            IntByReference bytesNeeded = new IntByReference();
            advapi.QueryServiceStatusEx(service, SC_STATUS_TYPE.SC_STATUS_PROCESS_INFO, null, 0, bytesNeeded);
            SERVICE_STATUS_PROCESS status = new SERVICE_STATUS_PROCESS(bytesNeeded.getValue());
            if (!advapi.QueryServiceStatusEx(service, SC_STATUS_TYPE.SC_STATUS_PROCESS_INFO, status, status.size(), bytesNeeded)) {
                throwException("QueryServiceStatusEx");
            }
            closeServiceHandle(service);
            closeServiceHandle(manager);
            return (status.dwCurrentState == Winsvc.SERVICE_RUNNING);
        }
        closeServiceHandle(manager);
    }
    return super.isRunning();
}
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) Advapi32(com.sun.jna.platform.win32.Advapi32)

Example 9 with SC_HANDLE

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

the class WindowsService method install.

@Override
public void install() {
    if (isRunning()) {
        System.out.println("Server must be stopped before installing");
        System.exit(Constants.RC_NO_INSTALL_WHEN_RUNNING);
    }
    Advapi32Ex advapi = Advapi32Ex.INSTANCE;
    SC_HANDLE manager = openServiceManager();
    SC_HANDLE service = advapi.OpenService(manager, config.getName(), Winsvc.SERVICE_ALL_ACCESS);
    try {
        if (service != null) {
            throw new WrapperException(Constants.RC_ALREADY_INSTALLED, 0, "Service " + config.getName() + " is already installed");
        } else {
            System.out.println("Installing " + config.getName() + " ...");
            String dependencies = null;
            if (config.getDependencies() != null && config.getDependencies().size() > 0) {
                StringBuffer sb = new StringBuffer();
                for (String dependency : config.getDependencies()) {
                    sb.append(dependency).append("\0");
                }
                dependencies = sb.append("\0").toString();
            }
            service = advapi.CreateService(manager, config.getName(), config.getDisplayName(), Winsvc.SERVICE_ALL_ACCESS, WinsvcEx.SERVICE_WIN32_OWN_PROCESS, config.isAutoStart() || config.isDelayStart() ? WinsvcEx.SERVICE_AUTO_START : WinsvcEx.SERVICE_DEMAND_START, WinsvcEx.SERVICE_ERROR_NORMAL, commandToString(getWrapperCommand("init")), null, null, dependencies, null, null);
            if (service != null) {
                Advapi32Ex.SERVICE_DESCRIPTION desc = new Advapi32Ex.SERVICE_DESCRIPTION(config.getDescription());
                advapi.ChangeServiceConfig2(service, WinsvcEx.SERVICE_CONFIG_DESCRIPTION, desc);
                if (config.isDelayStart()) {
                    WinsvcEx.SERVICE_DELAYED_AUTO_START_INFO delayedInfo = new WinsvcEx.SERVICE_DELAYED_AUTO_START_INFO(true);
                    advapi.ChangeServiceConfig2(service, WinsvcEx.SERVICE_CONFIG_DELAYED_AUTO_START_INFO, delayedInfo);
                }
            } else {
                throwException("CreateService");
            }
            System.out.println("Done");
        }
    } finally {
        closeServiceHandle(service);
        closeServiceHandle(manager);
    }
}
Also used : WinsvcEx(org.jumpmind.symmetric.wrapper.jna.WinsvcEx) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) WString(com.sun.jna.WString) Advapi32Ex(org.jumpmind.symmetric.wrapper.jna.Advapi32Ex)

Example 10 with SC_HANDLE

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

the class Win32Service method start.

/**
   * Ask the ServiceControlManager to start the service.
   * @return true on success
   */
public boolean start() {
    Advapi32 advapi32;
    SC_HANDLE serviceManager, service;
    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) {
            success = advapi32.StartService(service, 0, null);
            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