Search in sources :

Example 21 with CHAR

use of com.sun.jna.platform.win32.WinDef.CHAR in project jna by java-native-access.

the class Advapi32Test method testRegEnumKeyEx.

public void testRegEnumKeyEx() {
    HKEYByReference phKey = new HKEYByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(WinReg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", 0, WinNT.KEY_READ, phKey));
    IntByReference lpcSubKeys = new IntByReference();
    IntByReference lpcMaxSubKeyLen = new IntByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryInfoKey(phKey.getValue(), null, null, null, lpcSubKeys, lpcMaxSubKeyLen, null, null, null, null, null, null));
    char[] name = new char[lpcMaxSubKeyLen.getValue() + 1];
    for (int i = 0; i < lpcSubKeys.getValue(); i++) {
        IntByReference lpcchValueName = new IntByReference(lpcMaxSubKeyLen.getValue() + 1);
        assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegEnumKeyEx(phKey.getValue(), i, name, lpcchValueName, null, null, null, null));
        assertEquals(Native.toString(name).length(), lpcchValueName.getValue());
    }
    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)

Example 22 with CHAR

use of com.sun.jna.platform.win32.WinDef.CHAR 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 23 with CHAR

use of com.sun.jna.platform.win32.WinDef.CHAR 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)

Example 24 with CHAR

use of com.sun.jna.platform.win32.WinDef.CHAR in project jna by java-native-access.

the class Advapi32Test method testLookupAccountSid.

public void testLookupAccountSid() {
    // get SID bytes
    String sidString = EVERYONE;
    PSIDByReference sid = new PSIDByReference();
    assertTrue("Failed to create sid", Advapi32.INSTANCE.ConvertStringSidToSid(sidString, sid));
    PSID value = sid.getValue();
    try {
        int sidLength = Advapi32.INSTANCE.GetLengthSid(value);
        assertTrue("Non-positive sid length", sidLength > 0);
        // lookup account
        IntByReference cchName = new IntByReference();
        IntByReference cchReferencedDomainName = new IntByReference();
        PointerByReference peUse = new PointerByReference();
        assertFalse(Advapi32.INSTANCE.LookupAccountSid(null, value, null, cchName, null, cchReferencedDomainName, peUse));
        assertEquals(W32Errors.ERROR_INSUFFICIENT_BUFFER, Kernel32.INSTANCE.GetLastError());
        assertTrue(cchName.getValue() > 0);
        assertTrue(cchReferencedDomainName.getValue() > 0);
        char[] referencedDomainName = new char[cchReferencedDomainName.getValue()];
        char[] name = new char[cchName.getValue()];
        assertTrue(Advapi32.INSTANCE.LookupAccountSid(null, value, name, cchName, referencedDomainName, cchReferencedDomainName, peUse));
        // SidTypeWellKnownGroup
        assertEquals(5, peUse.getPointer().getInt(0));
        String nameString = Native.toString(name);
        String referencedDomainNameString = Native.toString(referencedDomainName);
        assertTrue(nameString.length() > 0);
        if (AbstractWin32TestSupport.isEnglishLocale) {
            assertEquals("Everyone", nameString);
        }
        assertTrue(referencedDomainNameString.length() == 0);
    } finally {
        Kernel32Util.freeLocalMemory(value.getPointer());
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PSIDByReference(com.sun.jna.platform.win32.WinNT.PSIDByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 25 with CHAR

use of com.sun.jna.platform.win32.WinDef.CHAR in project jna by java-native-access.

the class Kernel32Test method testQueryFullProcessImageName.

public void testQueryFullProcessImageName() {
    int pid = Kernel32.INSTANCE.GetCurrentProcessId();
    HANDLE h = Kernel32.INSTANCE.OpenProcess(WinNT.PROCESS_QUERY_INFORMATION, false, pid);
    assertNotNull("Failed (" + Kernel32.INSTANCE.GetLastError() + ") to get process ID=" + pid + " handle", h);
    try {
        char[] path = new char[WinDef.MAX_PATH];
        IntByReference lpdwSize = new IntByReference(path.length);
        boolean b = Kernel32.INSTANCE.QueryFullProcessImageName(h, 0, path, lpdwSize);
        assertTrue("Failed (" + Kernel32.INSTANCE.GetLastError() + ") to query process image name", b);
        assertTrue("Failed to query process image name, empty path returned", lpdwSize.getValue() > 0);
    } finally {
        Kernel32Util.closeHandle(h);
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)15 HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)7 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)5 VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)4 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)4 File (java.io.File)4 Test (org.junit.Test)4 Memory (com.sun.jna.Memory)3 BSTR (com.sun.jna.platform.win32.WTypes.BSTR)3 BYTE (com.sun.jna.platform.win32.WinDef.BYTE)3 CHAR (com.sun.jna.platform.win32.WinDef.CHAR)3 LONG (com.sun.jna.platform.win32.WinDef.LONG)3 SHORT (com.sun.jna.platform.win32.WinDef.SHORT)3 PSID (com.sun.jna.platform.win32.WinNT.PSID)3 PointerByReference (com.sun.jna.ptr.PointerByReference)3 BufferedWriter (java.io.BufferedWriter)3 FileWriter (java.io.FileWriter)3 PrintWriter (java.io.PrintWriter)3 Pointer (com.sun.jna.Pointer)2 Advapi32 (com.sun.jna.platform.win32.Advapi32)2