Skip to content

Commit

Permalink
Merge branch 'Honeyfy-539-adding-win32-function-QueryFullProcessImage…
Browse files Browse the repository at this point in the history
…Name'
  • Loading branch information
twall committed Dec 4, 2015
2 parents de5316d + ac765cb commit e002377
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Features
* [#535](https://github.com/java-native-access/jna/pull/535): Added `BitBlt` to `com.sun.jna.platform.win32.GDI32`, Added `com.sun.jna.platform.win32.GDI32Util` and added `getScreenshot()` to it - [@mlfreeman2](https://github.com/mlfreeman2).
* [#535](https://github.com/java-native-access/jna/pull/535): Added `SHEmptyRecycleBin`, `ShellExecuteEx` to `com.sun.jna.platform.win32.Shell32` - [@mlfreeman2](https://github.com/mlfreeman2).
* [#535](https://github.com/java-native-access/jna/pull/535): Added `GetDesktopWindow` to `com.sun.jna.platform.win32.User32` - [@mlfreeman2](https://github.com/mlfreeman2).
* [#540](https://github.com/java-native-access/jna/pull/539): Added Missing Windows kernel32 method: QueryFullProcessImageName - [@yossieilaty](https://github.com/yossieilaty).
* [#543](https://github.com/java-native-access/jna/pull/543): Added `ProcessIdToSessionId`, `LoadLibraryEx`, `FreeLibrary` and `Find/Load/Lock/SizeofResource` to `com.sun.jna.platform.win32.Kernel32` - [@mlfreeman2](https://github.com/mlfreeman2).
* [#545](https://github.com/java-native-access/jna/pull/545): Added `EnumResourceTypes` and `EnumResourceNames` to `com.sun.jna.platform.win32.Kernel32` - [@mlfreeman2](https://github.com/mlfreeman2).
* [#547](https://github.com/java-native-access/jna/pull/547): Added `GetSystemTimes` to `com.sun.jna.platform.win32.Kernel32` - [@dbwiddis](https://github.com/dbwiddis).
Expand Down
20 changes: 20 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,26 @@ boolean CreateProcessW(String lpApplicationName, char[] lpCommandLine,
* GetLastError.
*/
HANDLE OpenProcess(int fdwAccess, boolean fInherit, int IDProcess);

/**
* This function retrieves the full path of the executable file of a given process.
*
* @param hProcess
* Handle for the running process
* @param dwFlags
* 0 - The name should use the Win32 path format.
* 1(WinNT.PROCESS_NAME_NATIVE) - The name should use the native system path format.
* @param lpExeName
* pre-allocated character buffer for the returned path
* @param lpdwSize
* input: the size of the allocated buffer
* output: the length of the returned path in characters
*
* @return true if successful false if not. To get extended error information,
* call GetLastError.
*/
boolean QueryFullProcessImageName(HANDLE hProcess, int dwFlags, char[] lpExeName, IntByReference lpdwSize);


/**
* The GetTempPath function retrieves the path of the directory designated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,27 @@ public static final String extractVolumeGUID(String volumeGUIDPath) {

return volumeGUIDPath.substring(VOLUME_GUID_PATH_PREFIX.length(), volumeGUIDPath.length() - VOLUME_GUID_PATH_SUFFIX.length());
}

/**
*
* This function retrieves the full path of the executable file of a given process.
*
* @param hProcess
* Handle for the running process
* @param dwFlags
* 0 - The name should use the Win32 path format.
* 1(WinNT.PROCESS_NAME_NATIVE) - The name should use the native system path format.
*
* @return the full path of the process's executable file of null if failed. To get extended error information,
* call GetLastError.
*/
public static final String QueryFullProcessImageName(HANDLE hProcess, int dwFlags) {
char[] path = new char[WinDef.MAX_PATH];
IntByReference lpdwSize = new IntByReference(path.length);
if (Kernel32.INSTANCE.QueryFullProcessImageName(hProcess, 0, path, lpdwSize))
return new String(path).substring(0, lpdwSize.getValue());
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}

/**
* Gets the specified resource out of the specified executable file
Expand Down Expand Up @@ -874,5 +895,4 @@ public boolean invoke(HMODULE module, Pointer type, Pointer name, Pointer lParam
}
return result;
}

}
6 changes: 6 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/WinNT.java
Original file line number Diff line number Diff line change
Expand Up @@ -2177,6 +2177,12 @@ public EVENTLOGRECORD(Pointer p) {
*/
int PROCESS_TERMINATE = 0x00000001;

/**
* Required for getting process exe path in native system path format
* {@code Kernel32.QueryFullProcessImageName()}.
*/
int PROCESS_NAME_NATIVE = 0x00000001;

/**
* Required to perform an operation on the address space of a process (see
* {@code Kernel32.VirtualProtectEx()} and
Expand Down
11 changes: 11 additions & 0 deletions contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,17 @@ public void testOpenProcess() {
assertEquals(WinError.ERROR_ACCESS_DENIED, Kernel32.INSTANCE.GetLastError());
}

public void testQueryFullProcessImageName() {
HANDLE h = Kernel32.INSTANCE.OpenProcess(0, false, Kernel32.INSTANCE.GetCurrentProcessId());
assertNotNull("Failed (" + Kernel32.INSTANCE.GetLastError() + ") to get process handle", h);

char[] path = new char[WinDef.MAX_PATH];
IntByReference lpdwSize = new IntByReference(path.length);
boolean b = Kernel32.INSTANCE.QueryFullProcessImageName(h, 0, path, lpdwSize);
assertTrue("Failed (" + Kernel32.INSTANCE.GetLastError() + ") to query process image name", b);
assertTrue("Failed to query process image name, empty path returned", lpdwSize.getValue() > 0);
}

public void testGetTempPath() {
char[] buffer = new char[WinDef.MAX_PATH];
assertTrue(Kernel32.INSTANCE.GetTempPath(new DWORD(WinDef.MAX_PATH), buffer).intValue() > 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import java.util.List;
import java.util.Map;

import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.platform.win32.WinNT.LARGE_INTEGER;
import com.sun.jna.ptr.IntByReference;

import junit.framework.TestCase;

Expand Down Expand Up @@ -256,6 +258,14 @@ public final void testWritePrivateProfileSection() throws IOException {
}
}

public final void testQueryFullProcessImageName() {
HANDLE h = Kernel32.INSTANCE.OpenProcess(0, false, Kernel32.INSTANCE.GetCurrentProcessId());
assertNotNull("Failed (" + Kernel32.INSTANCE.GetLastError() + ") to get process handle", h);

String name = Kernel32Util.QueryFullProcessImageName(h, 0);
assertTrue("Failed to query process image name, empty path returned", name.length() > 0);
}

public void testGetResource() {
String winDir = Kernel32Util.getEnvironmentVariable("WINDIR");
assertNotNull("No WINDIR value returned", winDir);
Expand Down

0 comments on commit e002377

Please sign in to comment.