CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-net-java-dev-jna--jna-platform

Java Native Access Platform provides cross-platform mappings and utilities for commonly used platform functions across Windows, macOS, and Linux systems.

Pending
Overview
Eval results
Files

windows.mddocs/

Windows Platform APIs

Comprehensive Windows API bindings providing access to core system functions, user interface operations, graphics, networking, services, and Component Object Model (COM) automation. These bindings provide direct access to Win32 APIs through JNA interfaces.

Capabilities

Core System Functions (Kernel32)

Essential Windows system functions for process management, memory operations, file I/O, and system information.

/**
 * Core Windows system functions from kernel32.dll
 */
public interface Kernel32 extends StdCallLibrary, WinNT, Wincon {
    Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class, W32APIOptions.DEFAULT_OPTIONS);
    
    /**
     * Create a new process
     * @param lpApplicationName Path to executable (can be null)
     * @param lpCommandLine Command line string
     * @param lpProcessAttributes Process security attributes
     * @param lpThreadAttributes Thread security attributes  
     * @param bInheritHandles Whether child inherits handles
     * @param dwCreationFlags Process creation flags
     * @param lpEnvironment Environment block (can be null)
     * @param lpCurrentDirectory Current directory (can be null)
     * @param lpStartupInfo Startup information
     * @param lpProcessInformation Process information output
     * @return true if successful
     */
    boolean CreateProcess(String lpApplicationName, String lpCommandLine,
                         WinBase.SECURITY_ATTRIBUTES lpProcessAttributes,
                         WinBase.SECURITY_ATTRIBUTES lpThreadAttributes,
                         boolean bInheritHandles, DWORD dwCreationFlags,
                         Pointer lpEnvironment, String lpCurrentDirectory,
                         WinBase.STARTUPINFO lpStartupInfo,
                         WinBase.PROCESS_INFORMATION lpProcessInformation);
    
    /**
     * Open existing process for access
     * @param dwDesiredAccess Access rights requested
     * @param bInheritHandle Whether handle can be inherited
     * @param dwProcessId Process ID to open
     * @return Process handle, or null if failed
     */
    HANDLE OpenProcess(DWORD dwDesiredAccess, boolean bInheritHandle, DWORD dwProcessId);
    
    /**
     * Get current process ID
     * @return Current process identifier
     */
    DWORD GetCurrentProcessId();
    
    /**
     * Get current thread ID
     * @return Current thread identifier
     */
    DWORD GetCurrentThreadId();
    
    /**
     * Get last error code
     * @return Error code from last failed API call
     */
    DWORD GetLastError();
    
    /**
     * Allocate virtual memory
     * @param lpAddress Starting address (can be null)
     * @param dwSize Size to allocate
     * @param flAllocationType Allocation type flags
     * @param flProtect Memory protection flags
     * @return Allocated memory address, or null if failed
     */
    Pointer VirtualAlloc(Pointer lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect);
    
    /**
     * Free virtual memory
     * @param lpAddress Address to free
     * @param dwSize Size to free (0 for entire allocation)
     * @param dwFreeType Free operation type
     * @return true if successful
     */
    boolean VirtualFree(Pointer lpAddress, SIZE_T dwSize, DWORD dwFreeType);
}

Usage Examples:

import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.WinBase;

// Get current process information
DWORD processId = Kernel32.INSTANCE.GetCurrentProcessId();
DWORD threadId = Kernel32.INSTANCE.GetCurrentThreadId();
System.out.println("PID: " + processId + ", TID: " + threadId);

// Create a new process
WinBase.STARTUPINFO startupInfo = new WinBase.STARTUPINFO();
WinBase.PROCESS_INFORMATION processInfo = new WinBase.PROCESS_INFORMATION();

boolean success = Kernel32.INSTANCE.CreateProcess(
    null,                    // Application name
    "notepad.exe",           // Command line
    null,                    // Process attributes
    null,                    // Thread attributes
    false,                   // Inherit handles
    new DWORD(0),           // Creation flags
    null,                    // Environment
    null,                    // Current directory
    startupInfo,             // Startup info
    processInfo              // Process info
);

if (success) {
    System.out.println("Process created with PID: " + processInfo.dwProcessId);
} else {
    System.err.println("Failed to create process. Error: " + Kernel32.INSTANCE.GetLastError());
}

User Interface Operations (User32)

Windows user interface APIs for window management, message handling, and input processing.

/**
 * Windows user interface functions from user32.dll
 */
public interface User32 extends StdCallLibrary, WinUser, WinNT {
    User32 INSTANCE = Native.load("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);
    
    // Window visibility constants
    int SW_HIDE = 0;
    int SW_MAXIMIZE = 3;
    int SW_MINIMIZE = 6;
    int SW_RESTORE = 9;
    int SW_SHOW = 5;
    
    /**
     * Find window by class name and/or window title
     * @param lpClassName Window class name (can be null)
     * @param lpWindowName Window title (can be null)
     * @return Window handle, or null if not found
     */
    HWND FindWindow(String lpClassName, String lpWindowName);
    
    /**
     * Get window title text
     * @param hWnd Window handle
     * @param lpString Buffer to receive text
     * @param nMaxCount Maximum characters to copy
     * @return Number of characters copied
     */
    int GetWindowText(HWND hWnd, char[] lpString, int nMaxCount);
    
    /**
     * Send message to window
     * @param hWnd Target window handle
     * @param Msg Message identifier
     * @param wParam First message parameter
     * @param lParam Second message parameter
     * @return Message result
     */
    LRESULT SendMessage(HWND hWnd, int Msg, WPARAM wParam, LPARAM lParam);
    
    /**
     * Show or hide window
     * @param hWnd Window handle
     * @param nCmdShow Show command (SW_* constants)
     * @return true if window was previously visible
     */
    boolean ShowWindow(HWND hWnd, int nCmdShow);
    
    /**
     * Set window position and size
     * @param hWnd Window handle
     * @param hWndInsertAfter Z-order position
     * @param X Left position
     * @param Y Top position
     * @param cx Width
     * @param cy Height
     * @param uFlags Position flags
     * @return true if successful
     */
    boolean SetWindowPos(HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
    
    /**
     * Get window rectangle
     * @param hWnd Window handle
     * @param rect Rectangle structure to fill
     * @return true if successful
     */
    boolean GetWindowRect(HWND hWnd, WinDef.RECT rect);
}

Graphics Operations (GDI32)

Graphics Device Interface functions for drawing operations, device contexts, and graphics object management.

/**
 * Graphics Device Interface functions from gdi32.dll
 */
public interface GDI32 extends StdCallLibrary {
    GDI32 INSTANCE = Native.load("gdi32", GDI32.class, W32APIOptions.DEFAULT_OPTIONS);
    
    /**
     * Create compatible device context
     * @param hdc Device context to make compatible with (can be null)
     * @return Device context handle, or null if failed
     */
    HDC CreateCompatibleDC(HDC hdc);
    
    /**
     * Delete device context
     * @param hdc Device context to delete
     * @return true if successful
     */
    boolean DeleteDC(HDC hdc);
    
    /**
     * Perform bit block transfer
     * @param hdcDest Destination device context
     * @param nXDest Destination X coordinate
     * @param nYDest Destination Y coordinate
     * @param nWidth Width of area to copy
     * @param nHeight Height of area to copy
     * @param hdcSrc Source device context
     * @param nXSrc Source X coordinate
     * @param nYSrc Source Y coordinate
     * @param dwRop Raster operation code
     * @return true if successful
     */
    boolean BitBlt(HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight,
                   HDC hdcSrc, int nXSrc, int nYSrc, DWORD dwRop);
    
    /**
     * Select graphics object into device context
     * @param hdc Device context
     * @param hgdiobj Graphics object to select
     * @return Previously selected object, or null if failed
     */
    HGDIOBJ SelectObject(HDC hdc, HGDIOBJ hgdiobj);
    
    /**
     * Delete graphics object
     * @param hObject Graphics object to delete
     * @return true if successful
     */
    boolean DeleteObject(HGDIOBJ hObject);
}

Advanced Services (Advapi32)

Advanced Windows services including registry access, security, services management, and event logging.

/**
 * Advanced Windows services from advapi32.dll
 */
public interface Advapi32 extends StdCallLibrary {
    Advapi32 INSTANCE = Native.load("Advapi32", Advapi32.class, W32APIOptions.DEFAULT_OPTIONS);
    
    /**
     * Open registry key
     * @param hKey Parent key handle
     * @param lpSubKey Subkey name
     * @param ulOptions Reserved (should be 0)
     * @param samDesired Security access mask
     * @param phkResult Opened key handle output
     * @return Error code (ERROR_SUCCESS if successful)
     */
    int RegOpenKeyEx(HKEY hKey, String lpSubKey, int ulOptions, int samDesired, HKEYByReference phkResult);
    
    /**
     * Query registry value
     * @param hKey Registry key handle
     * @param lpValueName Value name (can be null for default)
     * @param lpReserved Reserved (should be null)
     * @param lpType Value type output
     * @param lpData Value data buffer
     * @param lpcbData Data buffer size
     * @return Error code (ERROR_SUCCESS if successful)
     */
    int RegQueryValueEx(HKEY hKey, String lpValueName, int lpReserved, IntByReference lpType,
                       byte[] lpData, IntByReference lpcbData);
    
    /**
     * Close registry key
     * @param hKey Key handle to close
     * @return Error code (ERROR_SUCCESS if successful)
     */
    int RegCloseKey(HKEY hKey);
    
    /**
     * Open process token
     * @param ProcessHandle Process handle
     * @param DesiredAccess Access rights requested
     * @param TokenHandle Token handle output
     * @return true if successful
     */
    boolean OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess, HANDLEByReference TokenHandle);
    
    /**
     * Get token information
     * @param TokenHandle Token handle
     * @param TokenInformationClass Information class to retrieve
     * @param TokenInformation Information buffer
     * @param TokenInformationLength Buffer length
     * @param ReturnLength Required length output
     * @return true if successful
     */
    boolean GetTokenInformation(HANDLE TokenHandle, int TokenInformationClass,
                               Structure TokenInformation, int TokenInformationLength,
                               IntByReference ReturnLength);
}

Windows Data Types

Essential Windows data structures and type definitions used throughout the APIs.

/**
 * Basic Windows type definitions
 */
public class WinDef {
    /**
     * Window handle
     */
    public static class HWND extends HANDLE {
        public HWND() { }
        public HWND(Pointer p) { super(p); }
    }
    
    /**
     * 32-bit unsigned integer
     */
    public static class DWORD extends IntegerType {
        public DWORD() { this(0); }
        public DWORD(long value) { super(4, value, true); }
    }
    
    /**
     * 16-bit unsigned integer
     */
    public static class WORD extends IntegerType {
        public WORD() { this(0); }
        public WORD(long value) { super(2, value, true); }
    }
    
    /**
     * 8-bit unsigned integer
     */
    public static class BYTE extends IntegerType {
        public BYTE() { this(0); }
        public BYTE(long value) { super(1, value, true); }
    }
    
    /**
     * Pointer-sized integer for memory sizes
     */
    public static class SIZE_T extends IntegerType {
        public SIZE_T() { this(0); }
        public SIZE_T(long value) { super(Native.POINTER_SIZE, value, true); }
    }
    
    /**
     * Registry key handle
     */
    public static class HKEY extends HANDLE {
        public HKEY() { }
        public HKEY(Pointer p) { super(p); }
    }
    
    /**
     * Device context handle
     */
    public static class HDC extends HANDLE {
        public HDC() { }
        public HDC(Pointer p) { super(p); }
    }
    
    /**
     * GDI object handle
     */
    public static class HGDIOBJ extends HANDLE {
        public HGDIOBJ() { }
        public HGDIOBJ(Pointer p) { super(p); }
    }
    
    /**
     * Rectangle structure
     */
    public static class RECT extends Structure {
        public int left, top, right, bottom;
        
        public RECT() { }
        
        public RECT(int left, int top, int right, int bottom) {
            this.left = left; this.top = top;
            this.right = right; this.bottom = bottom;
        }
        
        @Override
        protected List<String> getFieldOrder() {
            return Arrays.asList(new String[] {"left", "top", "right", "bottom"});
        }
    }
    
    /**
     * Point structure
     */
    public static class POINT extends Structure {
        public int x, y;
        
        public POINT() { }
        
        public POINT(int x, int y) {
            this.x = x; this.y = y;
        }
        
        @Override
        protected List<String> getFieldOrder() {
            return Arrays.asList(new String[] {"x", "y"});
        }
    }
}

/**
 * Windows NT definitions
 */
public class WinNT {
    /**
     * Generic handle type
     */
    public static class HANDLE extends PointerType {
        public HANDLE() { }
        public HANDLE(Pointer p) { setPointer(p); }
    }
    
    /**
     * Access mask for security operations
     */
    public static class ACCESS_MASK extends DWORD {
        public ACCESS_MASK() { }
        public ACCESS_MASK(long value) { super(value); }
    }
}

Utility Classes

High-level wrapper classes that provide easier-to-use APIs and better error handling.

/**
 * High-level Kernel32 utilities
 */
public class Kernel32Util {
    /**
     * Get computer name
     * @return Computer name string
     * @throws Win32Exception if operation fails
     */
    public static String getComputerName() throws Win32Exception;
    
    /**
     * Get user name
     * @return Current user name
     * @throws Win32Exception if operation fails
     */
    public static String getUserName() throws Win32Exception;
    
    /**
     * Format system error message
     * @param code Error code to format
     * @return Error message string
     */
    public static String formatMessage(int code);
}

/**
 * High-level Advapi32 utilities
 */
public class Advapi32Util {
    /**
     * Get user account information
     * @param systemName System name (can be null for local)
     * @param accountName Account name to lookup
     * @return Account information
     * @throws Win32Exception if lookup fails
     */
    public static Account getAccountByName(String systemName, String accountName) throws Win32Exception;
    
    /**
     * Get registry value as string
     * @param root Root key
     * @param keyPath Key path
     * @param valueName Value name
     * @return String value
     * @throws Win32Exception if registry operation fails
     */
    public static String registryGetStringValue(HKEY root, String keyPath, String valueName) throws Win32Exception;
}

/**
 * Windows-specific exception with error code
 */
public class Win32Exception extends RuntimeException {
    private final int errorCode;
    
    public Win32Exception(int errorCode) {
        super(Kernel32Util.formatMessage(errorCode));
        this.errorCode = errorCode;
    }
    
    public int getErrorCode() {
        return errorCode;
    }
}

Complete Windows API Coverage

The JNA Platform library provides comprehensive coverage of Windows APIs including:

  • System Services: Winsvc (Service Control Manager), Psapi (Process Status), NtDll (Native APIs)
  • Networking: WinINet (Internet), Netapi32 (Network Management), IPHlpAPI (IP Helper)
  • Hardware/Devices: SetupApi (Device Installation), Cfgmgr32 (Configuration Manager), Dxva2 (DirectX)
  • File System: Shell32 (Shell), Shlwapi (Shell Utilities), Winioctl (I/O Control)
  • Security: Secur32 (Security Support Provider), Wtsapi32 (Terminal Services)
  • Performance: Pdh (Performance Data Helper), WMI integration
  • Graphics: OpenGL32 bindings, GDI32Util high-level graphics utilities

Each API follows the same pattern of providing both direct native bindings and high-level utility classes for common operations.

Install with Tessl CLI

npx tessl i tessl/maven-net-java-dev-jna--jna-platform

docs

com.md

cross-platform.md

drag-drop.md

index.md

mac.md

unix-linux.md

windows.md

tile.json