VOID PrintObjects() { LPWSTR szDataSource = NULL, szMachineName = NULL, mszObjectList = NULL; DWORD dwBufferLength = 0; PDH_STATUS status = PdhEnumObjects(szDataSource, szMachineName, mszObjectList, &dwBufferLength, PERF_DETAIL_WIZARD, FALSE); //HEX HEX! Only a Magicians gets all the info he wants, and only Microsoft knows what that means if (status != PDH_MORE_DATA) goto die; mszObjectList = new WCHAR[dwBufferLength + 2]; status = PdhEnumObjects(szDataSource, szMachineName, mszObjectList, &dwBufferLength, PERF_DETAIL_WIZARD, FALSE); if (FAILED(status)) goto die; DWORD c = 0; while (++c < dwBufferLength) { if (mszObjectList[c] == '\0') std::wcout << '\n'; else std::wcout << mszObjectList[c]; } delete[]mszObjectList; return; die: FormatPDHError(status); delete[]mszObjectList; }
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; }
JNIEXPORT jobjectArray SIGAR_JNI(win32_Pdh_pdhGetObjects) (JNIEnv *env, jclass cur) { PDH_STATUS status; DWORD list_size = 8096; LPTSTR list_buf = (LPTSTR)malloc(list_size * sizeof(TCHAR)); LPTSTR cur_object; DWORD i, num_objects = 0; jobjectArray array = NULL; status = PdhEnumObjects(NULL, NULL, list_buf, &list_size, PERF_DETAIL_WIZARD, FALSE); if (status == PDH_MORE_DATA) { // Re-try call with a larger buffer if (list_buf != NULL) free(list_buf); list_buf = (LPTSTR)malloc(list_size * sizeof(TCHAR)); status = PdhEnumObjects(NULL, NULL, list_buf, &list_size, PERF_DETAIL_WIZARD, FALSE); } if (status != ERROR_SUCCESS) { if (list_buf != NULL) free(list_buf); win32_throw_exception(env, get_error_message(status)); return NULL; } // Walk the return buffer counting the number of objects for (cur_object = list_buf, num_objects = 0; *cur_object != 0; cur_object += lstrlen(cur_object) + 1, num_objects++); array = JENV->NewObjectArray(env, num_objects, JENV->FindClass(env, "java/lang/String"), JENV->NewStringUTF(env, "")); if (JENV->ExceptionCheck(env)) { free(list_buf); return NULL; } for (cur_object = list_buf, i = 0; *cur_object != 0; i++) { int len = lstrlen(cur_object); jstring s = JENV->NewString(env, (const jchar *)cur_object, len); JENV->SetObjectArrayElement(env, array, i, s); if (JENV->ExceptionCheck(env)) { free(list_buf); return NULL; } cur_object += len + 1; } if (list_buf != NULL) free(list_buf); return array; }