Skip to content
This repository was archived by the owner on Jun 6, 2022. It is now read-only.

Commit 4064965

Browse files
author
adaei
committed
added helper method to get process id from window class/name
1 parent 0c6a193 commit 4064965

File tree

1 file changed

+42
-30
lines changed
  • java-win-memory-trainer/src/main/java/com/sprogcoder/memory

1 file changed

+42
-30
lines changed

java-win-memory-trainer/src/main/java/com/sprogcoder/memory/JTrainer.java

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,13 @@ public JTrainer(String windowClass, String windowText) throws WindowNotFoundExce
3636
{
3737
this.windowClass = windowClass;
3838
this.windowText = windowText;
39-
this.pid = JUser32.getWindowThreadProcessId(findWindow(windowClass, windowText));
40-
}
41-
42-
public int openProcess(int dwProcessId) throws MemoryException
43-
{
44-
int hProcess = JKernel32.openProcess(pid);
45-
46-
if (hProcess <= 0)
47-
{
48-
throw new MemoryException("OpenProcess", getLastError());
49-
}
50-
return hProcess;
39+
this.pid = getProcessIdFromWindow(windowClass, windowText);
5140
}
5241

5342
private HWND findWindow(String lpClassName, String lpWindowName) throws WindowNotFoundException
5443
{
5544
HWND hWnd = JUser32.findWindow(lpClassName, lpWindowName);
45+
5646
if (hWnd == null)
5747
{
5848
throw new WindowNotFoundException(lpClassName, lpWindowName);
@@ -61,17 +51,42 @@ private HWND findWindow(String lpClassName, String lpWindowName) throws WindowNo
6151
return hWnd;
6252
}
6353

64-
public void writeProcessMemory(int lpBaseAddress, int lpBuffer[]) throws MemoryException
54+
public String getLastError()
6555
{
66-
int hProcess = openProcess(pid);
67-
boolean success = JKernel32.writeProcessMemory(hProcess, lpBaseAddress, lpBuffer);
56+
return JKernel32.getLastError();
57+
}
6858

69-
if (!success)
59+
private int getProcessIdFromWindow(String lpClassName, String lpWindowName) throws WindowNotFoundException
60+
{
61+
return getWindowTheadProcessId(findWindow(windowClass, windowText));
62+
}
63+
64+
public int getWindowTheadProcessId(HWND hWnd)
65+
{
66+
return JUser32.getWindowThreadProcessId(hWnd);
67+
}
68+
69+
public boolean isProcessAvailable()
70+
{
71+
int hProcess = JKernel32.openProcess(pid);
72+
return (hProcess > 0);
73+
}
74+
75+
public void retryProcess() throws WindowNotFoundException
76+
{
77+
this.pid = getProcessIdFromWindow(windowClass, windowText);
78+
}
79+
80+
public int openProcess(int dwProcessId) throws MemoryException
81+
{
82+
int hProcess = JKernel32.openProcess(pid);
83+
84+
if (hProcess <= 0)
7085
{
71-
throw new MemoryException("WriteProcessMemory", getLastError());
86+
throw new MemoryException("OpenProcess", getLastError());
7287
}
73-
74-
JKernel32.closeHandle(hProcess);
88+
89+
return hProcess;
7590
}
7691

7792
public byte[] readProcessMemory(int lpBaseAddress, int nSize) throws MemoryException
@@ -89,20 +104,17 @@ public byte[] readProcessMemory(int lpBaseAddress, int nSize) throws MemoryExcep
89104
return result;
90105
}
91106

92-
public String getLastError()
107+
public void writeProcessMemory(int lpBaseAddress, int lpBuffer[]) throws MemoryException
93108
{
94-
return JKernel32.getLastError();
95-
}
109+
int hProcess = openProcess(pid);
110+
boolean success = JKernel32.writeProcessMemory(hProcess, lpBaseAddress, lpBuffer);
96111

97-
public void retryProcess() throws WindowNotFoundException
98-
{
99-
this.pid = JUser32.getWindowThreadProcessId(JUser32.findWindow(windowClass, windowText));
100-
}
112+
if (!success)
113+
{
114+
throw new MemoryException("WriteProcessMemory", getLastError());
115+
}
101116

102-
public boolean isProcessAvailable()
103-
{
104-
int hProcess = JKernel32.openProcess(pid);
105-
return (hProcess > 0);
117+
JKernel32.closeHandle(hProcess);
106118
}
107119

108120
}

0 commit comments

Comments
 (0)