How do I elevate my UAC permissions from Java?

According the accepted answer to this SO question, you cannot change the UAC permissions of a running process. According to the answers to this SO question, possible ways to launch a process with elevated permissions are: create a wrapper to launch the JVM (with the appropriate arguments!) with a windows manifest that requests raised privileges, … Read more

Privileges/owner issue when writing in C:\ProgramData\

No, C:\ProgramData, aka FOLDERID_ProgramData, has restricted security settings. Standard users can create files there. But these files are, by default, secured so that only the user that created the file can subsequently modify the file. The recommended solution is for your installer to create a sub directory of C:\ProgramData for your shared storage. And that … Read more

Elevating privileges doesn’t work with UseShellExecute=false

ProcessStartInfo.Verb will only have an effect if the process is started by ShellExecuteEx(). Which requires UseShellExecute = true. Redirecting I/O and hiding the window can only work if the process is started by CreateProcess(). Which requires UseShellExecute = false. Well, that’s why it doesn’t work. Not sure if forbidding to start a hidden process that … Read more

Detect if program is running with full administrator rights

Win9x: Everyone is “admin” NT4: OpenThreadToken/OpenProcessToken + GetTokenInformation(…,TokenGroups,…) on DOMAIN_ALIAS_RID_ADMINS SID in a loop 2000+: OpenThreadToken/OpenProcessToken + CheckTokenMembership on DOMAIN_ALIAS_RID_ADMINS SID Other alternatives are: IsUserAnAdmin or AccessCheck Checking the TOKEN_ELEVATION* stuff in the token is not required for testing the current process but it is useful if you need to find out if the user … Read more

Why do files get placed in “C:\Users\AppData\Local\VirtualStore\Program Files(x86)”?

An application that is not running with raised privileges should does not have access to the Program Files and Program Files (x86) directories. This is good for safety. In addition, in most cases when a developer tells his program to save data in the Program Files folder, for example, program settings, he has completely forgotten … Read more

Request Windows Vista UAC elevation if path is protected?

The best way to detect if they are unable to perform an action is to attempt it and catch the UnauthorizedAccessException. However as @DannySmurf correctly points out you can only elevate a COM object or separate process. There is a demonstration application within the Windows SDK Cross Technology Samples called UAC Demo. This demonstration application … Read more

Run process as administrator from a non-admin application

You must use ShellExecute. ShellExecute is the only API that knows how to launch Consent.exe in order to elevate. Sample (.NET) Source Code In C#, the way you call ShellExecute is to use Process.Start along with UseShellExecute = true: private void button1_Click(object sender, EventArgs e) { //Public domain; no attribution required. ProcessStartInfo info = new … Read more

How to run NOT elevated in Vista (.NET)

The WinSafer API’s allow a process to be launched as a limited, normal, or elevated user. Sample Usage: CreateSaferProcess(@”calc.exe”, “”, SaferLevel.NormalUser); Source code: //http://odetocode.com/Blogs/scott/archive/2004/10/28/602.aspx public static void CreateSaferProcess(String fileName, String arguments, SaferLevel saferLevel) { IntPtr saferLevelHandle = IntPtr.Zero; //Create a SaferLevel handle to match what was requested if (!WinSafer.SaferCreateLevel( SaferLevelScope.User, saferLevel, SaferOpen.Open, out saferLevelHandle, IntPtr.Zero)) … Read more

Windows 7 and Vista UAC – Programmatically requesting elevation in C#

WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator); if (!hasAdministrativeRight) { RunElevated(Application.ExecutablePath); this.Close(); Application.Exit(); } private static bool RunElevated(string fileName) { //MessageBox.Show(“Run: ” + fileName); ProcessStartInfo processInfo = new ProcessStartInfo(); processInfo.Verb = “runas”; processInfo.FileName = fileName; try { Process.Start(processInfo); return true; } catch (Win32Exception) { //Do nothing. Probably the user canceled the UAC window … Read more

How can I run a child process that requires elevation and wait?

Use ShellExecuteEx, rather than ShellExecute. This function will provide a handle for the created process, which you can use to call WaitForSingleObject on that handle to block until that process terminates. Finally, just call CloseHandle on the process handle to close it. Sample code (most of the error checking is omitted for clarity and brevity): … Read more