java - JNA Windows get process path -


i found code on stackoverflow explain how running process on windows, name , pid

kernel32 kernel32 = (kernel32) native.loadlibrary(kernel32.class, w32apioptions.unicode_options); tlhelp32.processentry32.byreference processentry = new tlhelp32.processentry32.byreference();  winnt.handle snapshot = kernel32.createtoolhelp32snapshot(tlhelp32.th32cs_snapprocess, new windef.dword(0)); try {     while (kernel32.process32next(snapshot, processentry)) {         system.out.println(processentry.th32processid + "\t" + native.tostring(processentry.szexefile));     } } {     kernel32.closehandle(snapshot); } 

my question is: how can process path?

using jna, need define moduleentry32 structure , map required functions :

import java.util.arrays; import java.util.list;  import com.sun.jna.native; import com.sun.jna.pointer; import com.sun.jna.structure; import com.sun.jna.platform.win32.kernel32; import com.sun.jna.platform.win32.windef; import com.sun.jna.win32.w32apioptions;  public interface processpathkernel32 extends kernel32 {     class moduleentry32 extends structure {         public static class byreference extends moduleentry32 implements structure.byreference {             public byreference() {             }              public byreference(pointer memory) {                 super(memory);             }         }         public moduleentry32() {             dwsize = new windef.dword(size());         }          public moduleentry32(pointer memory) {             super(memory);             read();         }           public dword dwsize;         public dword th32moduleid;         public dword th32processid;         public dword glblcntusage;         public dword proccntusage;         public pointer modbaseaddr;         public dword modbasesize;         public hmodule hmodule;         public char[] szmodule = new char[255+1]; // max_module_name32         public char[] szexepath = new char[max_path];         public string szmodule() { return native.tostring(this.szmodule); }         public string szexepath() { return native.tostring(this.szexepath); }         @override         protected list<string> getfieldorder() {             return arrays.aslist(new string[] {                 "dwsize", "th32moduleid", "th32processid", "glblcntusage", "proccntusage", "modbaseaddr", "modbasesize", "hmodule", "szmodule", "szexepath"             });         }     }      processpathkernel32 instance = (processpathkernel32)native.loadlibrary(processpathkernel32.class, w32apioptions.unicode_options);     boolean module32first(handle hsnapshot, moduleentry32.byreference lpme);     boolean module32next(handle hsnapshot, moduleentry32.byreference lpme); } 

then retrieve processes , each pid, retrieve module information (the path of module available). if running 32bits process module information 32bits processes (the path blank 64bits processes).

import com.sun.jna.native; import com.sun.jna.platform.win32.kernel32; import com.sun.jna.platform.win32.kernel32util; import com.sun.jna.platform.win32.tlhelp32; import com.sun.jna.platform.win32.windef; import com.sun.jna.platform.win32.winnt; import com.sun.jna.win32.w32apioptions;  public class processpathall {      public static void main(string ... args) {         kernel32 kernel32 = (kernel32) native.loadlibrary(kernel32.class, w32apioptions.default_options);         tlhelp32.processentry32.byreference processentry = new tlhelp32.processentry32.byreference();         winnt.handle processsnapshot =                  kernel32.createtoolhelp32snapshot(tlhelp32.th32cs_snapprocess, new windef.dword(0));         try {              while (kernel32.process32next(processsnapshot, processentry)) {                 // looks specific process                 // if (native.tostring(processentry.szexefile).equalsignorecase("textpad.exe")) {                 system.out.print(processentry.th32processid + "\t" + native.tostring(processentry.szexefile) + "\t");                 winnt.handle modulesnapshot =                      kernel32.createtoolhelp32snapshot(tlhelp32.th32cs_snapmodule, processentry.th32processid);                 try {                      processpathkernel32.moduleentry32.byreference me = new processpathkernel32.moduleentry32.byreference();                      processpathkernel32.instance.module32first(modulesnapshot, me);                      system.out.print(": " + me.szexepath() );                      system.out.println();                  }                  {                      kernel32.closehandle(modulesnapshot);                  }                 // }             }         }          {             kernel32.closehandle(processsnapshot);         }     } } 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -