Granting admin priveleges to a process started from a C# app

R

Ray Mitchell

Hello,

I wrote a C# GUI application that I use to install, uninstall, start, and
stop arbitrary Windows services. Although I would like to have handled all
the gorey details in the C# code itself, I only found one example of how to
do this on the Internet and I could never get it to work properly. As an
alternative I decided to merely use the C# code as a GUI wrapper around
running the Microsoft "installutil.exe" executable as a process. As klugey
as it may seem it worked perfectly in all respects, but that was under
Windows XP. When I now try use this setup on Windows 7 I get the following
error message every time my C# application tries to run the installutil.exe
process: <The requested operation requires elevation: Installer file
"installutil.exe".> This apparently means that I somehow have to grant
administrator privileges to the running of installutil.exe, even though I'm
already logged on as administrator. So, I guess it really all boils down to
how to set the appropriate privileges for a process that is started from
within a C# application so it will work properly on Windows 7 (and I assume
Windows Vista too). Is there something I can change in my code so that it
sets the privileges appropriately to run this process?

As an aside, if anyone knows of some C# code that actually works directly to
install, uninstall, start, and stop arbitrary Windows services without the
need for installutil.exe at all, I would appreciate information on it.
However, I assume I will still need to deal with the administrator privilege
issue.

Thanks,
Ray
 
P

Peter Duniho

Ray said:
[...] <The requested operation requires elevation: Installer file
"installutil.exe".> This apparently means that I somehow have to grant
administrator privileges to the running of installutil.exe, even though I'm
already logged on as administrator. So, I guess it really all boils down to
how to set the appropriate privileges for a process that is started from
within a C# application so it will work properly on Windows 7 (and I assume
Windows Vista too). Is there something I can change in my code so that it
sets the privileges appropriately to run this process?

Unfortunately, I don't have the details off the top of my head. But I
know that with the UAC feature introduced in Vista, there are a few
things to keep in mind:

– Even logged in as admin, your process does not normally run with
admin privileges

– There is some API somewhere that automatically requests elevated
permissions; I don't recall what it is, nor even whether it's exposed
via .NET

– In the app manifest, you can specify that your application requires
elevated permissions

With all of the above in mind, you can see that you have a few viable
options. The quick-and-dirty is to right-click your .exe or shortcut
and choose the "Run as Administrator" option. This will elevate the
privileges so that you can do admin stuff.

For an application that has as its sole purpose a need to do admin
stuff, you probably want to configure the manifest so that the app is
marked as needing admin privileges.

For an application that is mostly a user application, but occasionally
requires elevation, you should look into whatever API it is that allows
you to do that, so that the application uses normal user privileges most
of the time.

The elevation API is, if I recall, part of the broader "impersonation"
API. In any case, a Google search on "elevation" and/or
"impersonation", especially if you include ".NET" or "Windows 7" in the
search, should turn up some useful information.

Pete
 
M

Mr. Arnold

Ray Mitchell said:
Hello,

I wrote a C# GUI application that I use to install, uninstall, start, and
stop arbitrary Windows services. Although I would like to have handled all
the gorey details in the C# code itself, I only found one example of how to
do this on the Internet and I could never get it to work properly. As an
alternative I decided to merely use the C# code as a GUI wrapper around
running the Microsoft "installutil.exe" executable as a process. As klugey
as it may seem it worked perfectly in all respects, but that was under
Windows XP. When I now try use this setup on Windows 7 I get the following
error message every time my C# application tries to run the installutil.exe
process: <The requested operation requires elevation: Installer file
"installutil.exe".> This apparently means that I somehow have to grant
administrator privileges to the running of installutil.exe, even though I'm
already logged on as administrator. So, I guess it really all boils down to
how to set the appropriate privileges for a process that is started from
within a C# application so it will work properly on Windows 7 (and I assume
Windows Vista too). Is there something I can change in my code so that it
sets the privileges appropriately to run this process?

Windows Vista does not automatically assign Administrator privileges to
users, even if the user is an administrator. When performing tasks which
require administrator privileges, User Account Control (UAC) activates and
prompts the user for permission. The process is then elevated.

Windows 7 is Vista.
As an aside, if anyone knows of some C# code that actually works directly to
install, uninstall, start, and stop arbitrary Windows services without the
need for installutil.exe at all, I would appreciate information on it.
However, I assume I will still need to deal with the administrator privilege
issue.

You what installutil.exe to run with the proper privileges within your
program, then I suspect you're going to have to set RunAsAdmin in the
properties of the exe itself. You do that by user Exploer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top