Java Code Examples for com.sun.jna.platform.win32.WinDef#DWORD

The following examples show how to use com.sun.jna.platform.win32.WinDef#DWORD . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WinUtil.java    From runelite with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Forcibly set focus to the given component
 *
 */
public static void requestForeground(Frame frame)
{
	// SetForegroundWindow can't set iconified windows to foreground, so set the
	// frame state to normal first
	frame.setState(Frame.NORMAL);

	User32 user32 = User32.INSTANCE;

	// Windows does not allow any process to set the foreground window, but it will if
	// the process received the last input event. So we send a F22 key event to the process.
	// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow
	WinUser.INPUT input = new WinUser.INPUT();
	input.type = new WinDef.DWORD(WinUser.INPUT.INPUT_KEYBOARD);
	input.input.ki.wVk = new WinDef.WORD(0x85); // VK_F22
	user32.SendInput(new WinDef.DWORD(1), (WinUser.INPUT[]) input.toArray(1), input.size());

	// Now we may set the foreground window
	WinDef.HWND hwnd = new WinDef.HWND(Native.getComponentPointer(frame));
	user32.SetForegroundWindow(hwnd);
}
 
Example 2
Source File: WindowsUtils.java    From RemoteSupportTool with Apache License 2.0 5 votes vote down vote up
/**
 * Create keyboard input event.
 *
 * @param character character to print
 * @param flags     flags
 * @return keyboard input event
 */
private static WinUser.INPUT createKeyboardInput(char character, long flags) {
    WinUser.INPUT input = new WinUser.INPUT();
    input.type = new WinDef.DWORD(WinUser.INPUT.INPUT_KEYBOARD);
    input.input.setType("ki");
    input.input.ki = new WinUser.KEYBDINPUT();
    input.input.ki.wVk = new WinDef.WORD(0);
    input.input.ki.wScan = new WinDef.WORD(character);
    input.input.ki.time = new WinDef.DWORD(0);
    input.input.ki.dwFlags = new WinDef.DWORD(flags);
    input.input.ki.dwExtraInfo = new BaseTSD.ULONG_PTR();
    return input;
}
 
Example 3
Source File: WindowsUtils.java    From RemoteSupportTool with Apache License 2.0 5 votes vote down vote up
/**
 * Enter a text by sending input events through the Windows API via JNA.
 *
 * @param text text to send through the Windows API
 * @throws WindowsException if the User32 library is not available or no events were processed
 * @see <a href="https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-sendinput">SendInput function</a>
 * @see <a href="https://stackoverflow.com/a/22308727">Using SendInput to send unicode characters beyond U+FFFF</a>
 */
private static void sendTextViaUser32(String text) throws WindowsException {
    if (StringUtils.isEmpty(text))
        return;
    if (USER32 == null)
        throw new WindowsException("User32 library was not loaded.");

    //final List<Long> pointers = new ArrayList<>();
    //noinspection EmptyFinallyBlock
    try {
        final List<WinUser.INPUT> events = new ArrayList<>();
        for (int i = 0; i < text.length(); i++) {
            final char c = text.charAt(i);
            //LOGGER.debug("printing " + c);
            events.add(createKeyboardInput(c, WinUser.KEYBDINPUT.KEYEVENTF_UNICODE));
            events.add(createKeyboardInput(c, WinUser.KEYBDINPUT.KEYEVENTF_KEYUP | WinUser.KEYBDINPUT.KEYEVENTF_UNICODE));
        }
        //for (WinUser.INPUT i : events) {
        //    long address = Pointer.nativeValue(i.getPointer());
        //    if (!pointers.contains(address)) pointers.add(address);
        //}

        WinUser.INPUT[] inputs = events.toArray(new WinUser.INPUT[0]);
        inputs = (WinUser.INPUT[]) inputs[0].toArray(inputs);
        //for (WinUser.INPUT i : inputs) {
        //    long address = Pointer.nativeValue(i.getPointer());
        //    if (!pointers.contains(address)) pointers.add(address);
        //}

        final WinDef.DWORD result = USER32.SendInput(
                new WinDef.DWORD(inputs.length), inputs, inputs[0].size());

        if (result.intValue() < 1) {
            LOGGER.error("last error: {}", Kernel32Util.getLastErrorMessage());
            throw new WindowsException("No events were executed.");
        }
        //LOGGER.debug("result: {}", result.intValue());
    } finally {
        //for (Long address : pointers) {
        //    Kernel32Util.freeLocalMemory(new Pointer(address));
        //}
    }
}
 
Example 4
Source File: Advapi32.java    From winthing with Apache License 2.0 5 votes vote down vote up
boolean InitiateSystemShutdown(
    String lpMachineName,
    String lpMessage,
    WinDef.DWORD dwTimeout,
    boolean bForceAppsClosed,
    boolean bRebootAfterShutdown
);
 
Example 5
Source File: Win32ProcessTools.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
public static boolean adjustPrivileges() {

        WinNT.TOKEN_PRIVILEGES tp = new WinNT.TOKEN_PRIVILEGES(1);
        WinNT.TOKEN_PRIVILEGES oldtp = new WinNT.TOKEN_PRIVILEGES(1);
        WinNT.LUID luid = new WinNT.LUID();
        WinNT.HANDLEByReference hTokenRef = new WinNT.HANDLEByReference();
        if (!Advapi32.INSTANCE.OpenProcessToken(Kernel32.INSTANCE.GetCurrentProcess(), WinNT.TOKEN_ADJUST_PRIVILEGES | WinNT.TOKEN_QUERY, hTokenRef)) {
            return false;
        }
        WinNT.HANDLE hToken = hTokenRef.getValue();
        if (!Advapi32.INSTANCE.LookupPrivilegeValue(null, WinNT.SE_DEBUG_NAME, luid)) {
            Kernel32.INSTANCE.CloseHandle(hToken);
            return false;
        }

        tp.PrivilegeCount = new WinDef.DWORD(1);
        tp.Privileges = new WinNT.LUID_AND_ATTRIBUTES[1];
        tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(luid, new WinDef.DWORD(WinNT.SE_PRIVILEGE_ENABLED));

        IntByReference retSize = new IntByReference(0);
        if (!Advapi32.INSTANCE.AdjustTokenPrivileges(hToken, false, tp, tp.size(), oldtp, retSize)) {
            Kernel32.INSTANCE.CloseHandle(hToken);
            return false;
        }
        Kernel32.INSTANCE.CloseHandle(hToken);
        privAdjusted = true;
        return true;
    }
 
Example 6
Source File: Kernel32Lib.java    From sheepit-client with GNU General Public License v2.0 4 votes vote down vote up
public PROCESSENTRY32() {
	dwSize = new WinDef.DWORD(size());
}
 
Example 7
Source File: Kernel32Lib.java    From sheepit-client with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Takes a snapshot of the specified processes, as well as the heaps, modules, and threads used by these processes.
 *
 * @param dwFlags       The portions of the system to be included in the snapshot.
 * @param th32ProcessID The process identifier of the process to be included in the snapshot. This parameter can be zero to indicate
 *                      the current process. This parameter is used when the TH32CS_SNAPHEAPLIST, TH32CS_SNAPMODULE,
 *                      TH32CS_SNAPMODULE32, or TH32CS_SNAPALL value is specified. Otherwise, it is ignored and all processes are
 *                      included in the snapshot.
 *                      <p/>
 *                      If the specified process is the Idle process or one of the CSRSS processes, this function fails and the last
 *                      error code is ERROR_ACCESS_DENIED because their access restrictions prevent user-level code from opening them.
 *                      <p/>
 *                      If the specified process is a 64-bit process and the caller is a 32-bit process, this function fails and the
 *                      last error code is ERROR_PARTIAL_COPY (299).
 * @return If the function succeeds, it returns an open handle to the specified snapshot.
 * <p/>
 * If the function fails, it returns INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.
 * Possible error codes include ERROR_BAD_LENGTH.
 */
public WinNT.HANDLE CreateToolhelp32Snapshot(WinDef.DWORD dwFlags, WinDef.DWORD th32ProcessID);
 
Example 8
Source File: WindowsJNAAffinity.java    From Java-Thread-Affinity with Apache License 2.0 votes vote down vote up
void SetThreadAffinityMask(final int pid, final WinDef.DWORD lpProcessAffinityMask) throws LastErrorException;