throw exceptions in jni, GetLastError may have changed by the time it returns to java land

This commit is contained in:
Doug MacEachern 2005-06-26 22:02:02 +00:00
parent 10fde2cca9
commit c74f93fdea
2 changed files with 18 additions and 27 deletions

View File

@ -159,6 +159,10 @@ JNIEXPORT jlong SIGAR_JNI(win32_Service_CreateService)
env->ReleaseStringChars(displayName, (const jchar *)lpDisplayName); env->ReleaseStringChars(displayName, (const jchar *)lpDisplayName);
env->ReleaseStringChars(serviceName, (const jchar *)lpServiceName); env->ReleaseStringChars(serviceName, (const jchar *)lpServiceName);
if (lResult == 0) {
win32_throw_last_error(env);
}
return lResult; return lResult;
} }
@ -177,6 +181,10 @@ JNIEXPORT jlong SIGAR_JNI(win32_Service_OpenSCManager)
lResult = (jlong)OpenSCManager(lpMachine, NULL, access); lResult = (jlong)OpenSCManager(lpMachine, NULL, access);
env->ReleaseStringChars(machine, (const jchar *)lpMachine); env->ReleaseStringChars(machine, (const jchar *)lpMachine);
if (!lResult) {
win32_throw_last_error(env);
}
return lResult; return lResult;
} }
@ -188,12 +196,15 @@ JNIEXPORT jlong SIGAR_JNI(win32_Service_OpenService)
jint access) jint access)
{ {
jlong lResult; jlong lResult;
LPCTSTR lpService = (LPCTSTR)env->GetStringChars(service, NULL); LPCTSTR lpService = (LPCTSTR)env->GetStringChars(service, NULL);
lResult = (jlong)OpenService((SC_HANDLE)handle, lResult = (jlong)OpenService((SC_HANDLE)handle,
lpService, access); lpService, access);
env->ReleaseStringChars(service, (const jchar *)lpService); env->ReleaseStringChars(service, (const jchar *)lpService);
if (!lResult) {
win32_throw_last_error(env);
}
return lResult; return lResult;
} }
@ -302,6 +313,7 @@ JNIEXPORT jboolean SIGAR_JNI(win32_Service_QueryServiceConfig)
if (!QueryServiceConfig((SC_HANDLE)handle, config, if (!QueryServiceConfig((SC_HANDLE)handle, config,
sizeof(buffer), &bytes)) sizeof(buffer), &bytes))
{ {
win32_throw_last_error(env);
return JNI_FALSE; return JNI_FALSE;
} }

View File

@ -79,10 +79,6 @@ public class Service extends Win32 {
private Service() throws Win32Exception private Service() throws Win32Exception
{ {
this.manager = OpenSCManager("", SC_MANAGER_ALL_ACCESS); this.manager = OpenSCManager("", SC_MANAGER_ALL_ACCESS);
if (this.manager == 0) {
throw getLastErrorException();
}
} }
public static native List getServiceNames() throws Win32Exception; public static native List getServiceNames() throws Win32Exception;
@ -94,10 +90,6 @@ public class Service extends Win32 {
this.service = OpenService(this.manager, serviceName, this.service = OpenService(this.manager, serviceName,
SERVICE_ALL_ACCESS); SERVICE_ALL_ACCESS);
if (this.service == 0) {
throw getLastErrorException();
}
this.name = serviceName; this.name = serviceName;
} }
@ -143,10 +135,6 @@ public class Service extends Win32 {
config.getServiceStartName(), config.getServiceStartName(),
config.getPassword()); config.getPassword());
if (service.service == 0) {
throw getLastErrorException();
}
if (config.getDescription() != null) { if (config.getDescription() != null) {
service.setDescription(config.getDescription()); service.setDescription(config.getDescription());
} }
@ -270,20 +258,11 @@ public class Service extends Win32 {
public ServiceConfig getConfig() throws Win32Exception { public ServiceConfig getConfig() throws Win32Exception {
ServiceConfig config = new ServiceConfig(); ServiceConfig config = new ServiceConfig();
if (!QueryServiceConfig(this.service, config)) { QueryServiceConfig(this.service, config);
throw getLastErrorException();
}
config.setName(this.name); config.setName(this.name);
return config; return config;
} }
private static Win32Exception getLastErrorException()
{
int err = GetLastError();
return new Win32Exception(err, "Win32 Error Code: " +
err + ": " + GetErrorMessage(err));
}
private static native boolean ChangeServiceDescription(long handle, private static native boolean ChangeServiceDescription(long handle,
String description); String description);
@ -298,7 +277,7 @@ public class Service extends Win32 {
String path, String path,
String[] dependencies, String[] dependencies,
String startName, String startName,
String password); String password) throws Win32Exception;
private static native int ControlService(long handle, private static native int ControlService(long handle,
int control); int control);
@ -306,16 +285,16 @@ public class Service extends Win32 {
private static native boolean DeleteService(long handle); private static native boolean DeleteService(long handle);
private static native long OpenSCManager(String machine, private static native long OpenSCManager(String machine,
int access); int access) throws Win32Exception;
private static native long OpenService(long handle, private static native long OpenService(long handle,
String service, String service,
int access); int access) throws Win32Exception;
private static native int QueryServiceStatus(long handle); private static native int QueryServiceStatus(long handle);
private static native boolean QueryServiceConfig(long handle, private static native boolean QueryServiceConfig(long handle,
ServiceConfig config); ServiceConfig config) throws Win32Exception;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
List services; List services;