From eb1528f3f4e0bac215615ce86e61bcb22eb0cd84 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Sun, 12 Dec 2004 02:48:39 +0000 Subject: [PATCH] add Service.getServiceNames method --- bindings/java/src/jni/win32/service.cpp | 50 +++++++++++++++++++ .../src/net/hyperic/sigar/win32/Service.java | 13 ++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/bindings/java/src/jni/win32/service.cpp b/bindings/java/src/jni/win32/service.cpp index 18eb1fed..68f5350f 100644 --- a/bindings/java/src/jni/win32/service.cpp +++ b/bindings/java/src/jni/win32/service.cpp @@ -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, "", "()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; iNewString((const jchar *)services[i].lpServiceName, + lstrlen(services[i].lpServiceName)); + env->CallBooleanMethod(listobj, addid, name); + } + + CloseServiceHandle(handle); + delete services; + + return listobj; +} + #ifdef __cplusplus } #endif diff --git a/bindings/java/src/net/hyperic/sigar/win32/Service.java b/bindings/java/src/net/hyperic/sigar/win32/Service.java index a178d991..65776ecd 100644 --- a/bindings/java/src/net/hyperic/sigar/win32/Service.java +++ b/bindings/java/src/net/hyperic/sigar/win32/Service.java @@ -1,5 +1,7 @@ package net.hyperic.sigar.win32; +import java.util.List; + public class Service extends Win32Bindings implements java.io.Serializable { // Service State @@ -104,7 +106,9 @@ public class Service extends Win32Bindings implements java.io.Serializable if(this.m_hMgr == 0) 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