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);
}
}
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());
}
Aggregations