Search in sources :

Example 1 with HANDLE

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

the class Advapi32Test method testGetTokenGroupsInformation.

public void testGetTokenGroupsInformation() {
    HANDLEByReference phToken = new HANDLEByReference();
    try {
        HANDLE processHandle = Kernel32.INSTANCE.GetCurrentProcess();
        assertTrue(Advapi32.INSTANCE.OpenProcessToken(processHandle, WinNT.TOKEN_DUPLICATE | WinNT.TOKEN_QUERY, phToken));
        IntByReference tokenInformationLength = new IntByReference();
        assertFalse(Advapi32.INSTANCE.GetTokenInformation(phToken.getValue(), WinNT.TOKEN_INFORMATION_CLASS.TokenGroups, null, 0, tokenInformationLength));
        assertEquals(W32Errors.ERROR_INSUFFICIENT_BUFFER, Kernel32.INSTANCE.GetLastError());
        WinNT.TOKEN_GROUPS groups = new WinNT.TOKEN_GROUPS(tokenInformationLength.getValue());
        assertTrue(Advapi32.INSTANCE.GetTokenInformation(phToken.getValue(), WinNT.TOKEN_INFORMATION_CLASS.TokenGroups, groups, tokenInformationLength.getValue(), tokenInformationLength));
        assertTrue(tokenInformationLength.getValue() > 0);
        assertTrue(groups.GroupCount > 0);
        for (SID_AND_ATTRIBUTES sidAndAttribute : groups.getGroups()) {
            assertTrue(Advapi32.INSTANCE.IsValidSid(sidAndAttribute.Sid));
        // System.out.println(Advapi32Util.convertSidToStringSid(sidAndAttribute.Sid));
        }
    } finally {
        Kernel32Util.closeHandleRef(phToken);
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) SID_AND_ATTRIBUTES(com.sun.jna.platform.win32.WinNT.SID_AND_ATTRIBUTES) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 2 with HANDLE

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

the class Advapi32Test method testReadEventLogEntries.

public void testReadEventLogEntries() {
    HANDLE h = Advapi32.INSTANCE.OpenEventLog(null, "Application");
    IntByReference pnBytesRead = new IntByReference();
    IntByReference pnMinNumberOfBytesNeeded = new IntByReference();
    Memory buffer = new Memory(1024 * 64);
    // shorten test, avoid iterating through all events
    int maxReads = 3;
    int rc = 0;
    while (true) {
        if (maxReads-- <= 0)
            break;
        if (!Advapi32.INSTANCE.ReadEventLog(h, WinNT.EVENTLOG_SEQUENTIAL_READ | WinNT.EVENTLOG_FORWARDS_READ, 0, buffer, (int) buffer.size(), pnBytesRead, pnMinNumberOfBytesNeeded)) {
            rc = Kernel32.INSTANCE.GetLastError();
            if (rc == W32Errors.ERROR_INSUFFICIENT_BUFFER) {
                buffer = new Memory(pnMinNumberOfBytesNeeded.getValue());
                rc = 0;
                continue;
            }
            break;
        }
        int dwRead = pnBytesRead.getValue();
        Pointer pevlr = buffer;
        int maxRecords = 3;
        while (dwRead > 0 && maxRecords-- > 0) {
            EVENTLOGRECORD record = new EVENTLOGRECORD(pevlr);
            /*
                  System.out.println(record.RecordNumber.intValue()
                  + " Event ID: " + record.EventID.intValue()
                  + " Event Type: " + record.EventType.intValue()
                  + " Event Source: " + pevlr.getString(record.size(), true));
                */
            dwRead -= record.Length.intValue();
            pevlr = pevlr.share(record.Length.intValue());
        }
    }
    assertTrue("Unexpected error after reading event log: " + new Win32Exception(rc), rc == W32Errors.ERROR_HANDLE_EOF || rc == 0);
    assertTrue("Error closing event log", Advapi32.INSTANCE.CloseEventLog(h));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) Pointer(com.sun.jna.Pointer) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) EVENTLOGRECORD(com.sun.jna.platform.win32.WinNT.EVENTLOGRECORD)

Example 3 with HANDLE

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

the class Advapi32Test method testOpenProcessToken.

public void testOpenProcessToken() {
    HANDLEByReference phToken = new HANDLEByReference();
    try {
        HANDLE processHandle = Kernel32.INSTANCE.GetCurrentProcess();
        assertTrue(Advapi32.INSTANCE.OpenProcessToken(processHandle, WinNT.TOKEN_DUPLICATE | WinNT.TOKEN_QUERY, phToken));
    } finally {
        Kernel32Util.closeHandleRef(phToken);
    }
}
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 4 with HANDLE

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

the class Advapi32Test method testGetOldestEventLogRecord.

public void testGetOldestEventLogRecord() {
    HANDLE h = Advapi32.INSTANCE.OpenEventLog(null, "Application");
    IntByReference oldestRecord = new IntByReference();
    assertTrue(Advapi32.INSTANCE.GetOldestEventLogRecord(h, oldestRecord));
    assertTrue(oldestRecord.getValue() >= 0);
    assertTrue(Advapi32.INSTANCE.CloseEventLog(h));
}
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)

Example 5 with HANDLE

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

the class Advapi32Test method testOpenSCManager.

public void testOpenSCManager() {
    SC_HANDLE handle = Advapi32.INSTANCE.OpenSCManager(null, null, Winsvc.SC_MANAGER_CONNECT);
    assertNotNull(handle);
    assertTrue(Advapi32.INSTANCE.CloseServiceHandle(handle));
    assertNull(Advapi32.INSTANCE.OpenSCManager("invalidMachineName", null, Winsvc.SC_MANAGER_CONNECT));
    int err = Kernel32.INSTANCE.GetLastError();
    assertTrue("Unexpected error in OpenSCManager: " + err, err == W32Errors.RPC_S_SERVER_UNAVAILABLE || err == W32Errors.RPC_S_INVALID_NET_ADDR);
    assertNull(Advapi32.INSTANCE.OpenSCManager(null, "invalidDatabase", Winsvc.SC_MANAGER_CONNECT));
    assertEquals(W32Errors.ERROR_INVALID_NAME, Kernel32.INSTANCE.GetLastError());
}
Also used : SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE)

Aggregations

HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)113 IntByReference (com.sun.jna.ptr.IntByReference)48 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)33 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)29 File (java.io.File)26 Memory (com.sun.jna.Memory)21 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)19 Test (org.junit.Test)19 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)18 Pointer (com.sun.jna.Pointer)13 Advapi32 (com.sun.jna.platform.win32.Advapi32)13 HKEY (com.sun.jna.platform.win32.WinReg.HKEY)13 PointerByReference (com.sun.jna.ptr.PointerByReference)11 HWND (com.sun.jna.platform.win32.WinDef.HWND)8 CredHandle (com.sun.jna.platform.win32.Sspi.CredHandle)5 CtxtHandle (com.sun.jna.platform.win32.Sspi.CtxtHandle)5 SecBufferDesc (com.sun.jna.platform.win32.Sspi.SecBufferDesc)5 TimeStamp (com.sun.jna.platform.win32.Sspi.TimeStamp)5 BufferedImage (java.awt.image.BufferedImage)5 ArrayList (java.util.ArrayList)5