Search in sources :

Example 1 with CHAR

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

the class Advapi32Test method testRegQueryValueEx.

public void testRegQueryValueEx() {
    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 lpcbData = new IntByReference();
    IntByReference lpType = new IntByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(phKey.getValue(), "User Agent", 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(phKey.getValue(), "User Agent", 0, lpType, buffer, lpcbData));
    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 2 with CHAR

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

the class Advapi32Test method testLookupAccountName.

public void testLookupAccountName() {
    IntByReference pSid = new IntByReference(0);
    IntByReference pDomain = new IntByReference(0);
    PointerByReference peUse = new PointerByReference();
    String accountName = "Administrator";
    assertFalse(Advapi32.INSTANCE.LookupAccountName(null, accountName, null, pSid, null, pDomain, peUse));
    assertEquals(W32Errors.ERROR_INSUFFICIENT_BUFFER, Kernel32.INSTANCE.GetLastError());
    assertTrue(pSid.getValue() > 0);
    Memory sidMemory = new Memory(pSid.getValue());
    PSID pSidMemory = new PSID(sidMemory);
    char[] referencedDomainName = new char[pDomain.getValue() + 1];
    assertTrue(Advapi32.INSTANCE.LookupAccountName(null, accountName, pSidMemory, pSid, referencedDomainName, pDomain, peUse));
    assertEquals(SID_NAME_USE.SidTypeUser, peUse.getPointer().getInt(0));
    assertTrue(Native.toString(referencedDomainName).length() > 0);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) PointerByReference(com.sun.jna.ptr.PointerByReference) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 3 with CHAR

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

the class Advapi32Test method testRegSetValueEx_DWORD.

public void testRegSetValueEx_DWORD() {
    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_DWORD value
    int value = 42145;
    byte[] data = new byte[4];
    data[0] = (byte) (value & 0xff);
    data[1] = (byte) ((value >> 8) & 0xff);
    data[2] = (byte) ((value >> 16) & 0xff);
    data[3] = (byte) ((value >> 24) & 0xff);
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegSetValueEx(phkTest.getValue(), "DWORD", 0, WinNT.REG_DWORD, data, 4));
    // re-read the REG_DWORD value
    IntByReference lpType = new IntByReference();
    IntByReference lpcbData = new IntByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(phkTest.getValue(), "DWORD", 0, lpType, (char[]) null, lpcbData));
    assertEquals(WinNT.REG_DWORD, lpType.getValue());
    assertEquals(4, lpcbData.getValue());
    IntByReference valueRead = new IntByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(phkTest.getValue(), "DWORD", 0, lpType, valueRead, lpcbData));
    assertEquals(value, valueRead.getValue());
    // 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 4 with CHAR

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

the class WevtapiTest method testEvtOpenChannelEnum.

public void testEvtOpenChannelEnum() throws Exception {
    EVT_HANDLE channelHandle = null;
    List<String> channelList = new ArrayList<String>();
    try {
        channelHandle = Wevtapi.INSTANCE.EvtOpenChannelEnum(null, 0);
        if (channelHandle == null) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        char[] buff = new char[1024];
        IntByReference buffUsed = new IntByReference();
        while (true) {
            if (!Wevtapi.INSTANCE.EvtNextChannelPath(channelHandle, buff.length, buff, buffUsed)) {
                if (Kernel32.INSTANCE.GetLastError() == WinError.ERROR_NO_MORE_ITEMS) {
                    break;
                } else if (Kernel32.INSTANCE.GetLastError() == WinError.ERROR_INSUFFICIENT_BUFFER) {
                    buff = new char[buffUsed.getValue()];
                    if (!Wevtapi.INSTANCE.EvtNextChannelPath(channelHandle, buff.length, buff, buffUsed)) {
                        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
                    }
                }
            }
            channelList.add(Native.toString(buff));
        }
        assertThat(channelList.size() > 0, is(true));
    } finally {
        if (channelHandle != null) {
            Wevtapi.INSTANCE.EvtClose(channelHandle);
        }
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) EVT_HANDLE(com.sun.jna.platform.win32.Winevt.EVT_HANDLE) ArrayList(java.util.ArrayList)

Example 5 with CHAR

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

the class PsapiTest method testGetProcessImageFileName.

@Test
public void testGetProcessImageFileName() {
    HANDLE me = null;
    Win32Exception we = null;
    try {
        me = Kernel32.INSTANCE.OpenProcess(WinNT.PROCESS_ALL_ACCESS, false, Kernel32.INSTANCE.GetCurrentProcessId());
        assertTrue("Handle to my process should not be null", me != null);
        char[] buffer = new char[256];
        Psapi.INSTANCE.GetProcessImageFileName(me, buffer, 256);
        String path = new String(buffer);
        assertTrue("Image path should contain 'java' and '.exe'", path.contains("java") && path.contains(".exe"));
    } catch (Win32Exception e) {
        we = e;
        // re-throw to invoke finally block
        throw we;
    } finally {
        try {
            Kernel32Util.closeHandle(me);
        } catch (Win32Exception e) {
            if (we == null) {
                we = e;
            } else {
                we.addSuppressed(e);
            }
        }
        if (we != null) {
            throw we;
        }
    }
}
Also used : HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) Test(org.junit.Test)

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