use of com.sun.jna.platform.win32.WinReg.HKEY in project jna by java-native-access.
the class SetupApiTest method testSetupDiOpenDevRegKey.
/**
* Tests the mapping of {@link SetupApi#SetupDiOpenDevRegKey(HANDLE, SP_DEVINFO_DATA, int, int, int, int)} .
* <p>
* The test pass if SetupDiOpenDevRegKey(..) returns a valid {@link HKEY} pointing to the first found device on the current machine.
* <p>
* NOTE: We only test the mapping of SetupDiOpenDevRegKey(..), not it's functionality.
*/
public void testSetupDiOpenDevRegKey() {
// hDevInfoSet repesents a list of installed devices for all device
// setup classes or all device interface classes
HANDLE hDevInfoSet = SetupApi.INSTANCE.SetupDiGetClassDevs(null, null, null, DIGCF_ALLCLASSES);
assertTrue(hDevInfoSet != INVALID_HANDLE_VALUE);
SP_DEVINFO_DATA devInfo = new SP_DEVINFO_DATA();
// there must be least one device (drive,processor,pci,usb,...) on the
// current machine
assertTrue(SetupApi.INSTANCE.SetupDiEnumDeviceInfo(hDevInfoSet, FIRST_MEMBER, devInfo));
HKEY hDeviceKey = SetupApi.INSTANCE.SetupDiOpenDevRegKey(hDevInfoSet, devInfo, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE);
assertTrue(hDeviceKey != INVALID_HANDLE_VALUE);
Advapi32.INSTANCE.RegCloseKey(hDeviceKey);
}
use of com.sun.jna.platform.win32.WinReg.HKEY in project jna by java-native-access.
the class Advapi32Util method registryGetKey.
/**
* Get a registry key, the caller is responsible to close the key after use.
*
* @param root
* Root key.
* @param keyPath
* Path to a registry key.
*
* @param samDesired
* Access level (e.g. WinNT.KEY_READ)
*
* @return HKEYByReference.
*/
public static HKEYByReference registryGetKey(HKEY root, String keyPath, int samDesired) {
HKEYByReference phkKey = new HKEYByReference();
int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, samDesired, phkKey);
if (rc != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(rc);
}
return phkKey;
}
use of com.sun.jna.platform.win32.WinReg.HKEY in project jna by java-native-access.
the class Advapi32Util method registryDeleteValue.
/**
* Delete a registry value.
*
* @param root
* Root key.
* @param keyPath
* Path to an existing registry key.
* @param valueName
* Name of the value to delete.
*/
public static void registryDeleteValue(HKEY root, String keyPath, String valueName) {
HKEYByReference phkKey = new HKEYByReference();
int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE, phkKey);
if (rc != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(rc);
}
try {
registryDeleteValue(phkKey.getValue(), valueName);
} finally {
rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue());
if (rc != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(rc);
}
}
}
use of com.sun.jna.platform.win32.WinReg.HKEY in project jna by java-native-access.
the class Advapi32Util method registryDeleteKey.
/**
* Delete a registry key.
*
* @param root
* Root key.
* @param keyPath
* Path to an existing registry key.
* @param keyName
* Name of the key to delete.
*/
public static void registryDeleteKey(HKEY root, String keyPath, String keyName) {
HKEYByReference phkKey = new HKEYByReference();
int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE, phkKey);
if (rc != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(rc);
}
try {
registryDeleteKey(phkKey.getValue(), keyName);
} finally {
rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue());
if (rc != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(rc);
}
}
}
use of com.sun.jna.platform.win32.WinReg.HKEY in project jna by java-native-access.
the class Advapi32Util method registrySetIntValue.
/**
* Set an integer value in registry.
*
* @param root
* Root key.
* @param keyPath
* Path to an existing registry key.
* @param name
* Value name.
* @param value
* Value to write to registry.
*/
public static void registrySetIntValue(HKEY root, String keyPath, String name, int value) {
HKEYByReference phkKey = new HKEYByReference();
int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE, phkKey);
if (rc != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(rc);
}
try {
registrySetIntValue(phkKey.getValue(), name, value);
} finally {
rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue());
if (rc != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(rc);
}
}
}
Aggregations