JNIEXPORT jint JNICALL Java_org_krakenapps_winapi_PerformanceCounter_addCounter(JNIEnv *env, jobject obj, jint queryHandle, jstring category, jstring counter, jstring instance, jstring machine) { PDH_HCOUNTER phCounter = NULL; PDH_HQUERY phQuery = (PDH_HQUERY)queryHandle; LPTSTR counterPath = NULL; PDH_COUNTER_PATH_ELEMENTS pathElement; DWORD dwSize = 0; PDH_STATUS stat = 0; jboolean isCopy = JNI_FALSE; memset(&pathElement, 0, sizeof(pathElement)); pathElement.szObjectName = category ? (LPTSTR)(*env)->GetStringChars(env, category, &isCopy) : NULL; pathElement.szCounterName = counter ? (LPTSTR)(*env)->GetStringChars(env, counter, &isCopy) : NULL; pathElement.szInstanceName = instance ? (LPTSTR)(*env)->GetStringChars(env, instance, &isCopy) : NULL; pathElement.szMachineName = machine ? (LPTSTR)(*env)->GetStringChars(env, machine, &isCopy) : NULL; if(pathElement.szMachineName) { stat = PdhConnectMachine(pathElement.szMachineName); if(stat != ERROR_SUCCESS) { fprintf(stderr, "Error in PdhConnectMachine:, 0x%x\n", stat); if(pathElement.szMachineName) (*env)->ReleaseStringChars(env, category, pathElement.szObjectName); (*env)->ReleaseStringChars(env, counter, pathElement.szCounterName); (*env)->ReleaseStringChars(env, instance, pathElement.szInstanceName); (*env)->ReleaseStringChars(env, machine, pathElement.szMachineName); return 0; } } PdhMakeCounterPath(&pathElement, NULL, &dwSize, 0); if(dwSize == 0) { fprintf(stderr, "Error in PdhMakeCounterPath\n"); (*env)->ReleaseStringChars(env, category, pathElement.szObjectName); (*env)->ReleaseStringChars(env, counter, pathElement.szCounterName); (*env)->ReleaseStringChars(env, instance, pathElement.szInstanceName); (*env)->ReleaseStringChars(env, machine, pathElement.szMachineName); return 0; } counterPath = (LPTSTR)malloc(sizeof(TCHAR)*dwSize); stat = PdhMakeCounterPath(&pathElement, counterPath, &dwSize, 0); (*env)->ReleaseStringChars(env, category, pathElement.szObjectName); (*env)->ReleaseStringChars(env, counter, pathElement.szCounterName); (*env)->ReleaseStringChars(env, instance, pathElement.szInstanceName); (*env)->ReleaseStringChars(env, machine, pathElement.szMachineName); if(stat != ERROR_SUCCESS) { fprintf(stderr, "Error in PdhMakeCounterPath: 0x%x\n", stat); return 0; } stat = PdhAddCounter(phQuery, counterPath, 0, &phCounter); if(stat != ERROR_SUCCESS) { fprintf(stderr, "Error in PdhAddCounter: 0x%x\n", stat); return 0; } free(counterPath); PdhCollectQueryData(phQuery); return (jint)phCounter; }
JNIEXPORT void SIGAR_JNI(win32_Pdh_pdhConnectMachine) (JNIEnv *env, jobject cur, jstring jhost) { PDH_STATUS status; LPCTSTR host = JENV->GetStringChars(env, jhost, NULL); status = PdhConnectMachine(host); JENV->ReleaseStringChars(env, jhost, host); if (status != ERROR_SUCCESS) { win32_throw_exception(env, get_error_message(status)); } }
JNIEXPORT jobject JNICALL Java_org_krakenapps_winapi_PerformanceCounter_getCounters(JNIEnv *env, jobject obj, jstring category, jstring machine, jint detail) { jclass clzHashMap = (*env)->FindClass(env, "java/util/HashMap"); jmethodID hashMapInit = (*env)->GetMethodID(env, clzHashMap, "<init>", "()V"); jmethodID hashMapPut = (*env)->GetMethodID(env, clzHashMap, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); jobject counter = NULL; LPTSTR categoryName = (LPTSTR)(*env)->GetStringChars(env, category, JNI_FALSE); LPTSTR machineName = machine ? (LPTSTR)(*env)->GetStringChars(env, machine, JNI_FALSE) : NULL; jobjectArray counters = NULL; jobjectArray instances = NULL; LPTSTR lpCounterList = NULL; DWORD dwCounterListLength = 0; LPTSTR lpInstanceList = NULL; DWORD dwInstanceListLength = 0; DWORD stat = 0; if(machineName) { stat = PdhConnectMachine(machineName); if(stat != ERROR_SUCCESS) { fprintf(stderr, "Error in PdhConnectMachine:, 0x%x\n", stat); if(machineName) (*env)->ReleaseStringChars(env, machine, machineName); return NULL; } } PdhEnumObjectItems(NULL, machineName, categoryName, NULL, &dwCounterListLength, NULL, &dwInstanceListLength, detail, 0); lpCounterList = (LPTSTR)malloc(sizeof(TCHAR)*dwCounterListLength); lpInstanceList = (LPTSTR)malloc(sizeof(TCHAR)*dwInstanceListLength); stat = PdhEnumObjectItems(NULL, machineName, categoryName, lpCounterList, &dwCounterListLength, lpInstanceList, &dwInstanceListLength, detail, 0); (*env)->ReleaseStringChars(env, category, categoryName); if(machineName) (*env)->ReleaseStringChars(env, machine, machineName); if(stat != ERROR_SUCCESS) { fprintf(stderr, "Error in PdhEnumObjectItems\n"); return NULL; } counter = counter = (*env)->NewObject(env, clzHashMap, hashMapInit); counters = convertStringArray(env, lpCounterList, dwCounterListLength); instances = convertStringArray(env, lpInstanceList, dwInstanceListLength); free(lpCounterList); free(lpInstanceList); (*env)->CallObjectMethod(env, counter, hashMapPut, (*env)->NewStringUTF(env, "counters"), counters); (*env)->CallObjectMethod(env, counter, hashMapPut, (*env)->NewStringUTF(env, "instances"), instances); return counter; }
JNIEXPORT jobjectArray JNICALL Java_org_krakenapps_winapi_PerformanceCounter_getCategories(JNIEnv *env, jobject obj, jstring machine, jint detail) { jobjectArray categoryList = NULL; LPTSTR machineName = machine ? (LPTSTR)(*env)->GetStringChars(env, machine, JNI_FALSE) : NULL; LPTSTR lpCategoryNameList = NULL; DWORD dwBufferLength = 0; PDH_STATUS stat = 0; if(machineName) { stat = PdhConnectMachine(machineName); if(stat != ERROR_SUCCESS) { fprintf(stderr, "Error in PdhConnectMachine:, 0x%x\n", stat); if(machineName) (*env)->ReleaseStringChars(env, machine, machineName); return NULL; } } PdhEnumObjects(NULL, machineName, NULL, &dwBufferLength, detail, TRUE); if(dwBufferLength == 0) { fprintf(stderr, "Error in PdhEnumObjects\n"); return NULL; } lpCategoryNameList = (LPTSTR)malloc(sizeof(TCHAR)*dwBufferLength); stat = PdhEnumObjects(NULL, machineName, lpCategoryNameList, &dwBufferLength, detail, TRUE); if(stat != ERROR_SUCCESS) { fprintf(stderr, "Error in PdhEnumObjects: 0x%x\n", stat); return NULL; } if(machineName) (*env)->ReleaseStringChars(env, machine, machineName); categoryList = convertStringArray(env, lpCategoryNameList, dwBufferLength); free(lpCategoryNameList); return categoryList; }