Skip to content

Commit

Permalink
Fix getProcessFilePath for querying 64 bit processes from 32bit JVMs
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasblaesing committed Dec 3, 2020
1 parent 46a7de6 commit 3cbe93a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Bug Fixes
* [#1244](https://github.com/java-native-access/jna/issues/1244): Fix building on GCC 10 - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#1252](https://github.com/java-native-access/jna/issues/1252): - Fix bindings of `CTL_ENTRY#getRgAttribute`, `CTL_INFO#getRgCTLEntry`, `CTL_INFO#getRgExtension`, `CERT_EXTENSIONS#getRgExtension`, `CERT_INFO#getRgExtension`, `CRL_INFO#getRgCRLEntry`, `CRL_INFO#getRgExtension`, `CRL_ENTRY#getRgExtension`. Add bindings for `CertEnumCertificatesInStore`, `CertEnumCTLsInStore`, `CertEnumCRLsInStore` and `CryptQueryObject` in `c.s.j.p.win32.Crypt32`.<br> *WARNING:* The signatures for `CTL_INFO#getRgCTLEntry` and `CTL_INFO#getRgExtension` were changed - as the original signatures were obviously wrong and read the wrong attributes, it is not considered an API break - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#1275](https://github.com/java-native-access/jna/issues/1275): Fix `CFStringRef#stringValue` for empty Strings - [@dyorgio](https://github.com/dyorgio).
* [#1278](https://github.com/java-native-access/jna/pull/1278): Improve compatibility of `c.s.j.p.WindowUtils#getProcessFilePath` and fix unittests for windows 32bit intel - [@matthiasblaesing](https://github.com/matthiasblaesing).

Release 5.6.0
=============
Expand Down
33 changes: 30 additions & 3 deletions contrib/platform/src/com/sun/jna/platform/WindowUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1297,11 +1297,38 @@ public String getProcessFilePath(final HWND hwnd) {
final int length = Psapi.INSTANCE.GetModuleFileNameExW(process,
null, filePath, filePath.length);
if (length == 0) {
if(Kernel32.INSTANCE.GetLastError() != WinNT.ERROR_INVALID_HANDLE) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
} else {
int lastError = Kernel32.INSTANCE.GetLastError();
if(lastError == WinNT.ERROR_INVALID_HANDLE) {
// ignore invalid handles
return "";
} else if (lastError == WinNT.ERROR_PARTIAL_COPY) {
// observed on 64 platforms when the JVM running the JNA
// code is 32 bit and the target process is 64 bit.
// Fallback to GetProcessImageFileName. But the returned
// path is in device form, so this needs to be mapped
// back to the drive form
final int length2 = Psapi.INSTANCE.GetProcessImageFileName(process,
filePath, filePath.length);
if (length2 == 0) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
String processImagePath = Native.toString(filePath);
char[] pathPrefixBuffer = new char[1024];
// Map DOS Drives to drive form paths
for (char i = 'A'; i <= 'Z'; i++) {
String drive = i + ":";
int ret = Kernel32.INSTANCE.QueryDosDevice(drive, pathPrefixBuffer, 1024);
if (ret != 0) {
String pathPrefix = Native.toString(pathPrefixBuffer);
if (processImagePath.startsWith(pathPrefix)) {
processImagePath = drive + processImagePath.substring(pathPrefix.length());
break;
}
}
}
return processImagePath;
} else {
throw new Win32Exception(lastError);
}
}
return Native.toString(filePath).trim();
Expand Down

0 comments on commit 3cbe93a

Please sign in to comment.