use of com.sun.jna.platform.win32.WinDef.BOOL in project jna by java-native-access.
the class Advapi32Test method testSetGetSecurityDescriptorDacl.
public void testSetGetSecurityDescriptorDacl() throws IOException {
SECURITY_DESCRIPTOR sd = new SECURITY_DESCRIPTOR(64 * 1024);
assertTrue(Advapi32.INSTANCE.InitializeSecurityDescriptor(sd, WinNT.SECURITY_DESCRIPTOR_REVISION));
ACL pAcl;
int cbAcl = 0;
PSID pSid = 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, pSid, cbSid));
int sidLength = Advapi32.INSTANCE.GetLengthSid(pSid);
cbAcl = Native.getNativeSize(ACL.class, null);
cbAcl += Native.getNativeSize(ACCESS_ALLOWED_ACE.class, null);
cbAcl += (sidLength - DWORD.SIZE);
cbAcl = Advapi32Util.alignOnDWORD(cbAcl);
pAcl = new ACL(cbAcl);
assertTrue(Advapi32.INSTANCE.InitializeAcl(pAcl, cbAcl, WinNT.ACL_REVISION));
assertTrue(Advapi32.INSTANCE.AddAccessAllowedAce(pAcl, WinNT.ACL_REVISION, WinNT.STANDARD_RIGHTS_ALL, pSid));
assertTrue(Advapi32.INSTANCE.SetSecurityDescriptorDacl(sd, true, pAcl, false));
BOOLByReference lpbDaclPresent = new BOOLByReference();
BOOLByReference lpbDaclDefaulted = new BOOLByReference();
PACLByReference pDacl = new PACLByReference();
assertTrue(Advapi32.INSTANCE.GetSecurityDescriptorDacl(sd, lpbDaclPresent, pDacl, lpbDaclDefaulted));
ACL pAclGet = pDacl.getValue();
assertEquals(new BOOL(true), lpbDaclPresent.getValue());
assertEquals(new BOOL(false), lpbDaclDefaulted.getValue());
assertEquals(1, pAclGet.AceCount);
assertEquals(WinNT.ACL_REVISION, pAclGet.AclRevision);
}
use of com.sun.jna.platform.win32.WinDef.BOOL in project jna by java-native-access.
the class WevtapiTest method testModifyChannelConfig.
public void testModifyChannelConfig() throws Exception {
EVT_HANDLE channelHandle = null;
try {
channelHandle = Wevtapi.INSTANCE.EvtOpenChannelConfig(null, "Application", 0);
assertNotNull(channelHandle);
Winevt.EVT_VARIANT evtVariant = WevtapiUtil.EvtGetChannelConfigProperty(channelHandle, EVT_CHANNEL_CONFIG_PROPERTY_ID.EvtChannelConfigClassicEventlog);
assertThat(((WinDef.BOOL) evtVariant.getValue()).booleanValue(), is(true));
Winevt.EVT_VARIANT setter = new Winevt.EVT_VARIANT();
setter.setValue(Winevt.EVT_VARIANT_TYPE.EvtVarTypeBoolean, new BOOL(false));
Wevtapi.INSTANCE.EvtSetChannelConfigProperty(channelHandle, EVT_CHANNEL_CONFIG_PROPERTY_ID.EvtChannelConfigClassicEventlog, 0, setter);
evtVariant = WevtapiUtil.EvtGetChannelConfigProperty(channelHandle, EVT_CHANNEL_CONFIG_PROPERTY_ID.EvtChannelConfigClassicEventlog);
assertThat(((WinDef.BOOL) evtVariant.getValue()).booleanValue(), is(false));
setter.setValue(Winevt.EVT_VARIANT_TYPE.EvtVarTypeBoolean, new BOOL(true));
Wevtapi.INSTANCE.EvtSetChannelConfigProperty(channelHandle, EVT_CHANNEL_CONFIG_PROPERTY_ID.EvtChannelConfigClassicEventlog, 0, setter);
evtVariant = WevtapiUtil.EvtGetChannelConfigProperty(channelHandle, EVT_CHANNEL_CONFIG_PROPERTY_ID.EvtChannelConfigClassicEventlog);
assertThat(((WinDef.BOOL) evtVariant.getValue()).booleanValue(), is(true));
// Writing back is skipped neighter is EvtChannelConfigClassicEventlog
// writable, nor is it a good idea to mess with the log of the developer machine
} finally {
if (channelHandle != null) {
Wevtapi.INSTANCE.EvtClose(channelHandle);
}
}
}
use of com.sun.jna.platform.win32.WinDef.BOOL in project jna by java-native-access.
the class IFileSystem3 method testConvertBoolean.
@Test
public void testConvertBoolean() {
VARIANT_BOOL testVariantBOOL = new VARIANT_BOOL(true);
VARIANT resultVariantBOOL = Convert.toVariant(testVariantBOOL);
assertEquals(true, resultVariantBOOL.booleanValue());
assertEquals(true, Convert.toJavaObject(resultVariantBOOL, Object.class, fact, false, false));
assertEquals(true, Convert.toJavaObject(resultVariantBOOL, Boolean.class, fact, false, false));
assertEquals(true, Convert.toJavaObject(resultVariantBOOL, boolean.class, fact, false, false));
BOOL testBOOL = new BOOL(true);
VARIANT resultBOOL = Convert.toVariant(testBOOL);
assertEquals(true, resultBOOL.booleanValue());
assertEquals(true, Convert.toJavaObject(resultBOOL, Object.class, fact, false, false));
assertEquals(true, Convert.toJavaObject(resultBOOL, Boolean.class, fact, false, false));
assertEquals(true, Convert.toJavaObject(resultBOOL, boolean.class, fact, false, false));
Boolean testBooleanObj = true;
VARIANT resultBooleanObj = Convert.toVariant(testBooleanObj);
boolean testBoolean = true;
VARIANT resultBoolean = Convert.toVariant(testBoolean);
assertEquals(true, resultBooleanObj.booleanValue());
assertEquals(true, resultBoolean.booleanValue());
assertEquals(true, Convert.toJavaObject(resultBooleanObj, Object.class, fact, false, false));
assertEquals(true, Convert.toJavaObject(resultBoolean, Object.class, fact, false, false));
assertEquals(true, Convert.toJavaObject(resultBooleanObj, boolean.class, fact, false, false));
assertEquals(true, Convert.toJavaObject(resultBoolean, boolean.class, fact, false, false));
assertEquals(true, Convert.toJavaObject(resultBooleanObj, Boolean.class, fact, false, false));
assertEquals(true, Convert.toJavaObject(resultBoolean, Boolean.class, fact, false, false));
}
use of com.sun.jna.platform.win32.WinDef.BOOL in project jna by java-native-access.
the class Convert method toVariant.
/**
* Convert a java value into a VARIANT suitable for passing in a COM
* invocation.
*
* <p><i>Implementation notes</i></p>
*
* <ul>
* <li>VARIANTs are not rewrapped, but passed through unmodified</li>
* <li>A string is wrapped into a BSTR, that is wrapped into the VARIANT.
* The string is allocated as native memory by the VARIANT constructor.
* The BSTR needs to be freed by {@link com.sun.jna.platform.win32.OleAuto#SysFreeString}.</li>
* </ul>
*
* @param value to be wrapped
* @return wrapped VARIANT
*/
public static VARIANT toVariant(Object value) {
if (value instanceof VARIANT) {
return (VARIANT) value;
} else if (value instanceof BSTR) {
return new VARIANT((BSTR) value);
} else if (value instanceof VARIANT_BOOL) {
return new VARIANT((VARIANT_BOOL) value);
} else if (value instanceof BOOL) {
return new VARIANT((BOOL) value);
} else if (value instanceof LONG) {
return new VARIANT((LONG) value);
} else if (value instanceof SHORT) {
return new VARIANT((SHORT) value);
} else if (value instanceof DATE) {
return new VARIANT((DATE) value);
} else if (value instanceof BYTE) {
return new VARIANT((BYTE) value);
} else if (value instanceof Byte) {
return new VARIANT((Byte) value);
} else if (value instanceof Character) {
return new VARIANT((Character) value);
} else if (value instanceof CHAR) {
return new VARIANT((CHAR) value);
} else if (value instanceof Short) {
return new VARIANT((Short) value);
} else if (value instanceof Integer) {
return new VARIANT((Integer) value);
} else if (value instanceof Long) {
return new VARIANT((Long) value);
} else if (value instanceof Float) {
return new VARIANT((Float) value);
} else if (value instanceof Double) {
return new VARIANT((Double) value);
} else if (value instanceof String) {
return new VARIANT((String) value);
} else if (value instanceof Boolean) {
return new VARIANT((Boolean) value);
} else if (value instanceof com.sun.jna.platform.win32.COM.IDispatch) {
return new VARIANT((com.sun.jna.platform.win32.COM.IDispatch) value);
} else if (value instanceof Date) {
return new VARIANT((Date) value);
} else if (value instanceof Proxy) {
InvocationHandler ih = Proxy.getInvocationHandler(value);
ProxyObject pobj = (ProxyObject) ih;
return new VARIANT(pobj.getRawDispatch());
} else if (value instanceof IComEnum) {
IComEnum enm = (IComEnum) value;
return new VARIANT(new WinDef.LONG(enm.getValue()));
} else if (value instanceof SAFEARRAY) {
return new VARIANT((SAFEARRAY) value);
} else {
return null;
}
}
use of com.sun.jna.platform.win32.WinDef.BOOL in project android by JetBrains.
the class ElevatedCommandLine method executeAsShellCommand.
/**
* On Windows we will execute this command as a shell command.
* This allows us to specify elevated privileges with the "runas" parameter.
*/
private Process executeAsShellCommand() throws IOException {
// First create a wrapper that sets the current work directory, such that batch files
// may call other batch/executable files in the same directory without specifying the
// directory.
// Note: This was needed for the Haxm silent_install.bat.
String exeName = new File(getExePath()).getName();
File wrapper = FileUtil.createTempFile(FileUtil.getNameWithoutExtension(exeName) + "_wrapper", ".bat", true);
String exePath = new File(getExePath()).getParent();
File logFile = FileUtil.createTempFile("haxm_install_log", ".txt");
FileUtil.writeToFile(wrapper, String.format("@echo off\n" + "setlocal enableextensions\n\n" + "cd /d \"%1$s\"\n\n" + "%2$s -log %3$s %%*", exePath, exeName, logFile));
setExePath(wrapper.getPath());
// Setup capturing of stdout and stderr in files.
// ShellExecuteEx does not allow for the capture from code.
final File outFile = FileUtil.createTempFile("haxm_out", ".txt", true);
final File errFile = FileUtil.createTempFile("haxm_err", ".txt", true);
addParameters(">", outFile.getPath(), "2>", errFile.getPath());
final ShellExecuteInfo info = new ShellExecuteInfo(this);
BOOL returnValue = Shell32Ex.INSTANCE.ShellExecuteEx(info);
final int errorCode = returnValue.booleanValue() ? 0 : Kernel32.INSTANCE.GetLastError();
// and wrap stdout and stderr into their respective {@link InputStream}.
return new Process() {
private HANDLE myProcess = info.hProcess;
private IntByReference myExitCode = new IntByReference(errorCode);
@Override
public OutputStream getOutputStream() {
throw new RuntimeException("Unexpected behaviour");
}
@Override
public InputStream getInputStream() {
return toInputStream(outFile);
}
@Override
public InputStream getErrorStream() {
return toInputStream(errFile);
}
@Override
public int waitFor() {
if (myProcess != null) {
Kernel32.INSTANCE.WaitForSingleObject(myProcess, INFINITE);
Kernel32.INSTANCE.GetExitCodeProcess(myProcess, myExitCode);
Kernel32.INSTANCE.CloseHandle(myProcess);
myProcess = null;
}
return myExitCode.getValue();
}
@Override
public int exitValue() {
return waitFor();
}
@Override
public void destroy() {
waitFor();
}
private InputStream toInputStream(@NotNull File file) {
try {
waitFor();
return new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
};
}
Aggregations