Skip to content

Commit 9892e49

Browse files
committed
Add mappings for Thread32First and Thread32Next to Kernel32
1 parent 1e86b54 commit 9892e49

File tree

4 files changed

+123
-3
lines changed

4 files changed

+123
-3
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Features
1717
* [#1194](https://github.com/java-native-access/jna/pull/1194): Add `GetConsoleScreenBufferInfo`, `ReadConsoleInput` and `WriteConsole` with associated structures to `c.s.j.p.win32.Wincon` - [@rednoah](https://github.com/rednoah).
1818
* [#1198](https://github.com/java-native-access/jna/pull/1198): Add `NetSessionEnum` to `c.s.j.p.win32.Netapi32` and `WTSEnumerateSessions`, `WTSQuerySessionInformation`, and `WTSFreeMemory` to `c.s.j.p.win32.Wtsapi32` - [@dbwiddis](https://github.com/dbwiddis).
1919
* [#1200](https://github.com/java-native-access/jna/pull/1200): Add mappings for `libudev` to `c.s.j.p.linux.Udev` - [@dbwiddis](https://github.com/dbwiddis).
20+
* [#1209](https://github.com/java-native-access/jna/pull/1209): Add mappings for `Thread32First` and `Thread32Next` to `c.s.j.p.win32.Kernel32` - [@dbwiddis](https://github.com/dbwiddis).
2021

2122
Bug Fixes
2223
---------

contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java

+31
Original file line numberDiff line numberDiff line change
@@ -2385,6 +2385,37 @@ boolean DeviceIoControl(HANDLE hDevice, int dwIoControlCode,
23852385
*/
23862386
boolean Process32Next(HANDLE hSnapshot, Tlhelp32.PROCESSENTRY32 lppe);
23872387

2388+
/**
2389+
* Retrieves information about the first thread of any process encountered in a
2390+
* system snapshot.
2391+
*
2392+
* @param hSnapshot
2393+
* A handle to the snapshot returned from a previous call to the
2394+
* CreateToolhelp32Snapshot function.
2395+
* @param lpte
2396+
* A pointer to a THREADENTRY32 structure.
2397+
* @return Returns TRUE if the first entry of the thread list has been copied to
2398+
* the buffer or FALSE otherwise. The ERROR_NO_MORE_FILES error value is
2399+
* returned by the GetLastError function if no threads exist or the
2400+
* snapshot does not contain thread information.
2401+
*/
2402+
boolean Thread32First(HANDLE hSnapshot, Tlhelp32.THREADENTRY32 lpte);
2403+
2404+
/**
2405+
* Retrieves information about the next process recorded in a system snapshot.
2406+
*
2407+
* @param hSnapshot
2408+
* A handle to the snapshot returned from a previous call to the
2409+
* CreateToolhelp32Snapshot function.
2410+
* @param lpte
2411+
* A pointer to a THREADENTRY32 structure.
2412+
* @return Returns TRUE if the next entry of the thread list has been copied to
2413+
* the buffer or FALSE otherwise. The ERROR_NO_MORE_FILES error value is
2414+
* returned by the GetLastError function if no threads exist or the
2415+
* snapshot does not contain thread information.
2416+
*/
2417+
boolean Thread32Next(HANDLE hSnapshot, Tlhelp32.THREADENTRY32 lpte);
2418+
23882419
/**
23892420
* The SetEnvironmentVariable function sets the contents of the specified
23902421
* environment variable for the current process.

contrib/platform/src/com/sun/jna/platform/win32/Tlhelp32.java

+69-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222
*/
2323
package com.sun.jna.platform.win32;
2424

25-
import java.util.List;
26-
2725
import com.sun.jna.Native;
26+
import com.sun.jna.NativeLong;
2827
import com.sun.jna.Pointer;
2928
import com.sun.jna.Structure;
3029
import com.sun.jna.Structure.FieldOrder;
@@ -171,11 +170,78 @@ public PROCESSENTRY32(Pointer memory) {
171170
}
172171
}
173172

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 {
181+
public ByReference() {
182+
}
183+
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+
int dwSize;
195+
196+
/**
197+
* This member is no longer used and is always set to zero.
198+
*/
199+
int cntUsage;
200+
201+
/**
202+
* The thread identifier, compatible with the thread identifier returned by the
203+
* CreateProcess function.
204+
*/
205+
int th32ThreadID;
206+
207+
/**
208+
* The identifier of the process that created the thread.
209+
*/
210+
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+
NativeLong tpBasePri;
218+
219+
/**
220+
* This member is no longer used and is always set to zero.
221+
*/
222+
NativeLong tpDeltaPri;
223+
224+
/**
225+
* This member is no longer used and is always set to zero.
226+
*/
227+
int dwFlags;
228+
229+
public THREADENTRY32() {
230+
dwSize = size();
231+
}
232+
233+
public THREADENTRY32(Pointer memory) {
234+
super(memory);
235+
read();
236+
}
237+
}
238+
174239
/**
175240
* Describes an entry from a list of the modules belonging to the specified
176241
* process.
177242
*
178-
* @see <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms684225(v=vs.85).aspx">MSDN</a>
243+
* @see <a href=
244+
* "https://msdn.microsoft.com/en-us/library/windows/desktop/ms684225(v=vs.85).aspx">MSDN</a>
179245
*/
180246
@FieldOrder({"dwSize", "th32ModuleID", "th32ProcessID", "GlblcntUsage",
181247
"ProccntUsage", "modBaseAddr", "modBaseSize", "hModule",

contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java

+22
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,28 @@ public void testGetProcessList() throws IOException {
11801180
}
11811181
}
11821182

1183+
public void testGetThreadList() throws IOException {
1184+
HANDLE threadEnumHandle = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPTHREAD,
1185+
new WinDef.DWORD(0));
1186+
assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(threadEnumHandle));
1187+
try {
1188+
Tlhelp32.THREADENTRY32.ByReference threadEntry = new Tlhelp32.THREADENTRY32.ByReference();
1189+
1190+
assertTrue(Kernel32.INSTANCE.Thread32First(threadEnumHandle, threadEntry));
1191+
1192+
List<Integer> threadIdList = new ArrayList<Integer>();
1193+
threadIdList.add(threadEntry.th32ThreadID);
1194+
1195+
while (Kernel32.INSTANCE.Thread32Next(threadEnumHandle, threadEntry)) {
1196+
threadIdList.add(threadEntry.th32ThreadID);
1197+
}
1198+
1199+
assertTrue(threadIdList.size() > 4);
1200+
} finally {
1201+
Kernel32Util.closeHandle(threadEnumHandle);
1202+
}
1203+
}
1204+
11831205
public final void testGetPrivateProfileInt() throws IOException {
11841206
final File tmp = File.createTempFile("testGetPrivateProfileInt", "ini");
11851207
tmp.deleteOnExit();

0 commit comments

Comments
 (0)