Search in sources :

Example 16 with Advapi32

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

the class Advapi32Test method testBackupEventLog.

/*
    public void testClearEventLog() {
    	HANDLE h = Advapi32.INSTANCE.OpenEventLog(null, "Application");
    	assertFalse(h.equals(WinBase.INVALID_HANDLE_VALUE));
    	IntByReference before = new IntByReference();
    	assertTrue(Advapi32.INSTANCE.GetNumberOfEventLogRecords(h, before));
    	assertTrue(before.getValue() >= 0);
    	assertTrue(Advapi32.INSTANCE.ClearEventLog(h, null));
    	IntByReference after = new IntByReference();
    	assertTrue(Advapi32.INSTANCE.GetNumberOfEventLogRecords(h, after));
    	assertTrue(after.getValue() < before.getValue() || before.getValue() == 0);
    	assertTrue(Advapi32.INSTANCE.CloseEventLog(h));
    }
    */
public void testBackupEventLog() {
    HANDLE h = Advapi32.INSTANCE.OpenEventLog(null, "Application");
    assertNotNull(h);
    String backupFileName = Kernel32Util.getTempPath() + "\\JNADevEventLog.bak";
    File f = new File(backupFileName);
    if (f.exists()) {
        f.delete();
    }
    assertTrue(Advapi32.INSTANCE.BackupEventLog(h, backupFileName));
    HANDLE hBackup = Advapi32.INSTANCE.OpenBackupEventLog(null, backupFileName);
    assertNotNull(hBackup);
    IntByReference n = new IntByReference();
    assertTrue(Advapi32.INSTANCE.GetNumberOfEventLogRecords(hBackup, n));
    assertTrue(n.getValue() >= 0);
    assertTrue(Advapi32.INSTANCE.CloseEventLog(h));
    assertTrue(Advapi32.INSTANCE.CloseEventLog(hBackup));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) File(java.io.File)

Example 17 with Advapi32

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

the class Advapi32Test method testMapGenericAllMask.

public void testMapGenericAllMask() {
    final GENERIC_MAPPING mapping = new GENERIC_MAPPING();
    mapping.genericRead = new DWORD(FILE_GENERIC_READ);
    mapping.genericWrite = new DWORD(FILE_GENERIC_WRITE);
    mapping.genericExecute = new DWORD(FILE_GENERIC_EXECUTE);
    mapping.genericAll = new DWORD(FILE_ALL_ACCESS);
    final DWORDByReference rights = new DWORDByReference(new DWORD(GENERIC_ALL));
    Advapi32.INSTANCE.MapGenericMask(rights, mapping);
    assertEquals(FILE_ALL_ACCESS, rights.getValue().intValue());
    assertTrue(GENERIC_ALL != (rights.getValue().intValue() & GENERIC_ALL));
}
Also used : GENERIC_MAPPING(com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD)

Example 18 with Advapi32

use of com.sun.jna.platform.win32.Advapi32 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 19 with Advapi32

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

the class Advapi32Test method testCloseServiceHandle.

public void testCloseServiceHandle() throws Exception {
    SC_HANDLE handle = Advapi32.INSTANCE.OpenSCManager(null, null, Winsvc.SC_MANAGER_CONNECT);
    assertNotNull(handle);
    assertTrue(Advapi32.INSTANCE.CloseServiceHandle(handle));
    assertFalse(Advapi32.INSTANCE.CloseServiceHandle(null));
    assertEquals(W32Errors.ERROR_INVALID_HANDLE, Kernel32.INSTANCE.GetLastError());
}
Also used : SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE)

Example 20 with Advapi32

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

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