Search in sources :

Example 1 with TOKEN_PRIVILEGES

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

the class Advapi32Test method testAdjustTokenPrivileges.

public void testAdjustTokenPrivileges() {
    HANDLEByReference hToken = new HANDLEByReference();
    assertTrue(Advapi32.INSTANCE.OpenProcessToken(Kernel32.INSTANCE.GetCurrentProcess(), WinNT.TOKEN_ADJUST_PRIVILEGES | WinNT.TOKEN_QUERY, hToken));
    try {
        // Find an already enabled privilege
        TOKEN_PRIVILEGES tp = new TOKEN_PRIVILEGES(1024);
        IntByReference returnLength = new IntByReference();
        assertTrue(Advapi32.INSTANCE.GetTokenInformation(hToken.getValue(), WinNT.TOKEN_INFORMATION_CLASS.TokenPrivileges, tp, tp.size(), returnLength));
        assertTrue(tp.PrivilegeCount.intValue() > 0);
        WinNT.LUID luid = null;
        for (int i = 0; i < tp.PrivilegeCount.intValue(); i++) {
            if ((tp.Privileges[i].Attributes.intValue() & WinNT.SE_PRIVILEGE_ENABLED) > 0) {
                luid = tp.Privileges[i].Luid;
            }
        }
        assertTrue(luid != null);
        // Re-enable it. That should succeed.
        tp = new WinNT.TOKEN_PRIVILEGES(1);
        tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(luid, new DWORD(WinNT.SE_PRIVILEGE_ENABLED));
        assertTrue(Advapi32.INSTANCE.AdjustTokenPrivileges(hToken.getValue(), false, tp, 0, null, null));
    } finally {
        Kernel32Util.closeHandleRef(hToken);
    }
}
Also used : TOKEN_PRIVILEGES(com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES) IntByReference(com.sun.jna.ptr.IntByReference) TOKEN_PRIVILEGES(com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference)

Example 2 with TOKEN_PRIVILEGES

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

the class W32Service method addShutdownPrivilegeToProcess.

private void addShutdownPrivilegeToProcess() {
    HANDLEByReference hToken = new HANDLEByReference();
    LUID luid = new LUID();
    Advapi32.INSTANCE.OpenProcessToken(Kernel32.INSTANCE.GetCurrentProcess(), WinNT.TOKEN_ADJUST_PRIVILEGES, hToken);
    Advapi32.INSTANCE.LookupPrivilegeValue("", WinNT.SE_SHUTDOWN_NAME, luid);
    TOKEN_PRIVILEGES tp = new TOKEN_PRIVILEGES(1);
    tp.Privileges[0] = new LUID_AND_ATTRIBUTES(luid, new DWORD(WinNT.SE_PRIVILEGE_ENABLED));
    Advapi32.INSTANCE.AdjustTokenPrivileges(hToken.getValue(), false, tp, tp.size(), null, new IntByReference());
}
Also used : TOKEN_PRIVILEGES(com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES) IntByReference(com.sun.jna.ptr.IntByReference) LUID(com.sun.jna.platform.win32.WinNT.LUID) LUID_AND_ATTRIBUTES(com.sun.jna.platform.win32.WinNT.LUID_AND_ATTRIBUTES) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference)

Aggregations

DWORD (com.sun.jna.platform.win32.WinDef.DWORD)2 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)2 TOKEN_PRIVILEGES (com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES)2 IntByReference (com.sun.jna.ptr.IntByReference)2 LUID (com.sun.jna.platform.win32.WinNT.LUID)1 LUID_AND_ATTRIBUTES (com.sun.jna.platform.win32.WinNT.LUID_AND_ATTRIBUTES)1