Search in sources :

Example 21 with Advapi32

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

the class Advapi32Test method testDuplicateTokenEx.

public void testDuplicateTokenEx() {
    HANDLEByReference hExistingToken = new HANDLEByReference();
    HANDLEByReference phNewToken = new HANDLEByReference();
    try {
        HANDLE processHandle = Kernel32.INSTANCE.GetCurrentProcess();
        assertTrue(Advapi32.INSTANCE.OpenProcessToken(processHandle, WinNT.TOKEN_DUPLICATE | WinNT.TOKEN_QUERY, hExistingToken));
        assertTrue(Advapi32.INSTANCE.DuplicateTokenEx(hExistingToken.getValue(), WinNT.GENERIC_READ, null, SECURITY_IMPERSONATION_LEVEL.SecurityAnonymous, TOKEN_TYPE.TokenPrimary, phNewToken));
    } finally {
        Kernel32Util.closeHandleRefs(phNewToken, hExistingToken);
    }
}
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 22 with Advapi32

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

the class Advapi32Test method testReportEvent.

public void testReportEvent() {
    String applicationEventLog = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application";
    String jnaEventSource = "JNADevEventSource";
    String jnaEventSourceRegistryPath = applicationEventLog + "\\" + jnaEventSource;
    // ignore test if not able to create key (need to be administrator to do this).
    try {
        final boolean keyCreated = Advapi32Util.registryCreateKey(WinReg.HKEY_LOCAL_MACHINE, jnaEventSourceRegistryPath);
        if (!keyCreated) {
            return;
        }
    } catch (Win32Exception e) {
        return;
    }
    HANDLE h = Advapi32.INSTANCE.RegisterEventSource(null, jnaEventSource);
    IntByReference before = new IntByReference();
    assertTrue(Advapi32.INSTANCE.GetNumberOfEventLogRecords(h, before));
    assertNotNull(h);
    String[] s = { "JNA", "Event" };
    Memory m = new Memory(4);
    m.setByte(0, (byte) 1);
    m.setByte(1, (byte) 2);
    m.setByte(2, (byte) 3);
    m.setByte(3, (byte) 4);
    assertTrue(Advapi32.INSTANCE.ReportEvent(h, WinNT.EVENTLOG_ERROR_TYPE, 0, 0, null, 2, 4, s, m));
    IntByReference after = new IntByReference();
    assertTrue(Advapi32.INSTANCE.GetNumberOfEventLogRecords(h, after));
    assertTrue(before.getValue() < after.getValue());
    assertFalse(h.equals(WinBase.INVALID_HANDLE_VALUE));
    assertTrue(Advapi32.INSTANCE.DeregisterEventSource(h));
    Advapi32Util.registryDeleteKey(WinReg.HKEY_LOCAL_MACHINE, jnaEventSourceRegistryPath);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 23 with Advapi32

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

the class Advapi32Test method testSetGetSecurityDescriptorOwner.

public void testSetGetSecurityDescriptorOwner() {
    SECURITY_DESCRIPTOR sd = new SECURITY_DESCRIPTOR(64 * 1024);
    assertTrue(Advapi32.INSTANCE.InitializeSecurityDescriptor(sd, WinNT.SECURITY_DESCRIPTOR_REVISION));
    PSID pSidPut = new PSID(WinNT.SECURITY_MAX_SID_SIZE);
    IntByReference cbSid = new IntByReference(WinNT.SECURITY_MAX_SID_SIZE);
    assertTrue("Failed to create well-known SID", Advapi32.INSTANCE.CreateWellKnownSid(WELL_KNOWN_SID_TYPE.WinBuiltinAdministratorsSid, null, pSidPut, cbSid));
    assertTrue(Advapi32.INSTANCE.SetSecurityDescriptorOwner(sd, pSidPut, true));
    BOOLByReference lpbOwnerDefaulted = new BOOLByReference();
    PSIDByReference prSd = new PSIDByReference();
    assertTrue(Advapi32.INSTANCE.GetSecurityDescriptorOwner(sd, prSd, lpbOwnerDefaulted));
    PSID pSidGet = prSd.getValue();
    assertTrue(Advapi32.INSTANCE.EqualSid(pSidPut, pSidGet));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) BOOLByReference(com.sun.jna.platform.win32.WinDef.BOOLByReference) PSIDByReference(com.sun.jna.platform.win32.WinNT.PSIDByReference) SECURITY_DESCRIPTOR(com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Example 24 with Advapi32

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

the class Advapi32Test method testOpenEventLog.

public void testOpenEventLog() {
    HANDLE h = Advapi32.INSTANCE.OpenEventLog(null, "Application");
    assertNotNull(h);
    assertFalse(h.equals(WinBase.INVALID_HANDLE_VALUE));
    assertTrue(Advapi32.INSTANCE.CloseEventLog(h));
}
Also used : SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 25 with Advapi32

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

the class Advapi32Test method testControlService.

public void testControlService() {
    SC_HANDLE scmHandle = Advapi32.INSTANCE.OpenSCManager(null, null, Winsvc.SC_MANAGER_CONNECT);
    assertNotNull(scmHandle);
    SC_HANDLE serviceHandle = Advapi32.INSTANCE.OpenService(scmHandle, "eventlog", Winsvc.SERVICE_QUERY_CONFIG);
    assertNotNull(serviceHandle);
    Winsvc.SERVICE_STATUS serverStatus = new Winsvc.SERVICE_STATUS();
    assertNotNull(serviceHandle);
    assertFalse(Advapi32.INSTANCE.ControlService(serviceHandle, Winsvc.SERVICE_CONTROL_STOP, serverStatus));
    assertEquals(W32Errors.ERROR_ACCESS_DENIED, Kernel32.INSTANCE.GetLastError());
    assertTrue(Advapi32.INSTANCE.CloseServiceHandle(serviceHandle));
    assertTrue(Advapi32.INSTANCE.CloseServiceHandle(scmHandle));
}
Also used : SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE)

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