Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the Windows execution level to ask user for administrator privileges for a Rust program?

Tags:

windows

rust

I'm writing a Windows CLI app and I need to run it as administrator. In C# I would add this line to app.manifest:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> 

How I do that in Rust?

like image 544
Tomislav Nekic Avatar asked Dec 19 '18 07:12

Tomislav Nekic


People also ask

How do you make a program ask admin?

Right-click on your application or its shortcut, and then select Properties in the context menu. Under the Compatibility tab, check the “Run this program as an administrator” box and click OK. From now on, double-click on your application or shortcut and it should automatically run as administrator.

How do I force administrator privileges?

Hold down both the Ctrl and the Shift keys on your keyboard and then click or tap on that program's shortcut. You can also use the "Ctrl + Shift + Click/Tap" shortcut on an app's Start Menu tile to run it with administrator permissions in Windows 10.

How do I allow a domain user to run a program with administrator rights Windows 10?

To apply the setting to all users on the computer and regardless of which shortcut is used to start the application, click Change Setting For All Users to display the Properties dialog box for the application's .exe file, select the Run This Program As An Administrator check box, and then click OK twice.


1 Answers

Too late, but answering anyway. :-)

Please take a look at the winres library. It contains the following example:

The following manifest will brand the exe as requesting administrator privileges. Thus, everytime it is executed, a Windows UAC dialog will appear.

let mut res = winres::WindowsResource::new();
res.set_manifest(r#"
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
        <requestedPrivileges>
            <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
        </requestedPrivileges>
    </security>
</trustInfo>
</assembly>
"#);

The full example is documented and available here.

I have been using this library in a project that contains an icon and requires admin privileges, its build.rs using winres is hosted here. See the generated executable: enter image description here

HTH

like image 135
silvioprog Avatar answered Oct 03 '22 08:10

silvioprog