Skip to content

Commit

Permalink
Add mappings for Thread32First and Thread32Next to Kernel32
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed Jun 15, 2020
1 parent 1e86b54 commit 9892e49
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Features
* [#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).
* [#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).
* [#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).
* [#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).

Bug Fixes
---------
Expand Down
31 changes: 31 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java
Original file line number Diff line number Diff line change
Expand Up @@ -2385,6 +2385,37 @@ boolean DeviceIoControl(HANDLE hDevice, int dwIoControlCode,
*/
boolean Process32Next(HANDLE hSnapshot, Tlhelp32.PROCESSENTRY32 lppe);

/**
* Retrieves information about the first thread of any process encountered in a
* system snapshot.
*
* @param hSnapshot
* A handle to the snapshot returned from a previous call to the
* CreateToolhelp32Snapshot function.
* @param lpte
* A pointer to a THREADENTRY32 structure.
* @return Returns TRUE if the first entry of the thread list has been copied to
* the buffer or FALSE otherwise. The ERROR_NO_MORE_FILES error value is
* returned by the GetLastError function if no threads exist or the
* snapshot does not contain thread information.
*/
boolean Thread32First(HANDLE hSnapshot, Tlhelp32.THREADENTRY32 lpte);

/**
* Retrieves information about the next process recorded in a system snapshot.
*
* @param hSnapshot
* A handle to the snapshot returned from a previous call to the
* CreateToolhelp32Snapshot function.
* @param lpte
* A pointer to a THREADENTRY32 structure.
* @return Returns TRUE if the next entry of the thread list has been copied to
* the buffer or FALSE otherwise. The ERROR_NO_MORE_FILES error value is
* returned by the GetLastError function if no threads exist or the
* snapshot does not contain thread information.
*/
boolean Thread32Next(HANDLE hSnapshot, Tlhelp32.THREADENTRY32 lpte);

/**
* The SetEnvironmentVariable function sets the contents of the specified
* environment variable for the current process.
Expand Down
72 changes: 69 additions & 3 deletions contrib/platform/src/com/sun/jna/platform/win32/Tlhelp32.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
*/
package com.sun.jna.platform.win32;

import java.util.List;

import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.Structure.FieldOrder;
Expand Down Expand Up @@ -171,11 +170,78 @@ public PROCESSENTRY32(Pointer memory) {
}
}

/**
* Describes an entry from a list of the threads executing in the system when a
* snapshot was taken.
*/
@FieldOrder({ "dwSize", "cntUsage", "th32ThreadID", "th32OwnerProcessID", "tpBasePri", "tpDeltaPri", "dwFlags" })
public static class THREADENTRY32 extends Structure {

public static class ByReference extends THREADENTRY32 implements Structure.ByReference {
public ByReference() {
}

public ByReference(Pointer memory) {
super(memory);
}
}

/**
* The size of the structure, in bytes. Before calling the Thread32First
* function, set this member to sizeof(THREADENTRY32). If you do not initialize
* dwSize, Thread32First fails.
*/
int dwSize;

/**
* This member is no longer used and is always set to zero.
*/
int cntUsage;

/**
* The thread identifier, compatible with the thread identifier returned by the
* CreateProcess function.
*/
int th32ThreadID;

/**
* The identifier of the process that created the thread.
*/
int th32OwnerProcessID;

/**
* The kernel base priority level assigned to the thread. The priority is a
* number from 0 to 31, with 0 representing the lowest possible thread priority.
* For more information, see KeQueryPriorityThread.
*/
NativeLong tpBasePri;

/**
* This member is no longer used and is always set to zero.
*/
NativeLong tpDeltaPri;

/**
* This member is no longer used and is always set to zero.
*/
int dwFlags;

public THREADENTRY32() {
dwSize = size();
}

public THREADENTRY32(Pointer memory) {
super(memory);
read();
}
}

/**
* Describes an entry from a list of the modules belonging to the specified
* process.
*
* @see <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms684225(v=vs.85).aspx">MSDN</a>
* @see <a href=
* "https://msdn.microsoft.com/en-us/library/windows/desktop/ms684225(v=vs.85).aspx">MSDN</a>
*/
@FieldOrder({"dwSize", "th32ModuleID", "th32ProcessID", "GlblcntUsage",
"ProccntUsage", "modBaseAddr", "modBaseSize", "hModule",
Expand Down
22 changes: 22 additions & 0 deletions contrib/platform/test/com/sun/jna/platform/win32/Kernel32Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,28 @@ public void testGetProcessList() throws IOException {
}
}

public void testGetThreadList() throws IOException {
HANDLE threadEnumHandle = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPTHREAD,
new WinDef.DWORD(0));
assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(threadEnumHandle));
try {
Tlhelp32.THREADENTRY32.ByReference threadEntry = new Tlhelp32.THREADENTRY32.ByReference();

assertTrue(Kernel32.INSTANCE.Thread32First(threadEnumHandle, threadEntry));

List<Integer> threadIdList = new ArrayList<Integer>();
threadIdList.add(threadEntry.th32ThreadID);

while (Kernel32.INSTANCE.Thread32Next(threadEnumHandle, threadEntry)) {
threadIdList.add(threadEntry.th32ThreadID);
}

assertTrue(threadIdList.size() > 4);
} finally {
Kernel32Util.closeHandle(threadEnumHandle);
}
}

public final void testGetPrivateProfileInt() throws IOException {
final File tmp = File.createTempFile("testGetPrivateProfileInt", "ini");
tmp.deleteOnExit();
Expand Down

0 comments on commit 9892e49

Please sign in to comment.