Quantcast
Channel: » Java
Viewing all articles
Browse latest Browse all 2

Bewerken van het register in Java zonder bibliotheken

$
0
0

Ik wil een klein stukje code waarmee je registervermelding te wijzigen op uw Windows-systeem met behulp van post Java without any libraries support.
Dat is de code. Geniet ervan!!

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.prefs.Preferences;

public class RUtils {
public static int laatste HKEY_CURRENT_USER = 0×80000001;
public static int laatste HKEY_LOCAL_MACHINE = 0×80000002;
private static int laatste KEY_ALL_ACCESS = 0xf003f;
private static int laatste KEY_READ = 0×20019;
public static int laatste REG_ACCESSDENIED = 5;
public static int laatste REG_NOTFOUND = 2;
public static int laatste REG_SUCCESS = 0;
private static methode regCloseKey = null;
private static methode regCreateKeyEx = null;
private static methode regDeleteKey = null;
private static methode regDeleteValue = null;
private static methode regEnumKeyEx = null;
private static methode regEnumValue = null;
private static methode regOpenKey = null;
private static methode regQueryInfoKey = null;
private static methode regQueryValueEx = null;
private static methode regSetValueEx = null;
private static Voorkeuren userRoot = Preferences.userRoot();
private static klasse extends Preferences> userClass = userRoot.getClass();
private static Voorkeuren SystemRoot = Preferences.systemRoot();

statische {
probeer {
regOpenKey = userClass.getDeclaredMethod(“WindowsRegOpenKey”, nieuwe klasse[]{int.class, byte[].klasse,
int.class});
regOpenKey.setAccessible(ware);
regCloseKey = userClass.getDeclaredMethod(“WindowsRegCloseKey”, nieuwe klasse[]{int.class});
regCloseKey.setAccessible(ware);
= RegQueryValueEx userClass.getDeclaredMethod(“WindowsRegQueryValueEx”, nieuwe klasse[]{int.class,
byte[].klasse});
regQueryValueEx.setAccessible(ware);
regEnumValue = userClass.getDeclaredMethod(“WindowsRegEnumValue”, nieuwe klasse[]{int.class, int.class,
int.class});
regEnumValue.setAccessible(ware);
regQueryInfoKey = userClass.getDeclaredMethod(“WindowsRegQueryInfoKey1″, nieuwe klasse[]{int.class});
regQueryInfoKey.setAccessible(ware);
regEnumKeyEx = userClass.getDeclaredMethod(“WindowsRegEnumKeyEx”, nieuwe klasse[]{int.class, int.class,
int.class});
regEnumKeyEx.setAccessible(ware);
regCreateKeyEx = userClass.getDeclaredMethod(“WindowsRegCreateKeyEx”, nieuwe klasse[]{int.class,
byte[].klasse});
regCreateKeyEx.setAccessible(ware);
RegSetValueEx = userClass.getDeclaredMethod(“WindowsRegSetValueEx”, nieuwe klasse[]{int.class, byte[].klasse,
byte[].klasse});
regSetValueEx.setAccessible(ware);
regDeleteValue = userClass.getDeclaredMethod(“WindowsRegDeleteValue”, nieuwe klasse[]{int.class,
byte[].klasse});
regDeleteValue.setAccessible(ware);
regDeleteKey = userClass.getDeclaredMethod(“WindowsRegDeleteKey”, nieuwe klasse[]{int.class, byte[].klasse});
regDeleteKey.setAccessible(ware);
} vangst (Uitzondering e) {
e.printStackTrace();
}
}

/**
* Lees een waarde van sleutel en waarde naam
* @ Param HKEY HKEY_CURRENT_USER / HKEY_LOCAL_MACHINE
* @ Param key
* @ Param waardenaam
* @ Return de waarde
* @ Throws IllegalArgumentException
* @ Throws IllegalAccessException
* @ Throws InvocationTargetException
*/
public static String readString(int HKEY, String-toets, String waardenaam)
gooit IllegalArgumentException, IllegalAccessException, InvocationTargetException {
als (HKEY == HKEY_LOCAL_MACHINE) {
terug readString(SystemRoot, HKEY, -toets, waardenaam);
} else if (HKEY == HKEY_CURRENT_USER) {
terug readString(userRoot, HKEY, -toets, waardenaam);
} anders {
throw new IllegalArgumentException(“HKEY =” + HKEY);
}
}

/**
* Lees de waarde(met) and value name(met) vorm gegeven sleutel
* @ Param HKEY HKEY_CURRENT_USER / HKEY_LOCAL_MACHINE
* @ Param key
* @ Return de waarde naam(met) plus the value(met)
* @ Throws IllegalArgumentException
* @ Throws IllegalAccessException
* @ Throws InvocationTargetException
*/
public static Kaart readStringValues(int HKEY, String-toets)
gooit IllegalArgumentException, IllegalAccessException, InvocationTargetException {
als (HKEY == HKEY_LOCAL_MACHINE) {
terug readStringValues(SystemRoot, HKEY, -toets);
} else if (HKEY == HKEY_CURRENT_USER) {
terug readStringValues(userRoot, HKEY, -toets);
} anders {
throw new IllegalArgumentException(“HKEY =” + HKEY);
}
}

/**
* Lees de waarde naam(met) van een bepaalde sleutel
* @ Param HKEY HKEY_CURRENT_USER / HKEY_LOCAL_MACHINE
* @ Param key
* @ Return de waarde naam(met)
* @ Throws IllegalArgumentException
* @ Throws IllegalAccessException
* @ Throws InvocationTargetException
*/
public static Lijst readStringSubKeys(int HKEY, String-toets)
gooit IllegalArgumentException, IllegalAccessException, InvocationTargetException {
als (HKEY == HKEY_LOCAL_MACHINE) {
terug readStringSubKeys(SystemRoot, HKEY, -toets);
} else if (HKEY == HKEY_CURRENT_USER) {
terug readStringSubKeys(userRoot, HKEY, -toets);
} anders {
throw new IllegalArgumentException(“HKEY =” + HKEY);
}
}

/**
* Maak een toets
* @ Param HKEY HKEY_CURRENT_USER / HKEY_LOCAL_MACHINE
* @ Param key
* @ Throws IllegalArgumentException
* @ Throws IllegalAccessException
* @ Throws InvocationTargetException
*/
public static void createKey(int HKEY, String-toets)
gooit IllegalArgumentException, IllegalAccessException, InvocationTargetException {
int[] rechts;

als (HKEY == HKEY_LOCAL_MACHINE) {
rechts = createKey(SystemRoot, HKEY, -toets);
regCloseKey.invoke(SystemRoot, new Object[]{nieuwe Integer(rechts[0])});
} else if (HKEY == HKEY_CURRENT_USER) {
rechts = createKey(userRoot, HKEY, -toets);
regCloseKey.invoke(userRoot, new Object[]{nieuwe Integer(rechts[0])});
} anders {
throw new IllegalArgumentException(“HKEY =” + HKEY);
}

als (rechts[1] != REG_SUCCESS) {
throw new IllegalArgumentException(“rc =” + rechts[1] + ” key =” + -toets);
}
}

/**
* Schrijf een waarde in een bepaalde sleutel / waarde naam
* @ Param HKEY
* @ Param key
* @ Param waardenaam
* @ Param waarde
* @ Throws IllegalArgumentException
* @ Throws IllegalAccessException
* @ Throws InvocationTargetException
*/
public static void writeStringValue(int HKEY, String-toets, String waardenaam, Tekenreekswaarde)
gooit IllegalArgumentException, IllegalAccessException, InvocationTargetException {
als (HKEY == HKEY_LOCAL_MACHINE) {
writeStringValue(SystemRoot, HKEY, -toets, waardenaam, waarde);
} else if (HKEY == HKEY_CURRENT_USER) {
writeStringValue(userRoot, HKEY, -toets, waardenaam, waarde);
} anders {
throw new IllegalArgumentException(“HKEY =” + HKEY);
}
}

/**
* Verwijderen van een bepaalde sleutel
* @ Param HKEY
* @ Param key
* @ Throws IllegalArgumentException
* @ Throws IllegalAccessException
* @ Throws InvocationTargetException
*/
public static void deleteKey(int HKEY, String-toets)
gooit IllegalArgumentException, IllegalAccessException, InvocationTargetException {
int rc = -1;

als (HKEY == HKEY_LOCAL_MACHINE) {
rc = deleteKey(SystemRoot, HKEY, -toets);
} else if (HKEY == HKEY_CURRENT_USER) {
rc = deleteKey(userRoot, HKEY, -toets);
}

als (rc != REG_SUCCESS) {
throw new IllegalArgumentException(“rc =” + rc + ” key =” + -toets);
}
}

/**
* verwijderen van een waarde uit een bepaalde sleutel / waarde naam
* @ Param HKEY
* @ Param key
* @ Param waarde
* @ Throws IllegalArgumentException
* @ Throws IllegalAccessException
* @ Throws InvocationTargetException
*/
public static void deletevalue(int HKEY, String-toets, Tekenreekswaarde)
gooit IllegalArgumentException, IllegalAccessException, InvocationTargetException {
int rc = -1;

als (HKEY == HKEY_LOCAL_MACHINE) {
rc = deletevalue(SystemRoot, HKEY, -toets, waarde);
} else if (HKEY == HKEY_CURRENT_USER) {
rc = deletevalue(userRoot, HKEY, -toets, waarde);
}

als (rc != REG_SUCCESS) {
throw new IllegalArgumentException(“rc =” + rc + ” key =” + -toets + ” waarde =” + waarde);
}
}

// =====================
private static int deletevalue(Voorkeuren root, int HKEY, String-toets, Tekenreekswaarde)
gooit IllegalArgumentException, IllegalAccessException, InvocationTargetException {
int[] handles = (int[]) regOpenKey.invoke(root, new Object[]{nieuwe Integer(HKEY), toCstr(-toets),
nieuwe Integer(KEY_ALL_ACCESS)});

als (handgrepen[1] != REG_SUCCESS) {
terug handgrepen[1]; // kan worden REG_NOTFOUND, REG_ACCESSDENIED
}

int rc = ((Geheel getal) regDeleteValue.invoke(root,
new Object[]{nieuwe Integer(handgrepen[0]), toCstr(waarde)})).intValue();

regCloseKey.invoke(root, new Object[]{nieuwe Integer(handgrepen[0])});

terug rc;
}

private static int deleteKey(Voorkeuren root, int HKEY, String-toets)
gooit IllegalArgumentException, IllegalAccessException, InvocationTargetException {
int rc = ((Geheel getal) regDeleteKey.invoke(root, new Object[]{nieuwe Integer(HKEY), toCstr(-toets)})).intValue();

terug rc; // kan REG_NOTFOUND, REG_ACCESSDENIED, REG_SUCCESS
}

private static String readString(Voorkeuren root, int HKEY, String-toets, Tekenreekswaarde)
gooit IllegalArgumentException, IllegalAccessException, InvocationTargetException {
int[] handles = (int[]) regOpenKey.invoke(root, new Object[]{nieuwe Integer(HKEY), toCstr(-toets),
nieuwe Integer(KEY_READ)});

als (handgrepen[1] != REG_SUCCESS) {
terug null;
}

byte[] valb = (byte[]) regQueryValueEx.invoke(root, new Object[]{nieuwe Integer(handgrepen[0]), toCstr(waarde)});

regCloseKey.invoke(root, new Object[]{nieuwe Integer(handgrepen[0])});

terug ((valb != Null)
? nieuwe String(valb).trimmen()
: nietig);
}

private static Kaart readStringValues(Voorkeuren root, int HKEY, String-toets)
gooit IllegalArgumentException, IllegalAccessException, InvocationTargetException {
HashMap resultaten = nieuwe HashMap();
int[] handles = (int[]) regOpenKey.invoke(root, new Object[]{nieuwe Integer(HKEY), toCstr(-toets),
nieuwe Integer(KEY_READ)});

als (handgrepen[1] != REG_SUCCESS) {
terug null;
}

int[] info = (int[]) regQueryInfoKey.invoke(root, new Object[]{nieuwe Integer(handgrepen[0])});
int count = info[2]; // tellen
int maxlen = info[3]; // waarde max

voor (int index = 0; index byte[] name = (byte[]) regEnumValue.invoke(root, new Object[]{nieuwe Integer(handgrepen[0]), nieuwe Integer(index),
nieuwe Integer(maxlen + 1)});
String waarde = readString(HKEY, -toets, nieuwe String(naam));

results.put(nieuwe String(naam).trimmen(), waarde);
}

regCloseKey.invoke(root, new Object[]{nieuwe Integer(handgrepen[0])});

rendement resultaten;
}

private static Lijst readStringSubKeys(Voorkeuren root, int HKEY, String-toets)
gooit IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Lijst resultaten = new ArrayList();
int[] handles = (int[]) regOpenKey.invoke(root, new Object[]{nieuwe Integer(HKEY), toCstr(-toets),
nieuwe Integer(KEY_READ)});

als (handgrepen[1] != REG_SUCCESS) {
terug null;
}

int[] info = (int[]) regQueryInfoKey.invoke(root, new Object[]{nieuwe Integer(handgrepen[0])});
int count = info[2]; // tellen
int maxlen = info[3]; // waarde max

voor (int index = 0; index byte[] name = (byte[]) regEnumKeyEx.invoke(root, new Object[]{nieuwe Integer(handgrepen[0]), nieuwe Integer(index),
nieuwe Integer(maxlen + 1)});

results.add(nieuwe String(naam).trimmen());
}

regCloseKey.invoke(root, new Object[]{nieuwe Integer(handgrepen[0])});

rendement resultaten;
}

private static int[] createKey(Voorkeuren root, int HKEY, String-toets)
gooit IllegalArgumentException, IllegalAccessException, InvocationTargetException {
terug (int[]) regCreateKeyEx.invoke(root, new Object[]{nieuwe Integer(HKEY), toCstr(-toets)});
}

private static void writeStringValue(Voorkeuren root, int HKEY, String-toets, String waardenaam, Tekenreekswaarde)
gooit IllegalArgumentException, IllegalAccessException, InvocationTargetException {
int[] handles = (int[]) regOpenKey.invoke(root, new Object[]{nieuwe Integer(HKEY), toCstr(-toets),
nieuwe Integer(KEY_ALL_ACCESS)});

regSetValueEx.invoke(root, new Object[]{nieuwe Integer(handgrepen[0]), toCstr(waardenaam), toCstr(waarde)});
regCloseKey.invoke(root, new Object[]{nieuwe Integer(handgrepen[0])});
}

// hulpprogramma
private static byte[] toCstr(String str) {
byte[] resultaat = nieuwe byte[str.length() + 1];

voor (int i = 0; in resultaat[in] = (byte) str.charAt(in);
}

resultaat[str.length()] = 0;

rendement leiden;
}
}

The post Bewerken van het register in Java zonder bibliotheken appeared first on .


Viewing all articles
Browse latest Browse all 2

Latest Images





Latest Images