Package com.peterhi.win32

Source Code of com.peterhi.win32.Win32Functions

package com.peterhi.win32;

import com.sun.jna.WString;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.Win32Exception;

public class Win32Functions {

  public static void runAs(String name, String parameters) throws Win32Exception {
    if (name == null || name.isEmpty()) {
      throw new IllegalArgumentException();
    }
   
    if (parameters == null) {
      parameters = "";
    }
   
    SHELLEXECUTEINFO lpExecInfo = new SHELLEXECUTEINFO();
    lpExecInfo.fMask = 0x00000040; // SEE_MASK_NOCLOSEPROCESS;
    lpExecInfo.lpVerb = new WString("runas");
    lpExecInfo.lpFile = new WString(name);
    lpExecInfo.lpParameters = new WString(parameters);
    lpExecInfo.nShow = User32.SW_SHOWDEFAULT;
   
    if (!Shell32ExLibrary.INSTANCE.ShellExecuteEx(lpExecInfo)) {
      int lastError = Kernel32.INSTANCE.GetLastError();
      throw new Win32Exception(lastError);
    }
  }
 
}
TOP

Related Classes of com.peterhi.win32.Win32Functions

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.