use EnumServicesStatus wrapper
This commit is contained in:
		
							parent
							
								
									ee41ce16d2
								
							
						
					
					
						commit
						4971327ef1
					
				@ -4,6 +4,9 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#include "win32bindings.h"
 | 
					#include "win32bindings.h"
 | 
				
			||||||
#include "javasigar.h"
 | 
					#include "javasigar.h"
 | 
				
			||||||
 | 
					#include "sigar.h"
 | 
				
			||||||
 | 
					#include "sigar_private.h"
 | 
				
			||||||
 | 
					#include "sigar_os.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef __cplusplus
 | 
					#ifdef __cplusplus
 | 
				
			||||||
extern "C" {
 | 
					extern "C" {
 | 
				
			||||||
@ -218,12 +221,8 @@ SIGAR_JNI(win32_Service_QueryServiceStatus)
 | 
				
			|||||||
JNIEXPORT jobject SIGAR_JNI(win32_Service_getServiceNames)
 | 
					JNIEXPORT jobject SIGAR_JNI(win32_Service_getServiceNames)
 | 
				
			||||||
(JNIEnv *env, jclass)
 | 
					(JNIEnv *env, jclass)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    SC_HANDLE handle =
 | 
					    DWORD i, status;
 | 
				
			||||||
        OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
 | 
					    sigar_services_status_t ss;
 | 
				
			||||||
    ENUM_SERVICE_STATUS status, *services;
 | 
					 | 
				
			||||||
    BOOL retval;
 | 
					 | 
				
			||||||
    DWORD bytes, count, resume=0;
 | 
					 | 
				
			||||||
    DWORD type = SERVICE_WIN32, state = SERVICE_STATE_ALL;
 | 
					 | 
				
			||||||
    jobject listobj;
 | 
					    jobject listobj;
 | 
				
			||||||
    jclass listclass =
 | 
					    jclass listclass =
 | 
				
			||||||
        env->FindClass("java/util/ArrayList");
 | 
					        env->FindClass("java/util/ArrayList");
 | 
				
			||||||
@ -233,34 +232,22 @@ JNIEXPORT jobject SIGAR_JNI(win32_Service_getServiceNames)
 | 
				
			|||||||
        env->GetMethodID(listclass, "add",
 | 
					        env->GetMethodID(listclass, "add",
 | 
				
			||||||
                         "(Ljava/lang/Object;)"
 | 
					                         "(Ljava/lang/Object;)"
 | 
				
			||||||
                         "Z");
 | 
					                         "Z");
 | 
				
			||||||
 | 
					    SIGAR_ZERO(&ss);
 | 
				
			||||||
    if (handle == NULL) {
 | 
					    status = sigar_services_status_get(&ss, SERVICE_STATE_ALL);
 | 
				
			||||||
 | 
					    if (status != SIGAR_OK) {
 | 
				
			||||||
 | 
					        win32_throw_error(env, status);
 | 
				
			||||||
        return 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);
 | 
					    listobj = env->NewObject(listclass, listid);
 | 
				
			||||||
    for (int i=0; i<count; i++) {
 | 
					    for (i=0; i<ss.count; i++) {
 | 
				
			||||||
        jstring name = 
 | 
					        jstring name =
 | 
				
			||||||
            env->NewString((const jchar *)services[i].lpServiceName,
 | 
					            env->NewStringUTF((char *)ss.services[i].lpServiceName);
 | 
				
			||||||
                           lstrlen(services[i].lpServiceName));
 | 
					
 | 
				
			||||||
        env->CallBooleanMethod(listobj, addid, name);
 | 
					        env->CallBooleanMethod(listobj, addid, name);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    CloseServiceHandle(handle);
 | 
					    sigar_services_status_close(&ss);
 | 
				
			||||||
    delete services;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return listobj;
 | 
					    return listobj;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user