use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.
the class Advapi32UtilTest method registrySetEmptyValue.
private static void registrySetEmptyValue(HKEY root, String keyPath, String name, final int valueType) {
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 {
char[] data = new char[0];
rc = Advapi32.INSTANCE.RegSetValueEx(phkKey.getValue(), name, 0, valueType, data, 0);
if (rc != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(rc);
}
} finally {
rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue());
if (rc != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(rc);
}
}
}
use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.
the class Advapi32Test method testGetSidLength.
public void testGetSidLength() {
String sidString = EVERYONE;
PSIDByReference sid = new PSIDByReference();
assertTrue("SID conversion failed", Advapi32.INSTANCE.ConvertStringSidToSid(sidString, sid));
PSID value = sid.getValue();
try {
assertEquals("Wrong SID length", 12, Advapi32.INSTANCE.GetLengthSid(value));
} finally {
Kernel32Util.freeLocalMemory(value.getPointer());
}
}
use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.
the class Advapi32Test method testSetThreadTokenCurrentThread.
public void testSetThreadTokenCurrentThread() {
HANDLEByReference phToken = new HANDLEByReference();
HANDLEByReference phTokenDup = new HANDLEByReference();
try {
HANDLE threadHandle = Kernel32.INSTANCE.GetCurrentThread();
// See if thread has a token. If not, must duplicate process token and set thread token using that.
if (!Advapi32.INSTANCE.OpenThreadToken(threadHandle, WinNT.TOKEN_IMPERSONATE | WinNT.TOKEN_QUERY, false, phToken)) {
assertEquals(W32Errors.ERROR_NO_TOKEN, Kernel32.INSTANCE.GetLastError());
HANDLE processHandle = Kernel32.INSTANCE.GetCurrentProcess();
assertTrue(Advapi32.INSTANCE.OpenProcessToken(processHandle, WinNT.TOKEN_DUPLICATE, phToken));
assertTrue(Advapi32.INSTANCE.DuplicateTokenEx(phToken.getValue(), WinNT.TOKEN_IMPERSONATE, null, WinNT.SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, WinNT.TOKEN_TYPE.TokenImpersonation, phTokenDup));
// Null sets on current thread
assertTrue(Advapi32.INSTANCE.SetThreadToken(null, phTokenDup.getValue()));
} else {
//Null sets on current thread
assertTrue(Advapi32.INSTANCE.SetThreadToken(null, phToken.getValue()));
}
// Revert and cleanup
assertTrue(Advapi32.INSTANCE.SetThreadToken(null, null));
} finally {
Kernel32Util.closeHandleRefs(phToken, phTokenDup);
}
}
use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.
the class Advapi32Test method testWriteEncryptedFileRaw.
public void testWriteEncryptedFileRaw() throws Exception {
// create an encrypted file
File file = createTempFile();
String lpFileName = file.getAbsolutePath();
assertTrue(Advapi32.INSTANCE.EncryptFile(lpFileName));
// open file for export
ULONG ulFlags = new ULONG(0);
PointerByReference pvContext = new PointerByReference();
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.OpenEncryptedFileRaw(lpFileName, ulFlags, pvContext));
// read encrypted file
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
FE_EXPORT_FUNC pfExportCallback = new FE_EXPORT_FUNC() {
@Override
public DWORD callback(Pointer pbData, Pointer pvCallbackContext, ULONG ulLength) {
if (pbData == null) {
throw new NullPointerException("Callback data unexpectedly null");
}
byte[] arr = pbData.getByteArray(0, ulLength.intValue());
try {
outputStream.write(arr);
} catch (IOException e) {
throw new RuntimeException(e);
}
return new DWORD(W32Errors.ERROR_SUCCESS);
}
};
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.ReadEncryptedFileRaw(pfExportCallback, null, pvContext.getValue()));
outputStream.close();
Advapi32.INSTANCE.CloseEncryptedFileRaw(pvContext.getValue());
// open file for import
String lbFileName2 = System.getProperty("java.io.tmpdir") + File.separator + "backup-" + file.getName();
ULONG ulFlags2 = new ULONG(CREATE_FOR_IMPORT);
PointerByReference pvContext2 = new PointerByReference();
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.OpenEncryptedFileRaw(lbFileName2, ulFlags2, pvContext2));
// write encrypted file
final IntByReference elementsReadWrapper = new IntByReference(0);
FE_IMPORT_FUNC pfImportCallback = new FE_IMPORT_FUNC() {
@Override
public DWORD callback(Pointer pbData, Pointer pvCallbackContext, ULONGByReference ulLength) {
int elementsRead = elementsReadWrapper.getValue();
int remainingElements = outputStream.size() - elementsRead;
int length = Math.min(remainingElements, ulLength.getValue().intValue());
pbData.write(0, outputStream.toByteArray(), elementsRead, length);
elementsReadWrapper.setValue(elementsRead + length);
ulLength.setValue(new ULONG(length));
return new DWORD(W32Errors.ERROR_SUCCESS);
}
};
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.WriteEncryptedFileRaw(pfImportCallback, null, pvContext2.getValue()));
Advapi32.INSTANCE.CloseEncryptedFileRaw(pvContext2.getValue());
file.delete();
new File(lbFileName2.toString()).delete();
}
use of com.sun.jna.platform.win32.Advapi32 in project jna by java-native-access.
the class Advapi32Test method testRegSetValueEx_REG_SZ.
public void testRegSetValueEx_REG_SZ() {
HKEYByReference phKey = new HKEYByReference();
// create parent key
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(WinReg.HKEY_CURRENT_USER, "Software", 0, WinNT.KEY_WRITE | WinNT.KEY_READ, phKey));
HKEYByReference phkTest = new HKEYByReference();
IntByReference lpdwDisposition = new IntByReference();
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCreateKeyEx(phKey.getValue(), "JNAAdvapi32Test", 0, null, 0, WinNT.KEY_ALL_ACCESS, null, phkTest, lpdwDisposition));
// write a REG_SZ value
char[] lpData = Native.toCharArray("Test");
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegSetValueEx(phkTest.getValue(), "REG_SZ", 0, WinNT.REG_SZ, lpData, lpData.length * 2));
// re-read the REG_SZ value
IntByReference lpType = new IntByReference();
IntByReference lpcbData = new IntByReference();
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(phkTest.getValue(), "REG_SZ", 0, lpType, (char[]) null, lpcbData));
assertEquals(WinNT.REG_SZ, lpType.getValue());
assertTrue(lpcbData.getValue() > 0);
char[] buffer = new char[lpcbData.getValue()];
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(phkTest.getValue(), "REG_SZ", 0, lpType, buffer, lpcbData));
assertEquals("Test", Native.toString(buffer));
// delete the test key
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phkTest.getValue()));
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegDeleteKey(phKey.getValue(), "JNAAdvapi32Test"));
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));
}
Aggregations