diff --git a/java/net/i2p/router/Elevator.java b/java/net/i2p/router/Elevator.java new file mode 100644 index 0000000..3f69327 --- /dev/null +++ b/java/net/i2p/router/Elevator.java @@ -0,0 +1,29 @@ +package net.i2p.router; + +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 + ")"); + } + } +} diff --git a/java/net/i2p/router/Shell32X.java b/java/net/i2p/router/Shell32X.java index 63f7c7a..c7906eb 100644 --- a/java/net/i2p/router/Shell32X.java +++ b/java/net/i2p/router/Shell32X.java @@ -1,3 +1,5 @@ +package net.i2p.router; + import java.util.*; import com.sun.jna.Native;