下面列出了怎么用com.sun.jna.platform.win32.Kernel32Util的API类实例代码及写法,或者点击链接到github查看源代码。
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));
}
}
/**
* 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;
}
}
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()));
}
}
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()));
}
}
public void suspend() throws SystemException {
final boolean success = kernel32.SetSystemPowerState(
true,
false
);
if (!success) {
throw new SystemException(Kernel32Util.formatMessage(kernel32.GetLastError()));
}
}
public void hibernate() throws SystemException {
final boolean success = kernel32.SetSystemPowerState(
false,
false
);
if (!success) {
throw new SystemException(Kernel32Util.formatMessage(kernel32.GetLastError()));
}
}
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;
}
/**
* 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;
}
}
/**
* 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));
//}
}
}
public static String getLastError() {
return Kernel32Util.getLastErrorMessage();
}
/**
* 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");
}
}