use of com.sun.jna.platform.win32.WinNT.HANDLEByReference in project jna by java-native-access.
the class WinspoolUtil method getPrinterInfo2.
public static PRINTER_INFO_2 getPrinterInfo2(String printerName) {
IntByReference pcbNeeded = new IntByReference();
IntByReference pcReturned = new IntByReference();
HANDLEByReference pHandle = new HANDLEByReference();
if (!Winspool.INSTANCE.OpenPrinter(printerName, pHandle, null))
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
Win32Exception we = null;
PRINTER_INFO_2 pinfo2 = null;
try {
Winspool.INSTANCE.GetPrinter(pHandle.getValue(), 2, null, 0, pcbNeeded);
if (pcbNeeded.getValue() <= 0)
return new PRINTER_INFO_2();
pinfo2 = new PRINTER_INFO_2(pcbNeeded.getValue());
if (!Winspool.INSTANCE.GetPrinter(pHandle.getValue(), 2, pinfo2.getPointer(), pcbNeeded.getValue(), pcReturned))
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
pinfo2.read();
} catch (Win32Exception e) {
we = e;
} finally {
if (!Winspool.INSTANCE.ClosePrinter(pHandle.getValue())) {
Win32Exception ex = new Win32Exception(Kernel32.INSTANCE.GetLastError());
if (we != null) {
ex.addSuppressed(we);
}
}
}
if (we != null) {
throw we;
}
return pinfo2;
}
use of com.sun.jna.platform.win32.WinNT.HANDLEByReference in project jna by java-native-access.
the class WinspoolUtil method getJobInfo1.
public static JOB_INFO_1[] getJobInfo1(HANDLEByReference phPrinter) {
IntByReference pcbNeeded = new IntByReference();
IntByReference pcReturned = new IntByReference();
Winspool.INSTANCE.EnumJobs(phPrinter.getValue(), 0, 255, 1, null, 0, pcbNeeded, pcReturned);
if (pcbNeeded.getValue() <= 0) {
return new JOB_INFO_1[0];
}
JOB_INFO_1 pJobEnum = new JOB_INFO_1(pcbNeeded.getValue());
if (!Winspool.INSTANCE.EnumJobs(phPrinter.getValue(), 0, 255, 1, pJobEnum.getPointer(), pcbNeeded.getValue(), pcbNeeded, pcReturned)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}
pJobEnum.read();
return (JOB_INFO_1[]) pJobEnum.toArray(pcReturned.getValue());
}
use of com.sun.jna.platform.win32.WinNT.HANDLEByReference in project jna by java-native-access.
the class Advapi32Test method testSetThreadTokenCurrentThread.
public void testSetThreadTokenCurrentThread() {
HANDLEByReference phToken = new HANDLEByReference();
HANDLEByReference phTokenDup = new HANDLEByReference();
try {
HANDLE threadHandle = Kernel32.INSTANCE.GetCurrentThread();
// See if thread has a token. If not, must duplicate process token and set thread token using that.
if (!Advapi32.INSTANCE.OpenThreadToken(threadHandle, WinNT.TOKEN_IMPERSONATE | WinNT.TOKEN_QUERY, false, phToken)) {
assertEquals(W32Errors.ERROR_NO_TOKEN, Kernel32.INSTANCE.GetLastError());
HANDLE processHandle = Kernel32.INSTANCE.GetCurrentProcess();
assertTrue(Advapi32.INSTANCE.OpenProcessToken(processHandle, WinNT.TOKEN_DUPLICATE, phToken));
assertTrue(Advapi32.INSTANCE.DuplicateTokenEx(phToken.getValue(), WinNT.TOKEN_IMPERSONATE, null, WinNT.SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, WinNT.TOKEN_TYPE.TokenImpersonation, phTokenDup));
// Null sets on current thread
assertTrue(Advapi32.INSTANCE.SetThreadToken(null, phTokenDup.getValue()));
} else {
//Null sets on current thread
assertTrue(Advapi32.INSTANCE.SetThreadToken(null, phToken.getValue()));
}
// Revert and cleanup
assertTrue(Advapi32.INSTANCE.SetThreadToken(null, null));
} finally {
Kernel32Util.closeHandleRefs(phToken, phTokenDup);
}
}
use of com.sun.jna.platform.win32.WinNT.HANDLEByReference in project jna by java-native-access.
the class Advapi32Test method testOpenThreadTokenNoToken.
public void testOpenThreadTokenNoToken() {
HANDLEByReference phToken = new HANDLEByReference();
HANDLE threadHandle = Kernel32.INSTANCE.GetCurrentThread();
assertNotNull(threadHandle);
assertFalse(Advapi32.INSTANCE.OpenThreadToken(threadHandle, WinNT.TOKEN_READ, false, phToken));
assertEquals(W32Errors.ERROR_NO_TOKEN, Kernel32.INSTANCE.GetLastError());
}
use of com.sun.jna.platform.win32.WinNT.HANDLEByReference in project jna by java-native-access.
the class Advapi32Test method testSetNamedSecurityInfoForFileWithSACL.
public void testSetNamedSecurityInfoForFileWithSACL() throws Exception {
boolean impersontating = false;
HANDLEByReference phToken = new HANDLEByReference();
HANDLEByReference phTokenDuplicate = new HANDLEByReference();
try {
// open thread or process token, elevate
if (!Advapi32.INSTANCE.OpenThreadToken(Kernel32.INSTANCE.GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES, false, phToken)) {
assertEquals(W32Errors.ERROR_NO_TOKEN, Kernel32.INSTANCE.GetLastError());
// OpenThreadToken may fail with W32Errors.ERROR_NO_TOKEN if current thread is anonymous. When this happens,
// we need to open the process token to duplicate it, then set our thread token.
assertTrue(Advapi32.INSTANCE.OpenProcessToken(Kernel32.INSTANCE.GetCurrentProcess(), TOKEN_DUPLICATE, phToken));
// Process token opened, now duplicate
assertTrue(Advapi32.INSTANCE.DuplicateTokenEx(phToken.getValue(), TOKEN_ADJUST_PRIVILEGES | TOKEN_IMPERSONATE, null, SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, TOKEN_TYPE.TokenImpersonation, phTokenDuplicate));
// And set thread token.
assertTrue(Advapi32.INSTANCE.SetThreadToken(null, phTokenDuplicate.getValue()));
impersontating = true;
}
// Which token to adjust depends on whether we had to impersonate or not.
HANDLE tokenAdjust = impersontating ? phTokenDuplicate.getValue() : phToken.getValue();
WinNT.TOKEN_PRIVILEGES tp = new WinNT.TOKEN_PRIVILEGES(1);
WinNT.LUID pLuid = new WinNT.LUID();
assertTrue(Advapi32.INSTANCE.LookupPrivilegeValue(null, SE_SECURITY_NAME, pLuid));
tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(pLuid, new DWORD(WinNT.SE_PRIVILEGE_ENABLED));
assertTrue(Advapi32.INSTANCE.AdjustTokenPrivileges(tokenAdjust, false, tp, 0, null, null));
assertTrue(Advapi32.INSTANCE.LookupPrivilegeValue(null, SE_RESTORE_NAME, pLuid));
tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(pLuid, new DWORD(WinNT.SE_PRIVILEGE_ENABLED));
assertTrue(Advapi32.INSTANCE.AdjustTokenPrivileges(tokenAdjust, false, tp, 0, null, null));
// create a temp file
File file = createTempFile();
int infoType = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION;
PointerByReference ppsidOwner = new PointerByReference();
PointerByReference ppsidGroup = new PointerByReference();
PointerByReference ppDacl = new PointerByReference();
PointerByReference ppSacl = new PointerByReference();
PointerByReference ppSecurityDescriptor = new PointerByReference();
String filePath = file.getAbsolutePath();
try {
try {
assertEquals("GetNamedSecurityInfo(" + filePath + ")", 0, Advapi32.INSTANCE.GetNamedSecurityInfo(filePath, AccCtrl.SE_OBJECT_TYPE.SE_FILE_OBJECT, infoType, ppsidOwner, ppsidGroup, ppDacl, ppSacl, ppSecurityDescriptor));
// Send the DACL as a SACL
assertEquals("SetNamedSecurityInfo(" + filePath + ")", 0, Advapi32.INSTANCE.SetNamedSecurityInfo(filePath, AccCtrl.SE_OBJECT_TYPE.SE_FILE_OBJECT, infoType, ppsidOwner.getValue(), ppsidGroup.getValue(), ppDacl.getValue(), ppDacl.getValue()));
} finally {
file.delete();
}
} finally {
Kernel32Util.freeLocalMemory(ppSecurityDescriptor.getValue());
}
if (impersontating) {
assertTrue("SetThreadToken", Advapi32.INSTANCE.SetThreadToken(null, null));
} else {
tp.Privileges[0] = new WinNT.LUID_AND_ATTRIBUTES(pLuid, new DWORD(0));
assertTrue("AdjustTokenPrivileges", Advapi32.INSTANCE.AdjustTokenPrivileges(tokenAdjust, false, tp, 0, null, null));
}
} finally {
Kernel32Util.closeHandleRefs(phToken, phTokenDuplicate);
}
}
Aggregations