diff --git a/bindings/java/src/jni/win32/service.cpp b/bindings/java/src/jni/win32/service.cpp index 22729cee..072b70bf 100644 --- a/bindings/java/src/jni/win32/service.cpp +++ b/bindings/java/src/jni/win32/service.cpp @@ -63,14 +63,23 @@ JNIEXPORT jboolean SIGAR_JNI(win32_Service_CloseServiceHandle) return CloseServiceHandle((SC_HANDLE)handle); } -JNIEXPORT jboolean SIGAR_JNI(win32_Service_ControlService) +JNIEXPORT jint SIGAR_JNI(win32_Service_ControlService) (JNIEnv *, jclass, jlong handle, jint control) { + BOOL retval; SERVICE_STATUS status; if (control == 0) { - return StartService((SC_HANDLE)handle, 0, NULL); + retval = StartService((SC_HANDLE)handle, 0, NULL); + } + else { + retval = ControlService((SC_HANDLE)handle, control, &status); + } + if (retval) { + return ERROR_SUCCESS; + } + else { + return GetLastError(); } - return ControlService((SC_HANDLE)handle, control, &status); } JNIEXPORT jlong SIGAR_JNI(win32_Service_CreateService) diff --git a/bindings/java/src/net/hyperic/sigar/win32/Service.java b/bindings/java/src/net/hyperic/sigar/win32/Service.java index 4b27cf43..21c0c37e 100644 --- a/bindings/java/src/net/hyperic/sigar/win32/Service.java +++ b/bindings/java/src/net/hyperic/sigar/win32/Service.java @@ -183,6 +183,12 @@ public class Service extends Win32 { return STATUS[getStatus()]; } + private void control(int ctl) throws Win32Exception { + if (ControlService(this.service, ctl) != SUCCESS) { + throw new Win32Exception(GetErrorMessage(ctl)); + } + } + private static class Waiter { long start = System.currentTimeMillis(); Service service; @@ -226,9 +232,7 @@ public class Service extends Win32 { public void stop() throws Win32Exception { - if (ControlService(this.service, CONTROL_STOP) == false) { - throw getLastErrorException(); - } + control(CONTROL_STOP); } public void stop(long timeout) throws Win32Exception @@ -247,9 +251,7 @@ public class Service extends Win32 { public void start() throws Win32Exception { - if (ControlService(this.service, CONTROL_START) == false) { - throw getLastErrorException(); - } + control(CONTROL_START); } public void start(long timeout) throws Win32Exception @@ -298,8 +300,8 @@ public class Service extends Win32 { String startName, String password); - private static native boolean ControlService(long handle, - int control); + private static native int ControlService(long handle, + int control); private static native boolean DeleteService(long handle); diff --git a/bindings/java/src/net/hyperic/sigar/win32/Win32.java b/bindings/java/src/net/hyperic/sigar/win32/Win32.java index 157c4b75..4bb6ad60 100644 --- a/bindings/java/src/net/hyperic/sigar/win32/Win32.java +++ b/bindings/java/src/net/hyperic/sigar/win32/Win32.java @@ -5,6 +5,8 @@ import net.hyperic.sigar.SigarException; abstract class Win32 { + static int SUCCESS = 0; + static { try { Sigar.load();