increase chance of accurate error messages

This commit is contained in:
Doug MacEachern 2005-06-26 05:33:52 +00:00
parent 7f2451aa76
commit 01f5cb6c9e
3 changed files with 24 additions and 11 deletions

View File

@ -63,14 +63,23 @@ JNIEXPORT jboolean SIGAR_JNI(win32_Service_CloseServiceHandle)
return CloseServiceHandle((SC_HANDLE)handle); 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) (JNIEnv *, jclass, jlong handle, jint control)
{ {
BOOL retval;
SERVICE_STATUS status; SERVICE_STATUS status;
if (control == 0) { 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) JNIEXPORT jlong SIGAR_JNI(win32_Service_CreateService)

View File

@ -183,6 +183,12 @@ public class Service extends Win32 {
return STATUS[getStatus()]; 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 { private static class Waiter {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
Service service; Service service;
@ -226,9 +232,7 @@ public class Service extends Win32 {
public void stop() throws Win32Exception public void stop() throws Win32Exception
{ {
if (ControlService(this.service, CONTROL_STOP) == false) { control(CONTROL_STOP);
throw getLastErrorException();
}
} }
public void stop(long timeout) throws Win32Exception public void stop(long timeout) throws Win32Exception
@ -247,9 +251,7 @@ public class Service extends Win32 {
public void start() throws Win32Exception public void start() throws Win32Exception
{ {
if (ControlService(this.service, CONTROL_START) == false) { control(CONTROL_START);
throw getLastErrorException();
}
} }
public void start(long timeout) throws Win32Exception public void start(long timeout) throws Win32Exception
@ -298,8 +300,8 @@ public class Service extends Win32 {
String startName, String startName,
String password); String password);
private static native boolean ControlService(long handle, private static native int ControlService(long handle,
int control); int control);
private static native boolean DeleteService(long handle); private static native boolean DeleteService(long handle);

View File

@ -5,6 +5,8 @@ import net.hyperic.sigar.SigarException;
abstract class Win32 { abstract class Win32 {
static int SUCCESS = 0;
static { static {
try { try {
Sigar.load(); Sigar.load();