com.sun.jna.platform.win32.Kernel32Util Java Examples

The following examples show how to use com.sun.jna.platform.win32.Kernel32Util. 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: WindowsMsync.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
public static void msync(RandomAccessFile raf, long addr, long length)
        throws IOException {
    int retry = 0;
    boolean success;
    int lastError = 0;
    // FlushViewOfFile can fail with ERROR_LOCK_VIOLATION if the memory system is writing dirty
    // pages to disk. As there is no way to synchronize the flushing then we retry a limited
    // number of times.
    do {
        success = KERNEL_32.FlushViewOfFile(new Pointer(addr), new SIZE_T(length));
        if (success || (lastError = KERNEL_32.GetLastError()) != ERROR_LOCK_VIOLATION)
            break;
        retry++;
    } while (retry < 3);

    if (success) {
        // Finally calls FlushFileBuffers
        raf.getChannel().force(false);
    } else {
        throw new IOException(Kernel32Util.formatMessageFromLastErrorCode(lastError));
    }
}
 
Example #2
Source File: ConsumeWindowsEventLog.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor that allows injection of JNA interfaces
 *
 * @param wEvtApi  event api interface
 * @param kernel32 kernel interface
 */
public ConsumeWindowsEventLog(WEvtApi wEvtApi, Kernel32 kernel32) {
    this.wEvtApi = wEvtApi == null ? loadWEvtApi() : wEvtApi;
    this.kernel32 = kernel32 == null ? loadKernel32() : kernel32;
    this.errorLookup = new ErrorLookup(this.kernel32);
    if (this.kernel32 != null) {
        name = Kernel32Util.getComputerName();
    } else {
        // Won't be able to use the processor anyway because native libraries didn't load
        name = null;
    }
}
 
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: SystemService.java    From winthing with Apache License 2.0 5 votes vote down vote up
public void shutdown() throws SystemException {
    final boolean success = advapi32.InitiateSystemShutdown(
        null,
        null,
        new WinDef.DWORD(0),
        true,
        false
    );
    if (!success) {
        throw new SystemException(Kernel32Util.formatMessage(kernel32.GetLastError()));
    }
}
 
Example #5
Source File: SystemService.java    From winthing with Apache License 2.0 5 votes vote down vote up
public void reboot() throws SystemException {
    final boolean success = advapi32.InitiateSystemShutdown(
        null,
        null,
        new WinDef.DWORD(0),
        true,
        true
    );
    if (!success) {
        throw new SystemException(Kernel32Util.formatMessage(kernel32.GetLastError()));
    }
}
 
Example #6
Source File: SystemService.java    From winthing with Apache License 2.0 5 votes vote down vote up
public void suspend() throws SystemException {
    final boolean success = kernel32.SetSystemPowerState(
        true,
        false
    );
    if (!success) {
        throw new SystemException(Kernel32Util.formatMessage(kernel32.GetLastError()));
    }
}
 
Example #7
Source File: SystemService.java    From winthing with Apache License 2.0 5 votes vote down vote up
public void hibernate() throws SystemException {
    final boolean success = kernel32.SetSystemPowerState(
            false,
            false
        );
    if (!success) {
        throw new SystemException(Kernel32Util.formatMessage(kernel32.GetLastError()));
    }
}
 
Example #8
Source File: WinProcess.java    From sheepit-client with GNU General Public License v2.0 5 votes vote down vote up
private static WinNT.HANDLE getHandleByPid(int pid_) throws IOException {
	WinNT.HANDLE handle = Kernel32.INSTANCE.OpenProcess(0x0400 | // PROCESS_QUERY_INFORMATION
					0x0800 | // PROCESS_SUSPEND_RESUME
					0x0001 | // PROCESS_TERMINATE
					0x0200 | // PROCESS_SET_INFORMATION
					0x00100000, // SYNCHRONIZE
			false, pid_);
	if (handle == null) {
		throw new IOException(
				"OpenProcess failed: " + Kernel32Util.formatMessageFromLastErrorCode(Kernel32.INSTANCE.GetLastError()) + " (pid: " + pid_ + ")");
	}
	return handle;
}
 
Example #9
Source File: ConsumeWindowsEventLog.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor that allows injection of JNA interfaces
 *
 * @param wEvtApi  event api interface
 * @param kernel32 kernel interface
 */
public ConsumeWindowsEventLog(WEvtApi wEvtApi, Kernel32 kernel32) {
    this.wEvtApi = wEvtApi == null ? loadWEvtApi() : wEvtApi;
    this.kernel32 = kernel32 == null ? loadKernel32() : kernel32;
    this.errorLookup = new ErrorLookup(this.kernel32);
    if (this.kernel32 != null) {
        name = Kernel32Util.getComputerName();
    } else {
        // Won't be able to use the processor anyway because native libraries didn't load
        name = null;
    }
}
 
Example #10
Source File: JKernel32.java    From Flashtool with GNU General Public License v3.0 4 votes vote down vote up
public static String getLastError() {
	return Kernel32Util.getLastErrorMessage();
}
 
Example #11
Source File: HostnameUtilsWin.java    From netbeans with Apache License 2.0 3 votes vote down vote up
/**
 * Gets the computer name.
 * 
 * <p>This is the also known as the NetBIOS name, although NetBIOS is 
 * hardly used anymore. It is the same value as can be seen from the
 * {@code COMPUTERNAME} environment variable.
 * 
 * <p>
 * Windows API equivalent: {@code GetComputerName()} function from 
 * {@code Kernel32} library.
 * 
 * @return computer name
 * @throws NativeException if there was an error executing the
 *    system call.
 */
public static String getComputerName() throws NativeException {
    try {
        return Kernel32Util.getComputerName();
    } catch (Win32Exception ex) {
        LOGGER.log(Level.FINE, "Kernel32.GetComputerName error : {0}", ex.getHR().intValue());
        String env = System.getenv("COMPUTERNAME");
        if (env != null) {
            return env;
        }
        throw new NativeException(ex.getHR().intValue(), "error calling 'GetComputerName()' function");
    }
}