Skip to content

Commit

Permalink
Merge pull request #53 from stephanreiter/long_handles
Browse files Browse the repository at this point in the history
Use jlong for HANDLE and GetProcessId instead of Nt kernel private fu…
  • Loading branch information
oleg-nenashev authored Jul 14, 2018
2 parents a9e44d3 + 880c9c3 commit a7da800
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 20 deletions.
22 changes: 7 additions & 15 deletions native/envvar-cmdline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,22 +312,14 @@ jstring getCmdLineAndEnvVars(
return packedStr;
}

JNIEXPORT jint JNICALL Java_org_jvnet_winp_Native_getProcessId(JNIEnv* pEnv, jclass clazz, jint handle) {
JNIEXPORT jint JNICALL Java_org_jvnet_winp_Native_getProcessId(JNIEnv* pEnv, jclass clazz, jlong handle) {
char errorBuffer[ERRMSG_SIZE];
HANDLE hProcess = (HANDLE)handle;
PROCESS_BASIC_INFORMATION ProcInfo;
SIZE_T sRead;
if(!NT_SUCCESS(ZwQueryInformationProcess(hProcess, ProcessBasicInformation, &ProcInfo, sizeof(ProcInfo), &sRead))) {
reportError(pEnv,"Failed to ZWQueryInformationProcess");
DWORD pid = GetProcessId(hProcess);
if (!pid) {
sprintf_s<ERRMSG_SIZE>(errorBuffer, "Failed to get process id from handle=%ld.", handle);
reportError(pEnv, errorBuffer);
return -1;
}

return (jint)ProcInfo.UniqueProcessId;
/* ULONG id=0;
if(!ReadProcessMemory(hProcess, ProcInfo.UniqueProcessId, &id, sizeof(ULONG), &_)) {
reportError(pEnv,"Failed to read process ID");
return NULL;
}
return id;*/
return pid;
}
4 changes: 2 additions & 2 deletions native/java-interface.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion native/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// are changed infrequently
#pragma once

#define _WIN32_WINNT 0x500
#define _WIN32_WINNT 0x501

#include <windows.h>
#include <tchar.h>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jvnet/winp/Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Native {
native static boolean isCriticalProcess(int pid);
native static boolean isProcessRunning(int pid);
native static int setPriority(int pid, int value);
native static int getProcessId(int handle);
native static int getProcessId(long handle);
native static boolean exitWindowsEx(int flags,int reasonCode);

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jvnet/winp/WinProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public WinProcess(Process proc) {
try {
Field f = proc.getClass().getDeclaredField("handle");
f.setAccessible(true);
int handle = ((Number)f.get(proc)).intValue();
long handle = f.getLong(proc);
pid = Native.getProcessId(handle);
} catch (NoSuchFieldException e) {
throw new NotWindowsException(e);
Expand Down

0 comments on commit a7da800

Please sign in to comment.