1 /*
2  * The contents of this file is dual-licensed under 2
3  * alternative Open Source/Free licenses: LGPL 2.1 or later and
4  * Apache License 2.0. (starting with JNA version 4.0.0).
5  *
6  * You can freely decide which license you want to apply to
7  * the project.
8  *
9  * You may obtain a copy of the LGPL License at:
10  *
11  * http://www.gnu.org/licenses/licenses.html
12  *
13  * A copy is also included in the downloadable source code package
14  * containing JNA, in file "LGPL2.1".
15  *
16  * You may obtain a copy of the Apache License at:
17  *
18  * http://www.apache.org/licenses/
19  *
20  * A copy is also included in the downloadable source code package
21  * containing JNA, in file "AL2.0".
22  */
23 package com.sun.jna.platform.win32;
24 
25 import com.sun.jna.Native;
26 import com.sun.jna.NativeLong;
27 import com.sun.jna.Pointer;
28 import com.sun.jna.Structure;
29 import com.sun.jna.Structure.FieldOrder;
30 import com.sun.jna.platform.win32.WinDef.DWORD;
31 import com.sun.jna.platform.win32.WinDef.HMODULE;
32 
33 /**
34  * Interface for the Tlhelp32.h header file.
35  */
36 public interface Tlhelp32 {
37 
38     /**
39      * Includes all heaps of the process specified in th32ProcessID in the snapshot. To enumerate the heaps, see
40      * Heap32ListFirst.
41      */
42     WinDef.DWORD TH32CS_SNAPHEAPLIST = new WinDef.DWORD(0x00000001);
43 
44     /**
45      * Includes all processes in the system in the snapshot. To enumerate the processes, see Process32First.
46      */
47     WinDef.DWORD TH32CS_SNAPPROCESS  = new WinDef.DWORD(0x00000002);
48 
49     /**
50      * Includes all threads in the system in the snapshot. To enumerate the threads, see Thread32First.
51      */
52     WinDef.DWORD TH32CS_SNAPTHREAD   = new WinDef.DWORD(0x00000004);
53 
54     /**
55      *
56      * Used with Kernel32.CreateToolhelp32Snapshot<br>
57      * Includes all modules of the process specified in th32ProcessID in the
58      * snapshot. <br>
59      * To enumerate the modules, see Module32First.<br>
60      * If the function fails with ERROR_BAD_LENGTH, retry the function until it
61      * succeeds. <br>
62      * 64-bit Windows: Using this flag in a 32-bit process includes the 32-bit
63      * modules of the process specified in th32ProcessID, while using it in a
64      * 64-bit process includes the 64-bit modules.<br>
65      * To include the 32-bit modules of the process specified in th32ProcessID
66      * from a 64-bit process, use the TH32CS_SNAPMODULE32 flag.
67      *
68      * @see <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms682489(v=vs.85).aspx">MSDN</a>
69      */
70     WinDef.DWORD TH32CS_SNAPMODULE   = new WinDef.DWORD(0x00000008);
71 
72     /**
73      * Includes all 32-bit modules of the process specified in th32ProcessID in the snapshot when called from a 64-bit
74      * process. This flag can be combined with TH32CS_SNAPMODULE or TH32CS_SNAPALL. If the function fails with
75      * ERROR_BAD_LENGTH, retry the function until it succeeds.
76      */
77     WinDef.DWORD TH32CS_SNAPMODULE32 = new WinDef.DWORD(0x00000010);
78 
79     /**
80      * Includes all processes and threads in the system, plus the heaps and modules of the process specified in th32ProcessID.
81      */
82     WinDef.DWORD TH32CS_SNAPALL      = new WinDef.DWORD((TH32CS_SNAPHEAPLIST.intValue() |
83             TH32CS_SNAPPROCESS.intValue() | TH32CS_SNAPTHREAD.intValue() | TH32CS_SNAPMODULE.intValue()));
84 
85     /**
86      * Indicates that the snapshot handle is to be inheritable.
87      */
88     WinDef.DWORD TH32CS_INHERIT      = new WinDef.DWORD(0x80000000);
89 
90     int MAX_MODULE_NAME32 = 255;
91 
92     /**
93      * Describes an entry from a list of the processes residing in the system address space when a snapshot was taken.
94      */
95     @FieldOrder({"dwSize", "cntUsage", "th32ProcessID", "th32DefaultHeapID",
96         "th32ModuleID", "cntThreads", "th32ParentProcessID", "pcPriClassBase",
97         "dwFlags", "szExeFile"})
98     public static class PROCESSENTRY32 extends Structure {
99 
100         public static class ByReference extends PROCESSENTRY32 implements Structure.ByReference {
ByReference()101             public ByReference() {
102             }
103 
ByReference(Pointer memory)104             public ByReference(Pointer memory) {
105                 super(memory);
106             }
107         }
108 
109         /**
110          * The size of the structure, in bytes. Before calling the Process32First function, set this member to
111          * sizeof(PROCESSENTRY32). If you do not initialize dwSize, Process32First fails.
112          */
113         public WinDef.DWORD dwSize;
114 
115         /**
116          * This member is no longer used and is always set to zero.
117          */
118         public WinDef.DWORD cntUsage;
119 
120         /**
121          * The process identifier.
122          */
123         public WinDef.DWORD th32ProcessID;
124 
125         /**
126          * This member is no longer used and is always set to zero.
127          */
128         public BaseTSD.ULONG_PTR th32DefaultHeapID;
129 
130         /**
131          * This member is no longer used and is always set to zero.
132          */
133         public WinDef.DWORD th32ModuleID;
134 
135         /**
136          * The number of execution threads started by the process.
137          */
138         public WinDef.DWORD cntThreads;
139 
140         /**
141          * The identifier of the process that created this process (its parent process).
142          */
143         public WinDef.DWORD th32ParentProcessID;
144 
145         /**
146          * The base priority of any threads created by this process.
147          */
148         public WinDef.LONG pcPriClassBase;
149 
150         /**
151          * This member is no longer used, and is always set to zero.
152          */
153         public WinDef.DWORD dwFlags;
154 
155         /**
156          * The name of the executable file for the process. To retrieve the full path to the executable file, call the
157          * Module32First function and check the szExePath member of the MODULEENTRY32 structure that is returned.
158          * However, if the calling process is a 32-bit process, you must call the QueryFullProcessImageName function to
159          * retrieve the full path of the executable file for a 64-bit process.
160          */
161         public char[] szExeFile = new char[WinDef.MAX_PATH];
162 
PROCESSENTRY32()163         public PROCESSENTRY32() {
164             dwSize = new WinDef.DWORD(size());
165         }
166 
PROCESSENTRY32(Pointer memory)167         public PROCESSENTRY32(Pointer memory) {
168             super(memory);
169             read();
170         }
171     }
172 
173     /**
174      * Describes an entry from a list of the threads executing in the system when a
175      * snapshot was taken.
176      */
177     @FieldOrder({ "dwSize", "cntUsage", "th32ThreadID", "th32OwnerProcessID", "tpBasePri", "tpDeltaPri", "dwFlags" })
178     public static class THREADENTRY32 extends Structure {
179 
180         public static class ByReference extends THREADENTRY32 implements Structure.ByReference {
ByReference()181             public ByReference() {
182             }
183 
ByReference(Pointer memory)184             public ByReference(Pointer memory) {
185                 super(memory);
186             }
187         }
188 
189         /**
190          * The size of the structure, in bytes. Before calling the Thread32First
191          * function, set this member to sizeof(THREADENTRY32). If you do not initialize
192          * dwSize, Thread32First fails.
193          */
194         public int dwSize;
195 
196         /**
197          * This member is no longer used and is always set to zero.
198          */
199         public int cntUsage;
200 
201         /**
202          * The thread identifier, compatible with the thread identifier returned by the
203          * CreateProcess function.
204          */
205         public int th32ThreadID;
206 
207         /**
208          * The identifier of the process that created the thread.
209          */
210         public int th32OwnerProcessID;
211 
212         /**
213          * The kernel base priority level assigned to the thread. The priority is a
214          * number from 0 to 31, with 0 representing the lowest possible thread priority.
215          * For more information, see KeQueryPriorityThread.
216          */
217         public NativeLong tpBasePri;
218 
219         /**
220          * This member is no longer used and is always set to zero.
221          */
222         public NativeLong tpDeltaPri;
223 
224         /**
225          * This member is no longer used and is always set to zero.
226          */
227         public int dwFlags;
228 
THREADENTRY32()229         public THREADENTRY32() {
230             dwSize = size();
231         }
232 
THREADENTRY32(Pointer memory)233         public THREADENTRY32(Pointer memory) {
234             super(memory);
235             read();
236         }
237     }
238 
239     /**
240      * Describes an entry from a list of the modules belonging to the specified
241      * process.
242      *
243      * @see <a href=
244      *      "https://msdn.microsoft.com/en-us/library/windows/desktop/ms684225(v=vs.85).aspx">MSDN</a>
245      */
246     @FieldOrder({"dwSize", "th32ModuleID", "th32ProcessID", "GlblcntUsage",
247         "ProccntUsage", "modBaseAddr", "modBaseSize", "hModule",
248         "szModule", "szExePath"})
249     public class MODULEENTRY32W extends Structure {
250 
251         /**
252          * A representation of a MODULEENTRY32 structure as a reference
253          */
254         public static class ByReference extends MODULEENTRY32W implements Structure.ByReference {
ByReference()255             public ByReference() {
256             }
257 
ByReference(Pointer memory)258             public ByReference(Pointer memory) {
259                 super(memory);
260             }
261         }
262 
263         /**
264          * The size of the structure, in bytes. Before calling the Module32First
265          * function, set this member to sizeof(MODULEENTRY32). If you do not
266          * initialize dwSize, Module32First fails.
267          */
268         public DWORD dwSize;
269 
270         /**
271          * This member is no longer used, and is always set to one.
272          */
273         public DWORD th32ModuleID;
274 
275         /**
276          * The identifier of the process whose modules are to be examined.
277          */
278         public DWORD th32ProcessID;
279 
280         /**
281          * The load count of the module, which is not generally meaningful, and
282          * usually equal to 0xFFFF.
283          */
284         public DWORD GlblcntUsage;
285 
286         /**
287          * The load count of the module (same as GlblcntUsage), which is not
288          * generally meaningful, and usually equal to 0xFFFF.
289          */
290         public DWORD ProccntUsage;
291 
292         /**
293          * The base address of the module in the context of the owning process.
294          */
295         public Pointer modBaseAddr;
296 
297         /**
298          * The size of the module, in bytes.
299          */
300         public DWORD modBaseSize;
301 
302         /**
303          * A handle to the module in the context of the owning process.
304          */
305         public HMODULE hModule;
306 
307         /**
308          * The module name.
309          */
310         public char[] szModule = new char[MAX_MODULE_NAME32 + 1];
311 
312         /**
313          * The module path.
314          */
315         public char[] szExePath = new char[Kernel32.MAX_PATH];
316 
MODULEENTRY32W()317         public MODULEENTRY32W() {
318             dwSize = new WinDef.DWORD(size());
319         }
320 
MODULEENTRY32W(Pointer memory)321         public MODULEENTRY32W(Pointer memory) {
322             super(memory);
323             read();
324         }
325 
326         /**
327          * @return The module name.
328          */
szModule()329         public String szModule() {
330             return Native.toString(this.szModule);
331         }
332 
333         /**
334          * @return The module path.
335          */
szExePath()336         public String szExePath() {
337             return Native.toString(this.szExePath);
338         }
339     }
340 }
341