use of com.sun.jna.platform.win32.WinDef.DWORD in project jna by java-native-access.
the class EnumMoniker_Test method Next.
@Test
public void Next() {
// GetRunningObjectTable
PointerByReference pprot = new PointerByReference();
HRESULT hr = Ole32.INSTANCE.GetRunningObjectTable(new DWORD(0), pprot);
COMUtils.checkRC(hr);
IRunningObjectTable rot = new RunningObjectTable(pprot.getValue());
// EnumRunning
PointerByReference ppenumMoniker = new PointerByReference();
hr = rot.EnumRunning(ppenumMoniker);
COMUtils.checkRC(hr);
IEnumMoniker iterator = new EnumMoniker(ppenumMoniker.getValue());
// Reset
hr = iterator.Reset();
COMUtils.checkRC(hr);
// Next
PointerByReference rgelt1 = new PointerByReference();
ULONGByReference pceltFetched1 = new ULONGByReference();
hr = iterator.Next(new ULONG(1), rgelt1, pceltFetched1);
COMUtils.checkRC(hr);
// Next
PointerByReference rgelt2 = new PointerByReference();
ULONGByReference pceltFetched2 = new ULONGByReference();
hr = iterator.Next(new ULONG(1), rgelt2, pceltFetched2);
COMUtils.checkRC(hr);
assertNotEquals(rgelt1.getValue(), rgelt2.getValue());
}
use of com.sun.jna.platform.win32.WinDef.DWORD in project jna by java-native-access.
the class EnumMoniker_Test method Reset.
@Test
public void Reset() {
// GetRunningObjectTable
PointerByReference pprot = new PointerByReference();
HRESULT hr = Ole32.INSTANCE.GetRunningObjectTable(new DWORD(0), pprot);
COMUtils.checkRC(hr);
IRunningObjectTable rot = new RunningObjectTable(pprot.getValue());
// EnumRunning
PointerByReference ppenumMoniker = new PointerByReference();
hr = rot.EnumRunning(ppenumMoniker);
COMUtils.checkRC(hr);
IEnumMoniker iterator = new EnumMoniker(ppenumMoniker.getValue());
// Reset
hr = iterator.Reset();
COMUtils.checkRC(hr);
// Next
PointerByReference rgelt1 = new PointerByReference();
ULONGByReference pceltFetched1 = new ULONGByReference();
hr = iterator.Next(new ULONG(1), rgelt1, pceltFetched1);
COMUtils.checkRC(hr);
// Reset
hr = iterator.Reset();
COMUtils.checkRC(hr);
// Next
PointerByReference rgelt2 = new PointerByReference();
ULONGByReference pceltFetched2 = new ULONGByReference();
hr = iterator.Next(new ULONG(1), rgelt2, pceltFetched2);
COMUtils.checkRC(hr);
assertEquals(rgelt1.getValue(), rgelt2.getValue());
}
use of com.sun.jna.platform.win32.WinDef.DWORD in project jna by java-native-access.
the class Advapi32Test method testAccessCheck.
public void testAccessCheck() {
final GENERIC_MAPPING mapping = new GENERIC_MAPPING();
mapping.genericRead = new DWORD(FILE_GENERIC_READ);
mapping.genericWrite = new DWORD(FILE_GENERIC_WRITE);
mapping.genericExecute = new DWORD(FILE_GENERIC_EXECUTE);
mapping.genericAll = new DWORD(FILE_ALL_ACCESS);
final Memory securityDescriptorMemoryPointer = new Memory(1);
final PRIVILEGE_SET privileges = new PRIVILEGE_SET(1);
privileges.PrivilegeCount = new DWORD(0);
final DWORDByReference privilegeLength = new DWORDByReference(new DWORD(privileges.size()));
final DWORDByReference grantedAccess = new DWORDByReference();
final BOOLByReference result = new BOOLByReference();
final boolean status = Advapi32.INSTANCE.AccessCheck(securityDescriptorMemoryPointer, null, new DWORD(FILE_GENERIC_READ), mapping, privileges, privilegeLength, grantedAccess, result);
assertFalse(status);
assertFalse(result.getValue().booleanValue());
assertEquals(WinError.ERROR_INVALID_HANDLE, Kernel32.INSTANCE.GetLastError());
}
use of com.sun.jna.platform.win32.WinDef.DWORD in project jna by java-native-access.
the class Advapi32Test method testWriteEncryptedFileRaw.
public void testWriteEncryptedFileRaw() throws Exception {
// create an encrypted file
File file = createTempFile();
String lpFileName = file.getAbsolutePath();
assertTrue(Advapi32.INSTANCE.EncryptFile(lpFileName));
// open file for export
ULONG ulFlags = new ULONG(0);
PointerByReference pvContext = new PointerByReference();
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.OpenEncryptedFileRaw(lpFileName, ulFlags, pvContext));
// read encrypted file
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
FE_EXPORT_FUNC pfExportCallback = new FE_EXPORT_FUNC() {
@Override
public DWORD callback(Pointer pbData, Pointer pvCallbackContext, ULONG ulLength) {
if (pbData == null) {
throw new NullPointerException("Callback data unexpectedly null");
}
byte[] arr = pbData.getByteArray(0, ulLength.intValue());
try {
outputStream.write(arr);
} catch (IOException e) {
throw new RuntimeException(e);
}
return new DWORD(W32Errors.ERROR_SUCCESS);
}
};
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.ReadEncryptedFileRaw(pfExportCallback, null, pvContext.getValue()));
outputStream.close();
Advapi32.INSTANCE.CloseEncryptedFileRaw(pvContext.getValue());
// open file for import
String lbFileName2 = System.getProperty("java.io.tmpdir") + File.separator + "backup-" + file.getName();
ULONG ulFlags2 = new ULONG(CREATE_FOR_IMPORT);
PointerByReference pvContext2 = new PointerByReference();
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.OpenEncryptedFileRaw(lbFileName2, ulFlags2, pvContext2));
// write encrypted file
final IntByReference elementsReadWrapper = new IntByReference(0);
FE_IMPORT_FUNC pfImportCallback = new FE_IMPORT_FUNC() {
@Override
public DWORD callback(Pointer pbData, Pointer pvCallbackContext, ULONGByReference ulLength) {
int elementsRead = elementsReadWrapper.getValue();
int remainingElements = outputStream.size() - elementsRead;
int length = Math.min(remainingElements, ulLength.getValue().intValue());
pbData.write(0, outputStream.toByteArray(), elementsRead, length);
elementsReadWrapper.setValue(elementsRead + length);
ulLength.setValue(new ULONG(length));
return new DWORD(W32Errors.ERROR_SUCCESS);
}
};
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.WriteEncryptedFileRaw(pfImportCallback, null, pvContext2.getValue()));
Advapi32.INSTANCE.CloseEncryptedFileRaw(pvContext2.getValue());
file.delete();
new File(lbFileName2.toString()).delete();
}
use of com.sun.jna.platform.win32.WinDef.DWORD 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