Petit aide mémoire sur l’utilisation de JNA.
1. Interface Kernel32
2. Interface User32
3. Utilisation
Avant toute chose, il faut que le projet référence les librairies JNA et plateform : https://github.com/twall/jna
1. Interface Kernel32
import com.sun.jna.Native ; import com.sun.jna.win32.StdCallLibrary ; /** * Interface for kernel32 using JNA. * Minimum supported client: Windows 2000 Professional [desktop apps only] * Minimum supported server: Windows 2000 Server [desktop apps only] * Header: Winbase.h (include Windows.h) * @author Microsoft */ public interface Kernel32 extends StdCallLibrary { /** * Instance of Kernel32 mapped with Native library kernel32. */ Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class); /** * Retrieves the number of milliseconds that have elapsed since the system was started. * @see http://msdn2.microsoft.com/en-us/library/ms724408.aspx * @return number of milliseconds that have elapsed since the system was started. */ int GetTickCount(); }
2. Interface User32
import com.sun.jna.Native ; import com.sun.jna.platform.win32.WinDef.HWND ; import com.sun.jna.platform.win32.WinDef.LPARAM ; import com.sun.jna.platform.win32.WinDef.LRESULT ; import com.sun.jna.platform.win32.WinDef.WPARAM ; import com.sun.jna.platform.win32.WinUser.LASTINPUTINFO ; import com.sun.jna.ptr.PointerByReference ; import com.sun.jna.win32.StdCallLibrary ; /** * Interface for user32 using JNA. * Minimum supported client: Windows 2000 Professional [desktop apps only] * Minimum supported server: Windows 2000 Server [desktop apps only] * Header: Winbase.h (include Windows.h) * * * * @author Microsoft */ public interface User32 extends StdCallLibrary { /** * Instance of Kernel32 mapped with Native library user32. */ User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class); // Constants definition --------------------------------------------------- /* * From WM_SYSCOMMAND message. * @see http://msdn.microsoft.com/en-us/library/windows/desktop/ms646360(v=vs.85).aspx */ /** * Sets the state of the display. * This command supports devices that have power-saving features, * such as a battery-powered personal computer. * The lParam parameter can have the following values: * -1 (the display is powering on) * 1 (the display is going to low power) * 2 (the display is being shut off) */ int SC_MONITORPOWER = 0xF170; /** * the display is powering on value of SC_MONITORPOWER. */ int SC_MONITOR_ON = -1; /** * the display is being shut off value of SC_MONITORPOWER. */ int SC_MONITOR_OFF = 2; /** * Executes the screen saver application specified in the [boot] section * of the System.ini file. */ int SC_SCREENSAVE = 0xF140; /** * Determines whether a screen saver is currently running on the window * station of the calling process. */ int SPI_GETSCREENSAVERRUNNING = 0x0072; // Methods definition ------------------------------------------------------ /** * Retrieves the time of the last input event. * @see http://msdn.microsoft.com/en-us/library/windows/desktop/ms646302(v=vs.85).aspx * @param result [out]: a LASTINPUTINFO structure that receives the time of the last input event. * @return If the function succeeds, the return value is nonzero else the return value is zero. */ boolean GetLastInputInfo(LASTINPUTINFO result); /** * Retrieves a handle to the foreground window (the window with which the user is currently working). The system assigns a slightly higher priority to the thread that creates the foreground window than it does to other threads. * @see http://msdn.microsoft.com/en-us/library/windows/desktop/ms633505(v=vs.85).aspx * @return handle to the foreground window. The foreground window can be NULL in certain circumstances, such as when a window is losing activation. */ HWND GetForegroundWindow(); /** * Sends the specified message to a window or windows (ANSI version). * @see http://msdn.microsoft.com/en-us/library/windows/desktop/ms644950(v=vs.85).aspx * @param paramHWND [in] A handle to the window whose window procedure will receive the message. If this parameter is HWND_BROADCAST ((HWND)0xffff), the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows. * Message sending is subject to UIPI. The thread of a process can send messages only to message queues of threads in processes of lesser or equal integrity level. * @param paramInt [in] The message to be sent. * @param paramWPARAM [in] Additional message-specific information. * @param paramLPARAM [in] Additional message-specific information. * @return LRESULT - The return value specifies the result of the message processing; it depends on the message sent. */ LRESULT SendMessageA(HWND paramHWND, int paramInt, WPARAM paramWPARAM, LPARAM paramLPARAM); /** * Retrieves or sets the value of one of the system-wide parameters. * This function can also update the user profile while setting a parameter. * @see http://msdn.microsoft.com/en-us/library/windows/desktop/ms724947(v=vs.85).aspx * @param uiAction UINT - The system-wide parameter to be retrieved or set * @param uiParam UINT - A parameter whose usage and format depends on the system parameter being queried or set * @param pvParam PVOID - A parameter whose usage and format depends on the system parameter being queried or set * @param fWInIni UINT - If a system parameter is being set, specifies whether the user profile is to be updated, and if so, whether the WM_SETTINGCHANGE message is to be broadcast to all top-level windows to notify them of the change * @return If the function succeeds, the return value is a nonzero value, else the return value is zero */ boolean SystemParametersInfoA(int uiAction, int uiParam, PointerByReference pvParam, int fWInIni); }
3. Utilisation
import com.sun.jna.platform.win32.WinDef ; import com.sun.jna.platform.win32.WinUser ; import com.sun.jna.platform.win32.WinUser.LASTINPUTINFO ; import com.sun.jna.ptr.PointerByReference ; /** * Get the amount of milliseconds that have elapsed since the last input event (mouse or keyboard) * @return idle time in milliseconds */ public static int getIdleTimeMillisWin32() { LASTINPUTINFO lastInputInfo = new LASTINPUTINFO(); User32.INSTANCE.GetLastInputInfo(lastInputInfo); return Kernel32.INSTANCE.GetTickCount() - lastInputInfo.dwTime; } /** * @return true if the screensaver is displayed */ public static boolean isScreenSaverDisplayed() { PointerByReference pointer = new PointerByReference(); User32.INSTANCE.SystemParametersInfoA(User32.SPI_GETSCREENSAVERRUNNING, 0, pointer, 0); // pointer points to a BOOL variable that receives TRUE if a screen saver is currently running, or FALSE otherwise return pointer.getPointer().getByte(0) == 1; } /** * Force scrensaver to go away. */ public static void forceScreenSaverExit() { User32.INSTANCE.SendMessageA(User32.INSTANCE.GetForegroundWindow(), WinUser.WM_CLOSE, new WinDef.WPARAM(0), new WinDef.LPARAM(0)); } /** * Force scrensaver display. */ public static void forceScreenSaverDisplay() { final WinDef.HWND lHwnd = User32.INSTANCE.GetForegroundWindow(); User32.INSTANCE.SendMessageA(lHwnd, WinUser.WM_SYSCOMMAND, new WinDef.WPARAM(User32.SC_SCREENSAVE), new WinDef.LPARAM(0)); }