Skip to content

Commit 2b8ca2d

Browse files
committed
Gives chance for process to load modules before giving up
1 parent 6415c31 commit 2b8ca2d

1 file changed

Lines changed: 58 additions & 42 deletions

File tree

src/main/java/com/beaudoin/jmm/process/impl/win32/Win32Process.java

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,55 +41,71 @@
4141
*/
4242
public final class Win32Process implements NativeProcess {
4343

44-
private final int id;
45-
private final Pointer handle;
46-
private Map<String, Module> modules;
44+
private final int id;
45+
private final Pointer handle;
46+
private Map<String, Module> modules;
4747

48-
public Win32Process(int id, Pointer handle) {
49-
this.id = id;
50-
this.handle = handle;
51-
initModules();
52-
}
48+
public Win32Process(int id, Pointer handle) {
49+
this.id = id;
50+
this.handle = handle;
51+
initModules();
52+
}
5353

54-
public Pointer pointer() {
55-
return handle;
56-
}
54+
public Pointer pointer() {
55+
return handle;
56+
}
5757

58-
@Override
59-
public int id() {
60-
return id;
61-
}
58+
@Override
59+
public int id() {
60+
return id;
61+
}
6262

63-
@Override
64-
public void initModules() {
65-
modules = Psapi.getModules(this);
66-
}
63+
@Override
64+
public void initModules() {
65+
System.out.println("hi");
66+
modules = Psapi.getModules(this);
67+
}
6768

68-
@Override
69-
public Module findModule(String moduleName) {
70-
return modules.get(moduleName);
71-
}
69+
@Override
70+
public Module findModule(String moduleName) {
71+
Module module = modules.get(moduleName);
72+
if (module == null) {
73+
int attempts = 60;
74+
for (; attempts-- > 0 && module == null; initModules()) {
75+
module = modules.get(moduleName);
76+
try {
77+
Thread.sleep(1000);
78+
} catch (InterruptedException e) {
79+
e.printStackTrace();
80+
}
81+
}
82+
if (module == null) {
83+
throw new RuntimeException(moduleName + " was not found!");
84+
}
85+
}
86+
return modules.get(moduleName);
87+
}
7288

73-
@Override
74-
public MemoryBuffer read(Pointer address, int size) {
75-
MemoryBuffer buffer = Cacheable.buffer(size);
76-
if (Kernel32.ReadProcessMemory(pointer(), address, buffer, size, 0) == 0) {
77-
throw new Win32Exception(Native.getLastError());
78-
}
79-
return buffer;
80-
}
89+
@Override
90+
public MemoryBuffer read(Pointer address, int size) {
91+
MemoryBuffer buffer = Cacheable.buffer(size);
92+
if (Kernel32.ReadProcessMemory(pointer(), address, buffer, size, 0) == 0) {
93+
throw new Win32Exception(Native.getLastError());
94+
}
95+
return buffer;
96+
}
8197

82-
@Override
83-
public NativeProcess write(Pointer address, MemoryBuffer buffer) {
84-
if (Kernel32.WriteProcessMemory(pointer(), address, buffer, buffer.size(), 0) == 0) {
85-
throw new Win32Exception(Native.getLastError());
86-
}
87-
return this;
88-
}
98+
@Override
99+
public NativeProcess write(Pointer address, MemoryBuffer buffer) {
100+
if (Kernel32.WriteProcessMemory(pointer(), address, buffer, buffer.size(), 0) == 0) {
101+
throw new Win32Exception(Native.getLastError());
102+
}
103+
return this;
104+
}
89105

90-
@Override
91-
public boolean canRead(Pointer address, int size) {
92-
return Kernel32.ReadProcessMemory(pointer(), address, Cacheable.buffer(size), size, 0) != 0;
93-
}
106+
@Override
107+
public boolean canRead(Pointer address, int size) {
108+
return Kernel32.ReadProcessMemory(pointer(), address, Cacheable.buffer(size), size, 0) != 0;
109+
}
94110

95111
}

0 commit comments

Comments
 (0)