2727import com .beaudoin .jmm .misc .Cacheable ;
2828import com .beaudoin .jmm .misc .MemoryBuffer ;
2929import com .beaudoin .jmm .natives .win32 .Kernel32 ;
30- import com .beaudoin .jmm .natives .win32 .Psapi ;
3130import com .beaudoin .jmm .process .Module ;
3231import com .beaudoin .jmm .process .NativeProcess ;
3332import com .sun .jna .Native ;
3433import com .sun .jna .Pointer ;
34+ import com .sun .jna .platform .win32 .Tlhelp32 ;
3535import com .sun .jna .platform .win32 .Win32Exception ;
3636
37+ import java .util .HashMap ;
3738import java .util .Map ;
3839
3940/**
@@ -43,7 +44,7 @@ public final class Win32Process implements NativeProcess {
4344
4445 private final int id ;
4546 private final Pointer handle ;
46- private Map <String , Module > modules ;
47+ private Map <String , Module > modules = new HashMap <>() ;
4748
4849 public Win32Process (int id , Pointer handle ) {
4950 this .id = id ;
@@ -62,7 +63,16 @@ public int id() {
6263
6364 @ Override
6465 public void initModules () {
65- modules = Psapi .getModules (this );
66+ Pointer snapshot = Kernel32 .CreateToolhelp32Snapshot (Tlhelp32 .TH32CS_SNAPMODULE32 .intValue () | Tlhelp32 .TH32CS_SNAPMODULE .intValue (), id ());
67+ Kernel32 .MODULEENTRY32W entry = new Kernel32 .MODULEENTRY32W .ByReference ();
68+ try {
69+ while (Kernel32 .Module32NextW (snapshot , entry )) {
70+ String name = entry .szModule ();
71+ modules .put (name , new Module (this , name , entry .getPointer (), entry .modBaseSize .intValue ()));
72+ }
73+ } finally {
74+ Kernel32 .CloseHandle (snapshot );
75+ }
6676 }
6777
6878 @ Override
0 commit comments