Making your assembly run "as Administrator" in Vista

This came from one of my favorites in the C# newsgroup, Willy DeNoyette. I'll simply post the questions and answers directly, since there is no need to modify it except for some formatting and cleanup:

Q) I need to restart the "Windows Audio Service" (audiosrv) via C#. I'm using the ServiceController Class to do this.
It is no problem under XP and no problem under vista if UAC is disabled.
But with enabled UAC i'm getting a "access refused" exception.
I also tried to do it via console with "net start ..." but the same error appears.
Three questions:
1. Is it possible to restart the "windows audio service" if UAC is
enabled ?

A) Yes when running as full "Administrator". That is start the console (cmd interpreter) by right clicking "Run as Administrator".

2. Why does the exception occur? If I do some other stuff, e.g. starting regedit via Process.Start() the user gets asked if he really wants that. I would expect the same behaviour for restarting
services.

A) The exception occurs because you are running as a standard user, only admins can start, stop .... services.
If you want the same behavior you'll have to insert a "manifest" in your executable file.

<?xml version="1.0" encoding="utf-8" ?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" >
<assemblyIdentity version="1.0.0.0"
processorArchitecture="X86"
name="someExecName"
type="win32" />
<description>Program description</description>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" />
</requestedPrivileges>
</security>
</trustInfo>
</assembly>



To add above manifest to the executable assembly, you have to run mt.exe like this:

mt -manifest somename.exe.manifest -outputresource:somename.exe;#1

PS change the someExecName and Program description to suit your needs...

3. If it's not possible at all: Can I find out programmatically
whether UAC is enabled?

A) See above

To further clarify:save the above manifest in a file called "some.exe.manifest", where someApp is the name of your executable file. Say you have "audioCntrl.exe", then you could name your manifest "audioCntrl.exe.manifest" and run mt.exe like:

mt -manifest audioCntrl.exe.manifest -outputresource:audioCntrl.exe;#1

Comments

  1. Anonymous6:49 PM

    The manifest doesn't even need to be embedded (though I think it is better to be in the exe). As long as the name matches and it's in the same folder as the exe - Vista will consider it. It's not only for .NET compiled executables either - any exe can be fit this way to require privileges.
    Now if I can just find a way to have an ActiveX control make Vista to ask for promotion the same way (embedding a manifest there doesn’t seem to work)…

    ReplyDelete
  2. Anonymous2:31 PM

    The exe truns off "Run as Administrator" after installing it on client's computer. How to keep exe "run as administrator" after installation?

    ReplyDelete

Post a Comment

Popular posts from this blog

FIREFOX / IE Word-Wrap, Word-Break, TABLES FIX

Some observations on Script Callbacks, "AJAX", "ATLAS" "AHAB" and where it's all going.

ASP.NET "App_Data": Writing files vs Application Restarts