Skip to content

Commit

Permalink
Merge pull request java-native-access#1053 from dbwiddis/master
Browse files Browse the repository at this point in the history
WinXP Compatibility for PdhUtil#PdhLookupPerfNameByIndex
  • Loading branch information
matthiasblaesing authored Jan 10, 2019
2 parents 1ce004b + fbcd7e2 commit 216cb34
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Features

Bug Fixes
---------
* [#1052](https://github.com/java-native-access/jna/issues/1052): WinXP compatibility for `c.s.j.p.win32.PdhUtil` - [@dbwiddis](https://github.com/dbwiddis).
* [#1052](https://github.com/java-native-access/jna/issues/1052), [#1053](https://github.com/java-native-access/jna/issues/1053): WinXP compatibility for `c.s.j.p.win32.PdhUtil` - [@dbwiddis](https://github.com/dbwiddis).

Release 5.2.0
=============
Expand Down
48 changes: 28 additions & 20 deletions contrib/platform/src/com/sun/jna/platform/win32/PdhUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,41 @@ public static String PdhLookupPerfNameByIndex(String szMachineName, int dwNameIn
// Call once with null buffer to get required buffer size
DWORDByReference pcchNameBufferSize = new DWORDByReference(new DWORD(0));
int result = Pdh.INSTANCE.PdhLookupPerfNameByIndex(szMachineName, dwNameIndex, null, pcchNameBufferSize);
// Windows XP requires a non-null buffer
if (result == PdhMsg.PDH_INVALID_ARGUMENT) {
pcchNameBufferSize = new DWORDByReference(new DWORD(1));
result = Pdh.INSTANCE.PdhLookupPerfNameByIndex(szMachineName, dwNameIndex, new Memory(1),
pcchNameBufferSize);
}
if (result != WinError.ERROR_SUCCESS && result != Pdh.PDH_MORE_DATA && result != Pdh.PDH_INSUFFICIENT_BUFFER) {
throw new PdhException(result);
}

// Can't allocate 0 memory
if (pcchNameBufferSize.getValue().intValue() < 1) {
return "";
Memory mem = null;
// Windows XP requires a non-null buffer and nonzero buffer size and
// will return PDH_INVALID_ARGUMENT.
if (result != PdhMsg.PDH_INVALID_ARGUMENT) {
// Vista+ branch: use returned buffer size for second query
if (result != WinError.ERROR_SUCCESS && result != Pdh.PDH_MORE_DATA) {
throw new PdhException(result);
}
// Can't allocate 0 memory
if (pcchNameBufferSize.getValue().intValue() < 1) {
return "";
}
// Allocate buffer and call again
mem = new Memory(pcchNameBufferSize.getValue().intValue() * CHAR_TO_BYTES);
result = Pdh.INSTANCE.PdhLookupPerfNameByIndex(szMachineName, dwNameIndex, mem, pcchNameBufferSize);
} else {
// XP branch: try increasing buffer sizes until successful
for (int bufferSize = 32; bufferSize <= Pdh.PDH_MAX_COUNTER_NAME; bufferSize *= 2) {
pcchNameBufferSize = new DWORDByReference(new DWORD(bufferSize));
mem = new Memory(bufferSize * CHAR_TO_BYTES);
result = Pdh.INSTANCE.PdhLookupPerfNameByIndex(szMachineName, dwNameIndex, mem, pcchNameBufferSize);
if (result != PdhMsg.PDH_INVALID_ARGUMENT && result != PdhMsg.PDH_INSUFFICIENT_BUFFER) {
break;
}
}
}
// Allocate buffer and call again
Memory mem = new Memory(pcchNameBufferSize.getValue().intValue() * CHAR_TO_BYTES);
result = Pdh.INSTANCE.PdhLookupPerfNameByIndex(szMachineName, dwNameIndex, mem, pcchNameBufferSize);

if(result != WinError.ERROR_SUCCESS) {
if (result != WinError.ERROR_SUCCESS) {
throw new PdhException(result);
}

// Convert buffer to Java String
if (CHAR_TO_BYTES == 1) {
return mem.getString(0);
return mem.getString(0); // NOSONAR squid:S2259
} else {
return mem.getWideString(0);
return mem.getWideString(0); // NOSONAR squid:S2259
}
}

Expand Down

0 comments on commit 216cb34

Please sign in to comment.