Search in sources :

Example 96 with Advapi32

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);
        }
    }
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference)

Example 97 with Advapi32

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());
    }
}
Also used : PSIDByReference(com.sun.jna.platform.win32.WinNT.PSIDByReference) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 98 with Advapi32

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);
    }
}
Also used : HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 99 with Advapi32

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();
}
Also used : ULONG(com.sun.jna.platform.win32.WinDef.ULONG) IntByReference(com.sun.jna.ptr.IntByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) Pointer(com.sun.jna.Pointer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ULONGByReference(com.sun.jna.platform.win32.WinDef.ULONGByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) FE_EXPORT_FUNC(com.sun.jna.platform.win32.WinBase.FE_EXPORT_FUNC) File(java.io.File) FE_IMPORT_FUNC(com.sun.jna.platform.win32.WinBase.FE_IMPORT_FUNC)

Example 100 with Advapi32

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()));
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference) IntByReference(com.sun.jna.ptr.IntByReference)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)51 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)39 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)31 HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)31 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)23 PSID (com.sun.jna.platform.win32.WinNT.PSID)20 PointerByReference (com.sun.jna.ptr.PointerByReference)20 Advapi32 (com.sun.jna.platform.win32.Advapi32)15 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)15 File (java.io.File)15 Memory (com.sun.jna.Memory)13 HKEY (com.sun.jna.platform.win32.WinReg.HKEY)13 PSIDByReference (com.sun.jna.platform.win32.WinNT.PSIDByReference)10 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)9 ACL (com.sun.jna.platform.win32.WinNT.ACL)9 SECURITY_DESCRIPTOR (com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR)7 Pointer (com.sun.jna.Pointer)6 ACCESS_ALLOWED_ACE (com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE)6 GENERIC_MAPPING (com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING)6 BOOLByReference (com.sun.jna.platform.win32.WinDef.BOOLByReference)5