Search in sources :

Example 1 with SHELLEXECUTEINFO

use of com.sun.jna.platform.win32.ShellAPI.SHELLEXECUTEINFO in project jna by java-native-access.

the class Shell32Test method testShellExecuteEx.

public void testShellExecuteEx() {
    File file = new File(System.getProperty("java.io.tmpdir"), System.nanoTime() + ".txt");
    try {
        try {
            fillTempFile(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        SHELLEXECUTEINFO lpExecInfo = new SHELLEXECUTEINFO();
        // to avoid opening something and having hProcess come up null
        // (meaning we opened something but can't close it)
        // we will do a negative test with a bogus action.
        lpExecInfo.lpVerb = "0p3n";
        lpExecInfo.nShow = User32.SW_SHOWDEFAULT;
        lpExecInfo.fMask = Shell32.SEE_MASK_NOCLOSEPROCESS | Shell32.SEE_MASK_FLAG_NO_UI;
        lpExecInfo.lpFile = file.getAbsolutePath();
        assertFalse("ShellExecuteEx should have returned false - action verb was bogus.", Shell32.INSTANCE.ShellExecuteEx(lpExecInfo));
        assertEquals("GetLastError() should have been set to ERROR_NO_ASSOCIATION because of bogus action", W32Errors.ERROR_NO_ASSOCIATION, Native.getLastError());
    } finally {
        if (file.exists()) {
            file.delete();
        }
    }
}
Also used : SHELLEXECUTEINFO(com.sun.jna.platform.win32.ShellAPI.SHELLEXECUTEINFO) IOException(java.io.IOException) File(java.io.File)

Example 2 with SHELLEXECUTEINFO

use of com.sun.jna.platform.win32.ShellAPI.SHELLEXECUTEINFO 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);
            }
        }
    };
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) WString(com.sun.jna.WString) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

WString (com.sun.jna.WString)1 SHELLEXECUTEINFO (com.sun.jna.platform.win32.ShellAPI.SHELLEXECUTEINFO)1 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)1 IntByReference (com.sun.jna.ptr.IntByReference)1 File (java.io.File)1 IOException (java.io.IOException)1 NotNull (org.jetbrains.annotations.NotNull)1