add Service.getServiceNames method
This commit is contained in:
parent
0a8464d9e9
commit
eb1528f3f4
|
@ -355,6 +355,56 @@ JNIEXPORT jboolean SIGAR_JNI(win32_Service_StopService)
|
|||
return ControlService((SC_HANDLE)handle, SERVICE_CONTROL_STOP, &status);
|
||||
}
|
||||
|
||||
JNIEXPORT jobject SIGAR_JNI(win32_Service_getServiceNames)
|
||||
(JNIEnv *env, jclass)
|
||||
{
|
||||
SC_HANDLE handle =
|
||||
OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
|
||||
ENUM_SERVICE_STATUS status, *services;
|
||||
BOOL retval;
|
||||
DWORD bytes, count, resume=0;
|
||||
DWORD type = SERVICE_WIN32, state = SERVICE_STATE_ALL;
|
||||
jobject listobj;
|
||||
jclass listclass =
|
||||
env->FindClass("java/util/ArrayList");
|
||||
jmethodID listid =
|
||||
env->GetMethodID(listclass, "<init>", "()V");
|
||||
jmethodID addid =
|
||||
env->GetMethodID(listclass, "add",
|
||||
"(Ljava/lang/Object;)"
|
||||
"Z");
|
||||
|
||||
if (handle == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
retval = EnumServicesStatus(handle, type, state,
|
||||
&status, sizeof(status),
|
||||
&bytes, &count, &resume);
|
||||
|
||||
DWORD err = GetLastError();
|
||||
|
||||
if ((retval == FALSE) || err == ERROR_MORE_DATA) {
|
||||
DWORD size = bytes + sizeof(ENUM_SERVICE_STATUS);
|
||||
services = new ENUM_SERVICE_STATUS[size];
|
||||
EnumServicesStatus(handle, type, state, services,
|
||||
size, &bytes, &count, &resume);
|
||||
}
|
||||
|
||||
listobj = env->NewObject(listclass, listid);
|
||||
for (int i=0; i<count; i++) {
|
||||
jstring name =
|
||||
env->NewString((const jchar *)services[i].lpServiceName,
|
||||
lstrlen(services[i].lpServiceName));
|
||||
env->CallBooleanMethod(listobj, addid, name);
|
||||
}
|
||||
|
||||
CloseServiceHandle(handle);
|
||||
delete services;
|
||||
|
||||
return listobj;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package net.hyperic.sigar.win32;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Service extends Win32Bindings implements java.io.Serializable
|
||||
{
|
||||
// Service State
|
||||
|
@ -105,6 +107,8 @@ public class Service extends Win32Bindings implements java.io.Serializable
|
|||
Service.throwLastErrorException();
|
||||
}
|
||||
|
||||
public static native List getServiceNames() throws Win32Exception;
|
||||
|
||||
public Service(String serviceName) throws Win32Exception
|
||||
{
|
||||
this();
|
||||
|
@ -340,4 +344,11 @@ public class Service extends Win32Bindings implements java.io.Serializable
|
|||
private static final native int QueryServiceStartType(long handle);
|
||||
private static final native boolean StartService(long handle);
|
||||
private static final native boolean StopService(long handle);
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
List services = getServiceNames();
|
||||
for (int i=0; i<services.size(); i++) {
|
||||
System.out.println("'" + services.get(i) + "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue