Search in sources :

Example 1 with SERVICE_STATUS_PROCESS

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

the class W32Service method queryStatus.

/**
	 * Retrieves the current status of the specified service based on the specified information level.
	 * @return 
	 *  Service status information
	 */
public SERVICE_STATUS_PROCESS queryStatus() {
    IntByReference size = new IntByReference();
    Advapi32.INSTANCE.QueryServiceStatusEx(_handle, SC_STATUS_TYPE.SC_STATUS_PROCESS_INFO, null, 0, size);
    SERVICE_STATUS_PROCESS status = new SERVICE_STATUS_PROCESS(size.getValue());
    if (!Advapi32.INSTANCE.QueryServiceStatusEx(_handle, SC_STATUS_TYPE.SC_STATUS_PROCESS_INFO, status, status.size(), size)) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    return status;
}
Also used : SERVICE_STATUS_PROCESS(com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS_PROCESS) IntByReference(com.sun.jna.ptr.IntByReference)

Example 2 with SERVICE_STATUS_PROCESS

use of com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS_PROCESS 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 3 with SERVICE_STATUS_PROCESS

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

the class W32Service method waitForNonPendingState.

/**
     * Wait for the state to change to something other than a pending state.
     */
public void waitForNonPendingState() {
    SERVICE_STATUS_PROCESS status = queryStatus();
    int previousCheckPoint = status.dwCheckPoint;
    int checkpointStartTickCount = Kernel32.INSTANCE.GetTickCount();
    ;
    while (isPendingState(status.dwCurrentState)) {
        // if the checkpoint advanced, start new tick count
        if (status.dwCheckPoint != previousCheckPoint) {
            previousCheckPoint = status.dwCheckPoint;
            checkpointStartTickCount = Kernel32.INSTANCE.GetTickCount();
        }
        // if the time that passed is greater than the wait hint - throw timeout exception
        if (Kernel32.INSTANCE.GetTickCount() - checkpointStartTickCount > status.dwWaitHint) {
            throw new RuntimeException("Timeout waiting for service to change to a non-pending state.");
        }
        // do not wait longer than the wait hint. A good interval is 
        // one-tenth the wait hint, but no less than 1 second and no 
        // more than 10 seconds. 
        int dwWaitTime = status.dwWaitHint / 10;
        if (dwWaitTime < 1000)
            dwWaitTime = 1000;
        else if (dwWaitTime > 10000)
            dwWaitTime = 10000;
        try {
            Thread.sleep(dwWaitTime);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        status = queryStatus();
    }
}
Also used : SERVICE_STATUS_PROCESS(com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS_PROCESS)

Example 4 with SERVICE_STATUS_PROCESS

use of com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS_PROCESS 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 5 with SERVICE_STATUS_PROCESS

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

the class W32ServiceTest method testQueryStatus.

public void testQueryStatus() {
    W32Service service = _serviceManager.openService("eventlog", Winsvc.SERVICE_QUERY_STATUS);
    SERVICE_STATUS_PROCESS status = service.queryStatus();
    assertTrue(status.dwCurrentState == Winsvc.SERVICE_RUNNING || status.dwCurrentState == Winsvc.SERVICE_STOPPED);
    service.close();
}
Also used : SERVICE_STATUS_PROCESS(com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS_PROCESS)

Aggregations

SERVICE_STATUS_PROCESS (com.sun.jna.platform.win32.Winsvc.SERVICE_STATUS_PROCESS)5 IntByReference (com.sun.jna.ptr.IntByReference)3 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)2 Advapi32 (com.sun.jna.platform.win32.Advapi32)1