Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/main/java/ooo/oshi/foreign/windows/WtsApi32.java
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
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();
}

}


}
34 changes: 19 additions & 15 deletions src/test/java/ooo/oshi/Kernel32Test.java
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);
}
}