Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WinXP Compatibility for PdhUtil#PdhLookupPerfNameByIndex #1053

Merged
merged 2 commits into from
Jan 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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