use of com.sun.jna.ptr.IntByReference in project intellij-community by JetBrains.
the class Restarter method restartOnWindows.
private static void restartOnWindows(String... beforeRestart) throws IOException {
Kernel32 kernel32 = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
Shell32 shell32 = (Shell32) Native.loadLibrary("shell32", Shell32.class);
int pid = kernel32.GetCurrentProcessId();
IntByReference argc = new IntByReference();
Pointer argvPtr = shell32.CommandLineToArgvW(kernel32.GetCommandLineW(), argc);
String[] argv = getRestartArgv(argvPtr.getWideStringArray(0, argc.getValue()));
kernel32.LocalFree(argvPtr);
// See https://blogs.msdn.microsoft.com/oldnewthing/20060515-07/?p=31203
// argv[0] as the program name is only a convention, i.e. there is no guarantee
// the name is the full path to the executable.
//
// See https://msdn.microsoft.com/en-us/library/windows/desktop/ms683197(v=vs.85).aspx
// To retrieve the full path to the executable, use "GetModuleFileName(NULL, ...)".
//
// Note: We use 32,767 as buffer size to avoid limiting ourselves to MAX_PATH (260).
char[] buffer = new char[32767];
if (kernel32.GetModuleFileNameW(null, buffer, new WinDef.DWORD(buffer.length)).intValue() > 0) {
argv[0] = Native.toString(buffer);
}
List<String> args = new ArrayList<>();
args.add(String.valueOf(pid));
args.add(String.valueOf(beforeRestart.length));
Collections.addAll(args, beforeRestart);
args.add(String.valueOf(argv.length));
Collections.addAll(args, argv);
runRestarter(new File(PathManager.getBinPath(), "restarter.exe"), args);
// Since the process ID is passed through the command line, we want to make sure that we don't exit before the "restarter"
// process has a chance to open the handle to our process, and that it doesn't wait for the termination of an unrelated
// process which happened to have the same process ID.
TimeoutUtil.sleep(500);
}
use of com.sun.jna.ptr.IntByReference 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);
}
}
};
}
use of com.sun.jna.ptr.IntByReference in project chipKIT32-MAX by chipKIT32.
the class Registry method createKey.
/**
* Create a new key.
*
* @param rootKey root key
* @param parent name of parent key
* @param name key name
* @return true on success
*/
public static boolean createKey(REGISTRY_ROOT_KEY rootKey, String parent, String name) {
Advapi32 advapi32;
IntByReference hkResult, dwDisposition;
int handle = 0;
boolean ret = false;
advapi32 = Advapi32.INSTANCE;
hkResult = new IntByReference();
dwDisposition = new IntByReference();
handle = openKey(rootKey, parent, WINNT.KEY_READ);
if (handle != 0) {
if (advapi32.RegCreateKeyEx(handle, name, 0, null, WINNT.REG_OPTION_NON_VOLATILE, WINNT.KEY_READ, null, hkResult, dwDisposition) == WINERROR.ERROR_SUCCESS) {
ret = true;
advapi32.RegCloseKey(hkResult.getValue());
} else {
ret = false;
}
advapi32.RegCloseKey(handle);
}
return (ret);
}
use of com.sun.jna.ptr.IntByReference in project chipKIT32-MAX by chipKIT32.
the class Registry method getRegistryRootKey.
/**
* Gets one of the root keys.
*
* @param key key type
* @return root key
*/
private static int getRegistryRootKey(REGISTRY_ROOT_KEY key) {
Advapi32 advapi32;
IntByReference pHandle;
int handle = 0;
advapi32 = Advapi32.INSTANCE;
pHandle = new IntByReference();
if (advapi32.RegOpenKeyEx(rootKeyMap.get(key), null, 0, 0, pHandle) == WINERROR.ERROR_SUCCESS) {
handle = pHandle.getValue();
}
return (handle);
}
use of com.sun.jna.ptr.IntByReference in project chipKIT32-MAX by chipKIT32.
the class Registry method valueExists.
/**
* Check for existence of a value.
*
* @param rootKey root key
* @param subKeyName key name
* @param name value name
* @return true if exists
*/
public static boolean valueExists(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name) {
Advapi32 advapi32;
IntByReference pType, lpcbData;
byte[] lpData = new byte[1];
int handle = 0;
boolean ret = false;
advapi32 = Advapi32.INSTANCE;
pType = new IntByReference();
lpcbData = new IntByReference();
handle = openKey(rootKey, subKeyName, WINNT.KEY_READ);
if (handle != 0) {
if (advapi32.RegQueryValueEx(handle, name, null, pType, lpData, lpcbData) != WINERROR.ERROR_FILE_NOT_FOUND) {
ret = true;
} else {
ret = false;
}
advapi32.RegCloseKey(handle);
}
return (ret);
}
Aggregations