📄 advapi32.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.misc.HKEY;
import org.xvolks.jnative.misc.REGSAM;
import org.xvolks.jnative.misc.registry.RegData;
import org.xvolks.jnative.misc.registry.RegKey;
import org.xvolks.jnative.misc.registry.RegQueryKey;
import org.xvolks.jnative.misc.registry.RegValue;
import org.xvolks.jnative.misc.registry.RegValueTypes;
import org.xvolks.jnative.pointers.NullPointer;
import org.xvolks.jnative.misc.basicStructures.*;
import org.xvolks.jnative.misc.registry.REGTYPE;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;
/**
*
* $Id: Advapi32.java,v 1.9 2007/04/27 18:44:43 thubby Exp $
*
* This software is released under the LGPL.
* @author Created by Marc DENTY - (c) 2006 JNative project
*/
public class Advapi32 {
public static final String DLL_NAME = "Advapi32.dll.";
private static String mLastErrorCode = "0";
private static JNative nRegOpenKeyEx;
private static JNative nRegCloseKey;
private static JNative nRegEnumKey;
private static JNative nRegQueryInfoKey;
private static JNative nRegQueryValueEx;
private static JNative nRegGetValue;
private static JNative nRegEnumValue;
private static JNative nRegCreateKeyEx;
private static JNative nRegSetValueExA;
private static JNative nRegDeleteKey;
private static JNative nRegDeleteValue;
private static JNative nOpenProcessToken;
private static JNative nGetCurrentProcess;
private static JNative nLookupPrivilegeValue;
private static JNative nAdjustTokenPrivileges;
/*
HANDLE TokenHandle,
BOOL DisableAllPrivileges,
PTOKEN_PRIVILEGES NewState,
DWORD BufferLength,
PTOKEN_PRIVILEGES PreviousState,
PDWORD ReturnLength
*/
public static boolean AdjustTokenPrivileges(int TokenHandle,
boolean DisableAllPrivileges,
TOKEN_PRIVILEGES NewState) throws NativeException, IllegalAccessException {
if (nAdjustTokenPrivileges == null) {
nAdjustTokenPrivileges = new JNative(DLL_NAME, "AdjustTokenPrivileges");
nAdjustTokenPrivileges.setRetVal(Type.INT);
}
int i = 0;
nAdjustTokenPrivileges.setParameter(i++, TokenHandle);
nAdjustTokenPrivileges.setParameter(i++, DisableAllPrivileges ? -1 : 0);
nAdjustTokenPrivileges.setParameter(i++, NewState.getPointer());
nAdjustTokenPrivileges.setParameter(i++, 0);
nAdjustTokenPrivileges.setParameter(i++, NullPointer.NULL);
nAdjustTokenPrivileges.setParameter(i++, 0);
nAdjustTokenPrivileges.invoke();
mLastErrorCode = nAdjustTokenPrivileges.getRetVal();
if (!"0".equals(mLastErrorCode)) {
return true;
} else {
return false;
}
}
/*
LPCTSTR lpSystemName,
LPCTSTR lpName,
PLUID lpLuid
*/
public static int LookupPrivilegeValue(String lpSystemName, String lpName) throws NativeException, IllegalAccessException {
if (nLookupPrivilegeValue == null) {
nLookupPrivilegeValue = new JNative(DLL_NAME, "LookupPrivilegeValueA"); // Use ANSI
nLookupPrivilegeValue.setRetVal(Type.INT);
}
Pointer p = new Pointer(MemoryBlockFactory.createMemoryBlock(4));
int i = 0;
nLookupPrivilegeValue.setParameter(i++, lpSystemName);
nLookupPrivilegeValue.setParameter(i++,lpName);
nLookupPrivilegeValue.setParameter(i++, p.getPointer());
nLookupPrivilegeValue.invoke();
mLastErrorCode = nLookupPrivilegeValue.getRetVal();
int ret = p.getAsInt(0);
p.dispose();
return ret;
}
/*
HANDLE ProcessHandle,
DWORD DesiredAccess,
PHANDLE TokenHandle
*/
public static int OpenProcessToken(int ProcessHandle, int DesiredAccess) throws NativeException, IllegalAccessException {
if (nOpenProcessToken == null) {
nOpenProcessToken = new JNative(DLL_NAME, "OpenProcessToken");
nOpenProcessToken.setRetVal(Type.INT);
}
Pointer p = new Pointer(MemoryBlockFactory.createMemoryBlock(4));
int i = 0;
nOpenProcessToken.setParameter(i++, ProcessHandle);
nOpenProcessToken.setParameter(i++, DesiredAccess);
nOpenProcessToken.setParameter(i++, p);
nOpenProcessToken.invoke();
mLastErrorCode = nOpenProcessToken.getRetVal();
int ret = p.getAsInt(0);
p.dispose();
return ret;
}
public static int GetCurrentProcess() throws NativeException, IllegalAccessException {
if (nGetCurrentProcess == null) {
nGetCurrentProcess = new JNative("kernel32.dll", "GetCurrentProcess");
nGetCurrentProcess.setRetVal(Type.INT);
}
nGetCurrentProcess.invoke();
return Integer.parseInt(nGetCurrentProcess.getRetVal());
}
public static boolean RegDeleteValue(HKEY hKey,
String lpSubKey
) throws NativeException, IllegalAccessException {
if (nRegDeleteValue == null) {
nRegDeleteValue = new JNative(DLL_NAME, "RegDeleteValueA");
nRegDeleteValue.setRetVal(Type.INT);
}
int i = 0;
nRegDeleteValue.setParameter(i++, hKey.getValue());
nRegDeleteValue.setParameter(i++, Type.STRING, lpSubKey);
nRegDeleteValue.invoke();
mLastErrorCode = nRegDeleteValue.getRetVal();
if ("0".equals(mLastErrorCode)) {
return true;
} else {
return false;
}
}
public static boolean RegDeleteKey(HKEY hKey,
String lpSubKey)
throws NativeException, IllegalAccessException {
if (nRegDeleteKey == null) {
nRegDeleteKey = new JNative(DLL_NAME, "RegDeleteKeyA");
nRegDeleteKey.setRetVal(Type.INT);
}
int i = 0;
nRegDeleteKey.setParameter(i++, hKey.getValue());
nRegDeleteKey.setParameter(i++, Type.STRING, lpSubKey);
nRegDeleteKey.invoke();
mLastErrorCode = nRegDeleteKey.getRetVal();
if ("0".equals(mLastErrorCode))
return true;
return false;
}
/*
HKEY hKey,
LPCTSTR lpValueName,
DWORD Reserved,
DWORD dwType,
const BYTE* lpData,
DWORD cbData
*/
public static boolean RegSetValueEx(HKEY hKey, String lpValueName, String value, REGTYPE type) throws Exception {
if (nRegSetValueExA == null) {
nRegSetValueExA = new JNative(DLL_NAME, "RegSetValueExA");
nRegSetValueExA.setRetVal(Type.INT);
}
int i = 0;
nRegSetValueExA.setParameter(i++, hKey.getValue());
nRegSetValueExA.setParameter(i++, Type.STRING, lpValueName);
nRegSetValueExA.setParameter(i++, 0);
nRegSetValueExA.setParameter(i++, type.getValue());
if(type.equals(REGTYPE.REG_DWORD)) {
nRegSetValueExA.setParameter(i++, Type.STRING, getBytes(Integer.parseInt(value)));
nRegSetValueExA.setParameter(i++, type.getSize());
} else {
nRegSetValueExA.setParameter(i++, Type.STRING, value);
nRegSetValueExA.setParameter(i++, value.length());
}
nRegSetValueExA.invoke();
mLastErrorCode = nRegSetValueExA.getRetVal();
if ("0".equals(mLastErrorCode))
return true;
return false;
}
public static final byte[] getBytes(int value) {
byte[] x = new byte[4];
x[0] = (byte)(value );
x[1] = (byte)(value >> 8);
x[2] = (byte)(value >> 16);
x[3] = (byte)(value >> 24);
return x;
}
/*
HKEY hKey,
LPCTSTR lpSubKey,
DWORD Reserved,
LPTSTR lpClass,
DWORD dwOptions,
REGSAM samDesired,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
PHKEY phkResult,
LPDWORD lpdwDisposition
*/
public static HKEY RegCreateKeyEx(HKEY hKey,
String lpSubKey,
REGSAM samDesired
) throws NativeException, IllegalAccessException {
if (nRegCreateKeyEx == null) {
nRegCreateKeyEx = new JNative(DLL_NAME, "RegCreateKeyExA");
nRegCreateKeyEx.setRetVal(Type.INT);
}
HKEY phkResult = new HKEY(0);
int i = 0;
nRegCreateKeyEx.setParameter(i++, hKey.getValue());
nRegCreateKeyEx.setParameter(i++, Type.STRING, lpSubKey);
nRegCreateKeyEx.setParameter(i++, 0);
nRegCreateKeyEx.setParameter(i++, 0);
nRegCreateKeyEx.setParameter(i++, 0);
nRegCreateKeyEx.setParameter(i++, samDesired.getValue());
nRegCreateKeyEx.setParameter(i++, 0);
nRegCreateKeyEx.setParameter(i++, phkResult.getPointer());
nRegCreateKeyEx.setParameter(i++, 0);
nRegCreateKeyEx.invoke();
mLastErrorCode = nRegCreateKeyEx.getRetVal();
if ("0".equals(mLastErrorCode))
return phkResult;
return null;
}
public static boolean doesKeyExist(HKEY base, String regKey) throws Exception {
HKEY key = null;
try {
key = Advapi32.RegOpenKeyEx(base,regKey, REGSAM.KEY_READ);
if(key != null) {
return true;
}
} catch(Exception e) {
throw e;
} finally {
closeKey(key);
}
return false;
}
public static void closeKey(HKEY key) {
try {
if(key != null)
Advapi32.RegCloseKey(key);
} catch(Exception e) {
e.printStackTrace();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -