📄 kernel32.java
字号:
package org.xvolks.jnative.util;
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
import org.xvolks.jnative.logging.JNativeLogger.SEVERITY;
import org.xvolks.jnative.misc.FreeDiskSpace;
import org.xvolks.jnative.misc.MemoryStatusEx;
import org.xvolks.jnative.misc.SecurityAttributes;
import org.xvolks.jnative.misc.SystemInfo;
import org.xvolks.jnative.misc.basicStructures.AbstractBasicData;
import org.xvolks.jnative.misc.basicStructures.DWORD;
import org.xvolks.jnative.misc.basicStructures.HANDLE;
import org.xvolks.jnative.misc.basicStructures.INT64;
import org.xvolks.jnative.misc.basicStructures.LONG;
import org.xvolks.jnative.pointers.NullPointer;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.HeapMemoryBlock;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;
/**
* Kernel32 this is the class wrapper to Kernel32.dll.<br>
* When a developper needs a function of this DLL (s)he should add it here.
*
* $Id: Kernel32.java,v 1.23 2007/04/27 18:42:03 thubby Exp $;
*
* This software is released under the LGPL.
*
* @author Created by Marc DENTY - (c) 2006 JNative project
*/
public class Kernel32 {
public static final String DLL_NAME = "Kernel32.dll";
private static JNative nOpenProcess;
private static JNative nCloseHandle;
private static JNative nTerminateProcess;
private static JNative nSetLastError;
private static JNative nCreateFileMapping;
private static JNative nMapViewOfFileEx;
private static JNative nGetVersion;
private static JNative nWriteFile;
private static JNative nReadFile;
public static final DWORD IOCTL_STORAGE_EJECT_MEDIA = new DWORD(2967560);
/*
BOOL DeviceIoControl(
HANDLE hDevice,
DWORD dwIoControlCode,
LPVOID lpInBuffer,
DWORD nInBufferSize,
LPVOID lpOutBuffer,
DWORD nOutBufferSize,
LPDWORD lpBytesReturned,
LPOVERLAPPED lpOverlapped
);
*
*Example:
*
try {
HANDLE handle = Kernel32.CreateFile("\\\\.\\d:",
Kernel32.AccessMask.GENERIC_ALL,
Kernel32.ShareMode.FILE_SHARE_VALID_FLAGS,
new SecurityAttributes(),
Kernel32.CreationDisposition.OPEN_EXISTING,
Kernel32.FileAttribute.FILE_ATTRIBUTE_READONLY,0);
if(handle != null) {
Kernel32.DeviceIoControl(handle, Kernel32.IOCTL_STORAGE_EJECT_MEDIA, NullPointer.NULL, new DWORD(0), NullPointer.NULL, new DWORD(0), NullPointer.NULL, NullPointer.NULL);
Kernel32.CloseHandle(handle);
}
} catch(Exception e) {
e.printStackTrace();
}
*/
public static boolean DeviceIoControl(HANDLE hDevice,
DWORD dwIoControlCode,
Pointer lpInBuffer,
DWORD nInBufferSize,
Pointer lpOutBuffer,
DWORD nOutBufferSize,
Pointer lpBytesReturned,
Pointer lpOverlapped)
throws NativeException, IllegalAccessException {
JNative DeviceIoControl = new JNative(DLL_NAME, "DeviceIoControl");
DeviceIoControl.setRetVal(Type.INT);
int pos = 0;
Pointer p = null;
DeviceIoControl.setParameter(pos++, hDevice.getValue());
DeviceIoControl.setParameter(pos++, dwIoControlCode.getValue());
DeviceIoControl.setParameter(pos++, lpInBuffer);
DeviceIoControl.setParameter(pos++, nInBufferSize.getValue());
DeviceIoControl.setParameter(pos++, lpOutBuffer);
DeviceIoControl.setParameter(pos++, nOutBufferSize.getValue());
// if lpOverlapped is NullPointer, lpBytesReturned must not be NullPointer!
// TODO: Is it maybe better here to throw an Exception??
if(lpOverlapped instanceof NullPointer && lpBytesReturned instanceof NullPointer) {
JNative.getLogger().log(SEVERITY.WARN, "lpOverlapped is NullPointer, lpBytesReturned must not be NullPointer!");
p = new Pointer(MemoryBlockFactory.createMemoryBlock(16));
DeviceIoControl.setParameter(pos++, p);
} else
DeviceIoControl.setParameter(pos++, lpBytesReturned);
DeviceIoControl.setParameter(pos++, lpOverlapped);
DeviceIoControl.invoke();
pos = DeviceIoControl.getRetValAsInt();
DeviceIoControl.dispose();
if(p != null)
p.dispose();
return (pos != 0);
}
/*
ATOM GlobalDeleteAtom(
ATOM nAtom
);
*/
public static int GlobalDeleteAtom(int atom) throws NativeException, IllegalAccessException {
JNative GlobalDeleteAtom = new JNative(DLL_NAME, "GlobalDeleteAtom");
GlobalDeleteAtom.setRetVal(Type.INT);
int pos = 0;
GlobalDeleteAtom.setParameter(pos++, atom);
GlobalDeleteAtom.invoke();
pos = GlobalDeleteAtom.getRetValAsInt();
GlobalDeleteAtom.dispose();
return pos;
}
/*
ATOM GlobalAddAtom(
LPCTSTR lpString
);
*/
public static int GlobalAddAtom(String lpString) throws NativeException, IllegalAccessException {
JNative GlobalAddAtom = new JNative(DLL_NAME, "GlobalAddAtomA");
GlobalAddAtom.setRetVal(Type.INT);
int pos = 0;
GlobalAddAtom.setParameter(pos++, lpString);
GlobalAddAtom.invoke();
pos = GlobalAddAtom.getRetValAsInt();
GlobalAddAtom.dispose();
return pos;
}
/*
*returns some Information about the Windows Operating System.
*Look at class WindowsVersion for some convenience methodes
*/
public static DWORD GetVersion() throws NativeException, IllegalAccessException {
if (nGetVersion == null) {
nGetVersion = new JNative(DLL_NAME, "GetVersion");
nGetVersion.setRetVal(Type.INT);
}
nGetVersion.invoke();
return new DWORD(nGetVersion.getRetValAsInt());
}
/*
HANDLE hProcess,
LPCVOID lpBaseAddress,
LPVOID lpBuffer,
SIZE_T nSize,
SIZE_T* lpNumberOfBytesRead
*/
public static boolean ReadProcessMemory(HANDLE hProcess,
int lpBaseAddress,
Pointer lpBuffer,
int len)
throws NativeException, IllegalAccessException {
JNative gms = new JNative(DLL_NAME, "ReadProcessMemory");
gms.setRetVal(Type.INT);
int i = 0;
gms.setParameter(i++, hProcess.getValue());
gms.setParameter(i++, lpBaseAddress);
gms.setParameter(i++, lpBuffer);
gms.setParameter(i++, len);
gms.setParameter(i++, 0);
gms.invoke();
i = gms.getRetValAsInt();
gms.dispose();
return (i != 0);
}
/*
HANDLE hProcess,
LPVOID lpBaseAddress,
LPCVOID lpBuffer,
SIZE_T nSize,
SIZE_T* lpNumberOfBytesWritten
*/
public static boolean WriteProcessMemory(HANDLE hProcess,
int lpBaseAddress,
Pointer lpBuffer,
int len)
throws NativeException, IllegalAccessException {
JNative gms = new JNative(DLL_NAME, "WriteProcessMemory");
gms.setRetVal(Type.INT);
int i = 0;
gms.setParameter(i++, hProcess.getValue());
gms.setParameter(i++, lpBaseAddress);
gms.setParameter(i++, lpBuffer);
gms.setParameter(i++, len);
gms.setParameter(i++, 0);
gms.invoke();
i = gms.getRetValAsInt();
gms.dispose();
return (i != 0);
}
/**
* Method globalMemoryStatusEx
*
* <pre>
* BOOL GlobalMemoryStatusEx(
* LPMEMORYSTATUSEX lpBuffer
* );
*
* Parameters
*
* lpBuffer
* [in, out] Pointer to a MEMORYSTATUSEX structure that receives information about current memory availability.
*
* Return Values
*
* If the function succeeds, the return value is nonzero.
*
* If the function fails, the return value is zero. To get extended error information, call GetLastError.
* Remarks
*
* You can use the GlobalMemoryStatusEx function to determine how much memory your application can allocate without severely impacting other applications.
*
* The information returned by the GlobalMemoryStatusEx function is volatile. There is no guarantee that two sequential calls to this function will return the same information.
* </pre>
*
* @return a MemoryStatusEx
*
* @exception NativeException
* @exception IllegalAccessException
*
*/
public static final MemoryStatusEx globalMemoryStatusEx()
throws NativeException, IllegalAccessException {
JNative gms = new JNative(DLL_NAME, "GlobalMemoryStatusEx");
gms.setRetVal(Type.INT);
MemoryStatusEx mem = new MemoryStatusEx();
gms.setParameter(0, mem.createPointer());
gms.invoke();
if (Integer.parseInt(gms.getRetVal()) == 0) {
gms.dispose();
throw new NativeException(
"The function call has failed. To get extended error information, call GetLastError.");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -