栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

通过UAC提升ProcessBuilder流程?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过UAC提升ProcessBuilder流程?

使用ProcessBuilder无法做到这一点,您需要调用Windows API。

我使用JNA通过类似于以下代码的代码来实现此目的:

Shell32X.java

import com.sun.jna.Native;import com.sun.jna.Pointer;import com.sun.jna.Structure;import com.sun.jna.WString;import com.sun.jna.platform.win32.Shell32;import com.sun.jna.platform.win32.WinDef.HINSTANCE;import com.sun.jna.platform.win32.WinDef.HWND;import com.sun.jna.platform.win32.WinNT.HANDLE;import com.sun.jna.platform.win32.WinReg.HKEY;import com.sun.jna.win32.W32APIOptions;public interface Shell32X extends Shell32{    Shell32X INSTANCE = (Shell32X)Native.loadLibrary("shell32", Shell32X.class, W32APIOptions.UNICODE_OPTIONS);    int SW_HIDE = 0;    int SW_MAXIMIZE = 3;    int SW_MINIMIZE = 6;    int SW_RESTORE = 9;    int SW_SHOW = 5;    int SW_SHOWDEFAULT = 10;    int SW_SHOWMAXIMIZED = 3;    int SW_SHOWMINIMIZED = 2;    int SW_SHOWMINNOACTIVE = 7;    int SW_SHOWNA = 8;    int SW_SHOWNOACTIVATE = 4;    int SW_SHOWNORMAL = 1;        int SE_ERR_FNF = 2;        int SE_ERR_PNF = 3;        int SE_ERR_ACCESSDENIED = 5;        int SE_ERR_OOM = 8;        int SE_ERR_DLLNOTFOUND = 32;        int SE_ERR_SHARE = 26;    int SEE_MASK_NOCLOSEPROCESS = 0x00000040;    int ShellExecute(int i, String lpVerb, String lpFile, String lpParameters, String lpDirectory, int nShow);    boolean ShellExecuteEx(SHELLEXECUTEINFO lpExecInfo);    public static class SHELLEXECUTEINFO extends Structure    {                public int cbSize = size();        public int fMask;        public HWND hwnd;        public WString lpVerb;        public WString lpFile;        public WString lpParameters;        public WString lpDirectory;        public int nShow;        public HINSTANCE hInstApp;        public Pointer lpIDList;        public WString lpClass;        public HKEY hKeyClass;        public int dwHotKey;                public HANDLE hMonitor;        public HANDLE hProcess;        protected List getFieldOrder() { return Arrays.asList(new String[] {     "cbSize", "fMask", "hwnd", "lpVerb", "lpFile", "lpParameters",     "lpDirectory", "nShow", "hInstApp", "lpIDList", "lpClass",     "hKeyClass", "dwHotKey", "hMonitor", "hProcess", });        }    }}

Elevator.java

package test;import test.Shell32X.SHELLEXECUTEINFO;import com.sun.jna.WString;import com.sun.jna.platform.win32.Kernel32;import com.sun.jna.platform.win32.Kernel32Util;public class Elevator{    public static void main(String... args)    {        executeAsAdministrator("c:\windows\system32\notepad.exe", "");    }    public static void executeAsAdministrator(String command, String args)    {        Shell32X.SHELLEXECUTEINFO execInfo = new Shell32X.SHELLEXECUTEINFO();        execInfo.lpFile = new WString(command);        if (args != null) execInfo.lpParameters = new WString(args);        execInfo.nShow = Shell32X.SW_SHOWDEFAULT;        execInfo.fMask = Shell32X.SEE_MASK_NOCLOSEPROCESS;        execInfo.lpVerb = new WString("runas");        boolean result = Shell32X.INSTANCE.ShellExecuteEx(execInfo);        if (!result)        { int lastError = Kernel32.INSTANCE.GetLastError(); String errorMessage = Kernel32Util.formatMessageFromLastErrorCode(lastError); throw new RuntimeException("Error performing elevation: " + lastError + ": " + errorMessage + " (apperror=" + execInfo.hInstApp + ")");        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/483351.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号