本文整理汇总了Java中com.sun.jna.platform.win32.Kernel32类的典型用法代码示例。如果您正苦于以下问题:Java Kernel32类的具体用法?Java Kernel32怎么用?Java Kernel32使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Kernel32类属于com.sun.jna.platform.win32包,在下文中一共展示了Kernel32类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getDiskSize
import com.sun.jna.platform.win32.Kernel32; //导入依赖的package包/类
private long getDiskSize(Disk disk) {
long result = -1l;
Kernel32 kernel32 = Kernel32.INSTANCE;
HANDLE diskHandle = kernel32.CreateFile(disk.path, WinNT.GENERIC_READ, WinNT.FILE_SHARE_READ, null,
WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
if (WinBase.INVALID_HANDLE_VALUE.equals(diskHandle)) {
return result;
}
try {
Memory output = new Memory(Native.getNativeSize(LARGE_INTEGER.class));
IntByReference lpBytes = new IntByReference();
boolean success = kernel32.DeviceIoControl(diskHandle,
WinioctlUtil.CTL_CODE(Winioctl.FILE_DEVICE_DISK, 0x17, Winioctl.METHOD_BUFFERED,
Winioctl.FILE_READ_ACCESS),
null, 0, output, Native.getNativeSize(LARGE_INTEGER.class), lpBytes, null);
// TODO: Check success?
result = output.getLong(0);
}
finally {
Kernel32Util.closeHandle(diskHandle);
}
return result;
}
示例2: getWindowsProcessId
import com.sun.jna.platform.win32.Kernel32; //导入依赖的package包/类
/**
* @param process NiFi Registry Process Reference
* @param logger Logger Reference for Debug
* @return Returns pid or null in-case pid could not be determined
* This method takes {@link Process} and {@link Logger} and returns
* the platform specific Handle for Win32 Systems, a.k.a <b>pid</b>
* In-case it fails to determine the pid, it will return Null.
* Purpose for the Logger is to log any interaction for debugging.
*/
private static Long getWindowsProcessId(final Process process, final Logger logger) {
/* determine the pid on windows plattforms */
try {
Field f = process.getClass().getDeclaredField("handle");
f.setAccessible(true);
long handl = f.getLong(process);
Kernel32 kernel = Kernel32.INSTANCE;
WinNT.HANDLE handle = new WinNT.HANDLE();
handle.setPointer(Pointer.createConstant(handl));
int ret = kernel.GetProcessId(handle);
logger.debug("Detected pid: {}", ret);
return Long.valueOf(ret);
} catch (final IllegalAccessException | NoSuchFieldException nsfe) {
logger.debug("Could not find PID for child process due to {}", nsfe);
}
return null;
}
示例3: while
import com.sun.jna.platform.win32.Kernel32; //导入依赖的package包/类
/**
* Finds the given process in the process list.
*
* @param processEntry The process entry.
* @param filenamePattern pattern matching the filename of the process.
* @return The found process entry.
*/
public static boolean findProcessEntry
(final Tlhelp32.PROCESSENTRY32.ByReference processEntry,
final Pattern filenamePattern) {
Kernel32 kernel32 = Native.loadLibrary(Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
boolean found = false;
try {
while (kernel32.Process32Next(snapshot, processEntry)) {
String fname = Native.toString(processEntry.szExeFile);
if (fname != null && filenamePattern.matcher(fname).matches()) {
found = true;
break;
}
}
} finally {
kernel32.CloseHandle(snapshot);
}
return found;
}
示例4: executeAsAdmin
import com.sun.jna.platform.win32.Kernel32; //导入依赖的package包/类
public static void executeAsAdmin(String command, String args) {
SHELLEXECUTEINFO execInfo = new SHELLEXECUTEINFO();
execInfo.lpFile = new WString(command);
if (args != null) {
execInfo.lpParameters = new WString(args);
}
execInfo.nShow = Shell32X.SW_SHOWDEFAULT;
execInfo.fMask = Shell32X.SEE_MASK_NOCLOSEPROCESS;
execInfo.lpVerb = new WString("runas");
boolean result = Shell32X.INSTANCE.ShellExecuteEx(execInfo);
if (!result) {
int lastError = Kernel32.INSTANCE.GetLastError();
String errorMessage = Kernel32Util.formatMessageFromLastErrorCode(lastError);
throw new RuntimeException("Error performing elevation: " + lastError + ": " + errorMessage + " (apperror=" + execInfo.hInstApp + ")");
}
}
示例5: getAppUserModelId
import com.sun.jna.platform.win32.Kernel32; //导入依赖的package包/类
public static String getAppUserModelId(HWND hwnd) {
IntByReference processID = new IntByReference();
User32.INSTANCE.GetWindowThreadProcessId(hwnd, processID);
HANDLE hProcess = Kernel32.INSTANCE.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, processID.getValue());
UINTByReference length = new UINTByReference();
Kernel32Ext.INSTANCE.GetApplicationUserModelId(hProcess, length, null);
char[] modelID = new char[length.getValue().intValue()];
Kernel32Ext.INSTANCE.GetApplicationUserModelId(hProcess, length, modelID);
return new String(Native.toString(modelID));
}
示例6: createHotKeys
import com.sun.jna.platform.win32.Kernel32; //导入依赖的package包/类
private void createHotKeys() {
Thread keys = new Thread(() -> {
keysThreadID = Kernel32.INSTANCE.GetCurrentThreadId();
User32.INSTANCE.RegisterHotKey(new HWND(Pointer.NULL), 1, MOD_WIN | MOD_NOREPEAT, VK_E);
MSG msg = new MSG();
while (User32.INSTANCE.GetMessage(msg, new HWND(Pointer.NULL), 0, 0) != 0 && running) {
if (msg.message == WM_HOTKEY) {
try {
switch (msg.wParam.intValue()) {
case 1:
new ProcessBuilder("explorer.exe", ",").start();
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
User32.INSTANCE.UnregisterHotKey(Pointer.NULL, 1);
});
keys.start();
}
示例7: freeSpace
import com.sun.jna.platform.win32.Kernel32; //导入依赖的package包/类
public static long freeSpace(File file)
{
if (!file.isDirectory() || !file.exists())
return -2;
WinNT.LARGE_INTEGER.ByReference lpTotalNumberOfFreeBytes = new WinNT.LARGE_INTEGER.ByReference();
lpTotalNumberOfFreeBytes.clear();
boolean ret = Kernel32.INSTANCE.GetDiskFreeSpaceEx(file.getPath(),
null, null, lpTotalNumberOfFreeBytes);
if (ret)
return lpTotalNumberOfFreeBytes.getValue();
else
{
String s = Kernel32Util
.formatMessageFromLastErrorCode(Kernel32.INSTANCE
.GetLastError());
log.severe("error in File.freeSpace getting for \""
+ file.getPath() + "\" " + s);
}
return -1;
}
示例8: totalSpace
import com.sun.jna.platform.win32.Kernel32; //导入依赖的package包/类
public static long totalSpace(File file)
{
if (!file.isDirectory() || !file.exists())
return -2;
WinNT.LARGE_INTEGER.ByReference lpTotalNumberOfBytes = new WinNT.LARGE_INTEGER.ByReference();
lpTotalNumberOfBytes.clear();
boolean ret = Kernel32.INSTANCE.GetDiskFreeSpaceEx(file.getPath(),
null, lpTotalNumberOfBytes, null);
if (ret)
return lpTotalNumberOfBytes.getValue();
else
{
String s = Kernel32Util
.formatMessageFromLastErrorCode(Kernel32.INSTANCE
.GetLastError());
log.severe("error in File.totalSpace getting for \""
+ file.getPath() + "\" " + s);
}
return -1;
}
示例9: calc
import com.sun.jna.platform.win32.Kernel32; //导入依赖的package包/类
private void calc()
{
if (System.currentTimeMillis() - _lastCall < 500)
return;
WinBase.MEMORYSTATUSEX lpBuffer = new WinBase.MEMORYSTATUSEX();
lpBuffer.dwLength = new DWORD(lpBuffer.size());
if (Kernel32.INSTANCE.GlobalMemoryStatusEx(lpBuffer))
{
lpBuffer.read();
_freeRAM = lpBuffer.ullAvailPhys.longValue();
_totalRAM = lpBuffer.ullTotalPhys.longValue();
_lastCall = System.currentTimeMillis();
}
else
{
if (_logger != null)
_logger.severe("ERROR: could not read free/total RAM");
else
System.out.println("ERROR: could not read free/total RAM");
}
}
示例10: finalize
import com.sun.jna.platform.win32.Kernel32; //导入依赖的package包/类
/**
* Memory disposal.
*
* @throws Throwable
*/
@Override
protected void finalize() throws Throwable {
try {
// Free the memory occupied by the string returned
// from the Win32 function.
Pointer strPointer = getPointerToString();
if (strPointer != null) {
Pointer result = Kernel32.INSTANCE.GlobalFree(strPointer);
if (result != null) {
// The call to GlobalFree has failed. This should never
// happen. If it really does happen, there isn't much we
// can do about it other than logging it.
Logger.log(getClass(), Logger.LogLevel.ERROR,
"Windows function GlobalFree failed while freeing memory for {0} object",
getClass().getSimpleName());
}
}
} finally {
// This will free the memory of the pointer-to-pointer
super.finalize();
}
}
示例11: getProcessId
import com.sun.jna.platform.win32.Kernel32; //导入依赖的package包/类
/**
* Retrieves the process ID (PID) for the specified {@link Process}.
*
* @param process the {@link Process} for whose PID to retrieve.
* @return The PID or zero if the PID couldn't be retrieved.
*/
public static int getProcessId(@Nullable Process process) {
if (process == null) {
return 0;
}
try {
Field field;
if (Platform.isWindows()) {
field = process.getClass().getDeclaredField("handle");
field.setAccessible(true);
int pid = Kernel32.INSTANCE.GetProcessId(new HANDLE(new Pointer(field.getLong(process))));
if (pid == 0 && LOGGER.isDebugEnabled()) {
int lastError = Kernel32.INSTANCE.GetLastError();
LOGGER.debug("KERNEL32.getProcessId() failed with error {}", lastError);
}
return pid;
}
field = process.getClass().getDeclaredField("pid");
field.setAccessible(true);
return field.getInt(process);
} catch (Exception e) {
LOGGER.warn("Failed to get process id for process \"{}\": {}", process, e.getMessage());
LOGGER.trace("", e);
return 0;
}
}
示例12: terminate
import com.sun.jna.platform.win32.Kernel32; //导入依赖的package包/类
public boolean terminate() {
Kernel32.HANDLE process = Kernel32.INSTANCE.OpenProcess(WinNT.PROCESS_TERMINATE | WinNT.SYNCHRONIZE, false, pid);
if (process.getPointer() == null) {
Runner.logger.warn("Unable to find process " + name + "(" + pid + ")");
return false;
} else {
Kernel32.INSTANCE.TerminateProcess(process, 1);
int wait = Kernel32.INSTANCE.WaitForSingleObject(process, 1000);
if (wait != WinBase.WAIT_OBJECT_0) {
Runner.logger.warn("Timed out while waiting for process " + name + "(" + pid + ") to end");
return false;
}
Kernel32.INSTANCE.CloseHandle(process);
return true;
}
}
示例13: getProcessPid
import com.sun.jna.platform.win32.Kernel32; //导入依赖的package包/类
/**
* Returns {@code pid} for Windows process
* @param process Windows process
* @return pid of the {@code process}
*/
public static int getProcessPid(Process process) {
if (process.getClass().getName().equals("java.lang.Win32Process") ||
process.getClass().getName().equals("java.lang.ProcessImpl")) {
try {
long handle = ReflectionUtil.getField(process.getClass(), process, long.class, "handle");
Kernel32 kernel = Kernel32.INSTANCE;
WinNT.HANDLE winHandle = new WinNT.HANDLE();
winHandle.setPointer(Pointer.createConstant(handle));
return kernel.GetProcessId(winHandle);
} catch (Throwable e) {
throw new IllegalStateException(e);
}
} else {
throw new IllegalStateException("Unknown Process implementation");
}
}
示例14: _openFile
import com.sun.jna.platform.win32.Kernel32; //导入依赖的package包/类
@Override
protected void _openFile(String path) throws IOException {
if (file != null) {
throw new IllegalStateException("The file is already open!");
}
file = Kernel32.INSTANCE.CreateFile(
path,
WinNT.GENERIC_WRITE + WinNT.GENERIC_READ,
WinNT.FILE_SHARE_READ,
null,
WinNT.OPEN_ALWAYS,
WinNT.FILE_ATTRIBUTE_NORMAL,
null);
if (WinNT.INVALID_HANDLE_VALUE.equals(file)) {
throw new IOException("Unable to open file: " + getLastErrorAsString());
}
}
示例15: _openMapping
import com.sun.jna.platform.win32.Kernel32; //导入依赖的package包/类
@Override
protected void _openMapping(long size) throws IOException {
if (mapping != null) {
throw new IllegalStateException("File is already mapped!");
}
mapping = Kernel32.INSTANCE.CreateFileMapping(
file,
null,
WinNT.PAGE_READWRITE,
(int) (size >> 8 * 4),
(int) (size & 0xFFFFFFFFL),
null);
if (mapping == null || WinNT.INVALID_HANDLE_VALUE.equals(mapping)) {
throw new IOException("Unable to map file: " + getLastErrorAsString());
} else if (Kernel32.INSTANCE.GetLastError() == WinError.ERROR_ALREADY_EXISTS) {
//File mapping already existing, TODO how to care about?
throw new IOException("ERROR_ALREADY_EXISTS, don't know how to handle that!");
}
}