Search in sources :

Example 96 with IntByReference

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);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) ArrayList(java.util.ArrayList) Pointer(com.sun.jna.Pointer) WString(com.sun.jna.WString) WinDef(com.sun.jna.platform.win32.WinDef) File(java.io.File)

Example 97 with IntByReference

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);
            }
        }
    };
}
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)

Example 98 with IntByReference

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);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference)

Example 99 with IntByReference

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);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference)

Example 100 with IntByReference

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);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)199 PointerByReference (com.sun.jna.ptr.PointerByReference)38 Memory (com.sun.jna.Memory)33 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)26 File (java.io.File)19 Pointer (com.sun.jna.Pointer)15 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)14 PSID (com.sun.jna.platform.win32.WinNT.PSID)13 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)11 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)11 HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)9 ACL (com.sun.jna.platform.win32.WinNT.ACL)8 Advapi32 (com.sun.jna.platform.win32.Advapi32)7 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)7 ACCESS_ALLOWED_ACE (com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE)6 SECURITY_DESCRIPTOR (com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR)6 HKEY (com.sun.jna.platform.win32.WinReg.HKEY)6 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)6 CredHandle (com.sun.jna.platform.win32.Sspi.CredHandle)5