Class WindowsForeignFunctions

java.lang.Object
oshi.ffm.ForeignFunctions
oshi.ffm.windows.WindowsForeignFunctions
Direct Known Subclasses:
Advapi32FFM, IPHlpAPIFFM, Kernel32FFM, PsapiFFM

public abstract class WindowsForeignFunctions extends ForeignFunctions
Utility class for working with the Foreign Function and Memory API. Provides helpers for library lookup, downcalls, and UTF-16 string conversion.
  • Constructor Details

    • WindowsForeignFunctions

      protected WindowsForeignFunctions()
  • Method Details

    • checkSuccess

      public static int checkSuccess(int rc, int... allowedErrors)
      Validates a Windows API return code, allowing ERROR_SUCCESS or specified codes.
      Parameters:
      rc - return code from a Windows API call
      allowedErrors - optional additional codes
      Returns:
      the original code if valid
      Throws:
      Win32Exception - if not successful
    • isSuccess

      public static boolean isSuccess(int winBool)
      Converts a Windows BOOL (0 or non-zero) to a Java boolean. In Windows API, 0 means FALSE and non-zero means TRUE.
      Parameters:
      winBool - the BOOL value returned by a Windows API
      Returns:
      true if non-zero, false if zero
    • readWideString

      public static String readWideString(MemorySegment seg)
      Reads a null-terminated UTF-16 wide string from the given memory segment.
      Parameters:
      seg - the memory segment containing the UTF-16 string
      Returns:
      the decoded Java string
    • readAnsiString

      public static String readAnsiString(MemorySegment seg, int maxLen)
      Reads a null-terminated ANSI (single-byte) string from the given memory segment.
      Parameters:
      seg - the memory segment containing the ANSI string
      maxLen - the maximum number of bytes to read
      Returns:
      the decoded Java string
    • setupTokenPrivileges

      public static MemorySegment setupTokenPrivileges(Arena arena, MemorySegment luid)
      Creates and initializes a TOKEN_PRIVILEGES structure with one privilege enabled.
      Parameters:
      arena - the memory arena used for allocation
      luid - the LUID of the privilege to enable
      Returns:
      a memory segment containing the initialized TOKEN_PRIVILEGES structure
    • toWideString

      public static MemorySegment toWideString(Arena arena, String s)
      Converts a Java string into a null-terminated UTF-16LE wide string memory segment.
      Parameters:
      arena - the memory arena used for allocation
      s - the Java string to convert
      Returns:
      a memory segment containing the UTF-16LE encoded string with a null terminator