This repository was archived by the owner on Apr 13, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
[WIP] Enumerate windows processes with wtsapi32 #20
Draft
adrian-kong
wants to merge
3
commits into
oshi:main
Choose a base branch
from
adrian-kong:adrian/winprocesses
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package ooo.oshi.foreign.windows; | ||
|
|
||
| import java.lang.foreign.*; | ||
| import java.lang.invoke.MethodHandle; | ||
|
|
||
| import static java.lang.foreign.ValueLayout.*; | ||
|
|
||
| public class WtsApi32 { | ||
|
|
||
| private static final SymbolLookup WTSAPI32; | ||
| private static final Linker LINKER; | ||
|
|
||
| static { | ||
| LINKER = Linker.nativeLinker(); | ||
| System.loadLibrary("wtsapi32"); | ||
| WTSAPI32 = SymbolLookup.loaderLookup(); | ||
| } | ||
|
|
||
| private static MethodHandle methodHandle(String methodName, FunctionDescriptor fd) { | ||
| return LINKER.downcallHandle(WTSAPI32.lookup(methodName).orElseThrow(), fd); | ||
| } | ||
|
|
||
| private static final MethodHandle WTSEnumerateProcessEx = methodHandle("WTSEnumerateProcessesA", FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_INT, JAVA_INT, JAVA_INT, ADDRESS, ADDRESS)); | ||
|
|
||
| private static final MemoryLayout _WTS_PROCESS_INFOA = MemoryLayout.structLayout( | ||
| JAVA_INT.withName("SessionId"), | ||
| JAVA_INT.withName("ProcessId"), | ||
| JAVA_CHAR.withName("pProcessName"), | ||
| ADDRESS.withName("pUserSid") | ||
| ).withName("WTS_PROCESS_INFOA"); | ||
|
|
||
| /** | ||
| * Refer to <a href="https://github.com/VFPX/Win32API/blob/master/libraries/wtsapi32/WTSEnumerateProcesses.md">Win32API</a> | ||
| */ | ||
| public static void enumerateProcesses() { | ||
| try { | ||
| try (MemorySession session = MemorySession.openConfined()) { | ||
| int hServer = 0; // represents null handle | ||
| int version = 1; // version of enumerated request, must be 1 | ||
| // out variables | ||
| Addressable ppProcessInfo = session.allocate(_WTS_PROCESS_INFOA); // array of WTS_PROCESS_INFO | ||
adrian-kong marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Addressable pCount = session.allocate(JAVA_INT); // process count | ||
| var ret = (boolean) WTSEnumerateProcessEx.invokeExact(hServer, 0, version, ppProcessInfo, pCount); | ||
|
|
||
| if (!ret) throw new Exception("error calling WTSEnumerateProcessesA"); | ||
|
|
||
| int processCount = ppProcessInfo.address().get(JAVA_INT, 0) - 1; | ||
| System.out.println("Total processes found: " + processCount); | ||
|
|
||
| // for (int i = 0; i < processCount; i++) { | ||
| // | ||
| // } | ||
|
|
||
| System.out.println(pCount.address().get(JAVA_INT, 0)); | ||
| } | ||
| } catch (Throwable ex) { | ||
| ex.printStackTrace(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,28 @@ | ||
| package ooo.oshi; | ||
|
|
||
| import ooo.oshi.foreign.windows.Kernel32Library; | ||
| import ooo.oshi.foreign.windows.WtsApi32; | ||
| import ooo.oshi.software.os.OperatingSystem; | ||
|
|
||
| public class Kernel32Test { | ||
| public static void main(String[] args) { | ||
|
|
||
| System.out.println("The operating system is: " + SystemInfo.getCurrentPlatform().getName()); | ||
| SystemInfo si = new SystemInfo(); | ||
| OperatingSystem os = si.getOperatingSystem(); | ||
| int currentProcessId = os.getProcessId(); | ||
| System.out.println("The current Process ID is: " + currentProcessId); | ||
|
|
||
| String computerName = Kernel32Library.getComputerName(); | ||
| System.out.println("Computer Name: " + computerName); | ||
|
|
||
| String tempPath = Kernel32Library.getTempPath(); | ||
| System.out.println("Temp Path: " + tempPath); | ||
|
|
||
| String processName = Kernel32Library.queryFullProcessImageName(currentProcessId, 0); | ||
| System.out.println("Current Process: " + processName); | ||
|
|
||
|
|
||
| WtsApi32.enumerateProcesses(); | ||
|
|
||
| // System.out.println("The operating system is: " + SystemInfo.getCurrentPlatform().getName()); | ||
| // SystemInfo si = new SystemInfo(); | ||
| // OperatingSystem os = si.getOperatingSystem(); | ||
| // int currentProcessId = os.getProcessId(); | ||
| // System.out.println("The current Process ID is: " + currentProcessId); | ||
| // | ||
| // String computerName = Kernel32Library.getComputerName(); | ||
| // System.out.println("Computer Name: " + computerName); | ||
| // | ||
| // String tempPath = Kernel32Library.getTempPath(); | ||
| // System.out.println("Temp Path: " + tempPath); | ||
| // | ||
| // String processName = Kernel32Library.queryFullProcessImageName(currentProcessId, 0); | ||
| // System.out.println("Current Process: " + processName); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.