@@ -36,23 +36,13 @@ public JTrainer(String windowClass, String windowText) throws WindowNotFoundExce
36
36
{
37
37
this .windowClass = windowClass ;
38
38
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 );
51
40
}
52
41
53
42
private HWND findWindow (String lpClassName , String lpWindowName ) throws WindowNotFoundException
54
43
{
55
44
HWND hWnd = JUser32 .findWindow (lpClassName , lpWindowName );
45
+
56
46
if (hWnd == null )
57
47
{
58
48
throw new WindowNotFoundException (lpClassName , lpWindowName );
@@ -61,17 +51,42 @@ private HWND findWindow(String lpClassName, String lpWindowName) throws WindowNo
61
51
return hWnd ;
62
52
}
63
53
64
- public void writeProcessMemory ( int lpBaseAddress , int lpBuffer []) throws MemoryException
54
+ public String getLastError ()
65
55
{
66
- int hProcess = openProcess ( pid );
67
- boolean success = JKernel32 . writeProcessMemory ( hProcess , lpBaseAddress , lpBuffer );
56
+ return JKernel32 . getLastError ( );
57
+ }
68
58
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 )
70
85
{
71
- throw new MemoryException ("WriteProcessMemory " , getLastError ());
86
+ throw new MemoryException ("OpenProcess " , getLastError ());
72
87
}
73
-
74
- JKernel32 . closeHandle ( hProcess ) ;
88
+
89
+ return hProcess ;
75
90
}
76
91
77
92
public byte [] readProcessMemory (int lpBaseAddress , int nSize ) throws MemoryException
@@ -89,20 +104,17 @@ public byte[] readProcessMemory(int lpBaseAddress, int nSize) throws MemoryExcep
89
104
return result ;
90
105
}
91
106
92
- public String getLastError ()
107
+ public void writeProcessMemory ( int lpBaseAddress , int lpBuffer []) throws MemoryException
93
108
{
94
- return JKernel32 . getLastError ( );
95
- }
109
+ int hProcess = openProcess ( pid );
110
+ boolean success = JKernel32 . writeProcessMemory ( hProcess , lpBaseAddress , lpBuffer );
96
111
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
+ }
101
116
102
- public boolean isProcessAvailable ()
103
- {
104
- int hProcess = JKernel32 .openProcess (pid );
105
- return (hProcess > 0 );
117
+ JKernel32 .closeHandle (hProcess );
106
118
}
107
119
108
120
}
0 commit comments