./src/windows/native/java/lang/ProcessImpl_md.c

Print this page
rev 5754 : [mq]: getPid-patch

@@ -30,10 +30,12 @@
 #include "jvm.h"
 #include "jni_util.h"
 #include "io_util.h"
 #include <windows.h>
 #include <io.h>
+#include <tchar.h>
+#include <psapi.h>
 
 /* We try to make sure that we can read and write 4095 bytes (the
  * fixed limit on Linux) to the pipe on all operating systems without
  * deadlock.  Windows 2000 inexplicably appears to need an extra 24
  * bytes of slop to avoid deadlock.

@@ -388,5 +390,45 @@
     if (h == INVALID_HANDLE_VALUE) {
         JNU_ThrowIOExceptionWithLastError(env, "CreateFileW");
     }
     return ptr_to_jlong(h);
 }
+
+JNIEXPORT jint JNICALL
+Java_java_lang_ProcessImpl_getProcessId(JNIEnv *env, jclass ignored, jlong handle)
+{
+    return  GetProcessId((HANDLE) handle);
+}
+
+JNIEXPORT jint JNICALL
+Java_java_lang_ProcessImpl_getCurrentPid(JNIEnv *env, jobject this)
+{
+    return  GetCurrentProcessId();
+}
+
+
+JNIEXPORT jstring JNICALL
+Java_java_lang_ProcessImpl_getModuleBaseName(JNIEnv *env, jclass ignored, jint pid)
+{
+    char szProcessName[MAX_PATH];
+
+    // Get a handle to the process.
+
+    HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
+                                   PROCESS_VM_READ,
+                                   FALSE, pid );
+
+    // Get the process name.
+
+    if (hProcess != NULL )
+    {
+        HMODULE hMod;
+        DWORD cbNeeded;
+
+        if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
+             &cbNeeded) ) {
+            GetModuleBaseName( hProcess, hMod, szProcessName, 
+                               sizeof(szProcessName) );
+        }
+    }
+    return szProcessName;
+}