|
28 | 28 | import com.sun.jna.Native; |
29 | 29 | import com.sun.jna.NativeLibrary; |
30 | 30 | import com.sun.jna.Pointer; |
| 31 | +import com.sun.jna.Structure; |
31 | 32 | import com.sun.jna.platform.win32.Tlhelp32; |
32 | 33 | import com.sun.jna.platform.win32.WinDef; |
33 | | -import com.sun.jna.ptr.IntByReference; |
34 | 34 | import com.sun.jna.win32.W32APIOptions; |
35 | 35 |
|
| 36 | +import java.util.Arrays; |
| 37 | +import java.util.List; |
| 38 | + |
36 | 39 | public final class Kernel32 { |
37 | 40 |
|
38 | 41 | static { |
39 | 42 | Native.register(NativeLibrary.getInstance("Kernel32", W32APIOptions.UNICODE_OPTIONS)); |
40 | 43 | } |
41 | 44 |
|
42 | | - public static native Pointer CreateToolhelp32Snapshot(WinDef.DWORD dword, int junk); |
| 45 | + public static native Pointer CreateToolhelp32Snapshot(int flags, int pid); |
43 | 46 |
|
44 | 47 | public static native boolean CloseHandle(Pointer pointer); |
45 | 48 |
|
46 | 49 | public static native Pointer OpenProcess(int desired, boolean inherit, int pid); |
47 | 50 |
|
48 | 51 | public static native boolean Process32Next(Pointer pointer, Tlhelp32.PROCESSENTRY32 entry); |
49 | 52 |
|
| 53 | + public static native boolean Module32NextW(Pointer pointer, Kernel32.MODULEENTRY32W entry); |
| 54 | + |
50 | 55 | public static native long ReadProcessMemory(Pointer process, Pointer address, MemoryBuffer memory, int size, int written); |
51 | 56 |
|
52 | 57 | public static native long WriteProcessMemory(Pointer process, Pointer address, MemoryBuffer memory, int size, int written); |
53 | 58 |
|
54 | | - public static native WinDef.HMODULE GetModuleHandle(String value); |
| 59 | + /** |
| 60 | + * Describes an entry from a list of the modules belonging to the specified |
| 61 | + * process. |
| 62 | + * |
| 63 | + * @see <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms684225(v=vs.85).aspx">MSDN</a> |
| 64 | + */ |
| 65 | + public static class MODULEENTRY32W extends Structure { |
| 66 | + |
| 67 | + public static final int MAX_MODULE_NAME32 = 255; |
| 68 | + |
| 69 | + /** |
| 70 | + * A representation of a MODULEENTRY32 structure as a reference |
| 71 | + */ |
| 72 | + public static class ByReference extends MODULEENTRY32W implements Structure.ByReference { |
| 73 | + public ByReference() { |
| 74 | + } |
| 75 | + |
| 76 | + public ByReference(Pointer memory) { |
| 77 | + super(memory); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * The size of the structure, in bytes. Before calling the Module32First |
| 83 | + * function, set this member to sizeof(MODULEENTRY32). If you do not |
| 84 | + * initialize dwSize, Module32First fails. |
| 85 | + */ |
| 86 | + public WinDef.DWORD dwSize; |
| 87 | + |
| 88 | + /** |
| 89 | + * This member is no longer used, and is always set to one. |
| 90 | + */ |
| 91 | + public WinDef.DWORD th32ModuleID; |
| 92 | + |
| 93 | + /** |
| 94 | + * The identifier of the process whose modules are to be examined. |
| 95 | + */ |
| 96 | + public WinDef.DWORD th32ProcessID; |
| 97 | + |
| 98 | + /** |
| 99 | + * The load count of the module, which is not generally meaningful, and |
| 100 | + * usually equal to 0xFFFF. |
| 101 | + */ |
| 102 | + public WinDef.DWORD GlblcntUsage; |
| 103 | + |
| 104 | + /** |
| 105 | + * The load count of the module (same as GlblcntUsage), which is not |
| 106 | + * generally meaningful, and usually equal to 0xFFFF. |
| 107 | + */ |
| 108 | + public WinDef.DWORD ProccntUsage; |
55 | 109 |
|
56 | | - public static native boolean GetExitCodeProcess(Pointer hProcess, IntByReference lpExitCode); |
| 110 | + /** |
| 111 | + * The base address of the module in the context of the owning process. |
| 112 | + */ |
| 113 | + public Pointer modBaseAddr; |
| 114 | + |
| 115 | + /** |
| 116 | + * The size of the module, in bytes. |
| 117 | + */ |
| 118 | + public WinDef.DWORD modBaseSize; |
| 119 | + |
| 120 | + /** |
| 121 | + * A handle to the module in the context of the owning process. |
| 122 | + */ |
| 123 | + public WinDef.HMODULE hModule; |
| 124 | + |
| 125 | + /** |
| 126 | + * The module name. |
| 127 | + */ |
| 128 | + public char[] szModule = new char[MAX_MODULE_NAME32 + 1]; |
| 129 | + |
| 130 | + /** |
| 131 | + * The module path. |
| 132 | + */ |
| 133 | + public char[] szExePath = new char[com.sun.jna.platform.win32.Kernel32.MAX_PATH]; |
| 134 | + |
| 135 | + public MODULEENTRY32W() { |
| 136 | + dwSize = new WinDef.DWORD(size()); |
| 137 | + } |
| 138 | + |
| 139 | + public MODULEENTRY32W(Pointer memory) { |
| 140 | + super(memory); |
| 141 | + read(); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @return The module name. |
| 146 | + */ |
| 147 | + public String szModule() { |
| 148 | + return Native.toString(this.szModule); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * @return The module path. |
| 153 | + */ |
| 154 | + public String szExePath() { |
| 155 | + return Native.toString(this.szExePath); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + protected List<String> getFieldOrder() { |
| 160 | + return Arrays.asList("dwSize", "th32ModuleID", "th32ProcessID", "GlblcntUsage", "ProccntUsage", "modBaseAddr", "modBaseSize", "hModule", "szModule", "szExePath"); |
| 161 | + } |
| 162 | + } |
57 | 163 |
|
58 | 164 | } |
0 commit comments