Java 打开 exe 程序

在实现 JavaFX 应用自更新的时候,需要在主应用(main.exe)里打开更新应用(update.exe),一开始想通过 ProcessBuilder 启动一个子进程实现的,但是这种方式打开的应用没有管理员的权限,更新应用需要管理员权限。经过一番查找之后,在 StackOverflow 上发现了另外一种方式可以打开 exe 应用,并且可以通过 UAC 提升为管理员权限。

具体方式就是:用 JNA 调用 Windows 的 API,用系统的接口打开 exe 应用。

添加 JNA

JNA 官网下载对应的 jar 文件加到项目 classpath 即可。

原文的代码

Shell32X.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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;

/** File not found. */
int SE_ERR_FNF = 2;

/** Path not found. */
int SE_ERR_PNF = 3;

/** Access denied. */
int SE_ERR_ACCESSDENIED = 5;

/** Out of memory. */
int SE_ERR_OOM = 8;

/** DLL not found. */
int SE_ERR_DLLNOTFOUND = 32;

/** Cannot share an open file. */
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
{
/*
DWORD cbSize;
ULONG fMask;
HWND hwnd;
LPCTSTR lpVerb;
LPCTSTR lpFile;
LPCTSTR lpParameters;
LPCTSTR lpDirectory;
int nShow;
HINSTANCE hInstApp;
LPVOID lpIDList;
LPCTSTR lpClass;
HKEY hkeyClass;
DWORD dwHotKey;
union {
HANDLE hIcon;
HANDLE hMonitor;
} DUMMYUNIONNAME;
HANDLE hProcess;
*/

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;

/*
* Actually:
* union {
* HANDLE hIcon;
* HANDLE hMonitor;
* } DUMMYUNIONNAME;
*/
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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 + ")");
}
}
}