views:

306

answers:

2

I've got a WinForms application that I am working on. There is one small piece of functionality that needs to be run as an administrator in Vista/Win7. I understand how I can set the requestedExecutionLevel for the application in the manifest. The trick is, I don't want to require the user to run the entire application as an administrator, just one part of it. So I would like to have most of the functionality run asInvoker.

If I put the admin functionality in a dll, is there a way to mark it as requireAdministrator? I tried to use MT to add a manifest to the dll, but that didn't seem to work. What do I need to do?

+2  A: 

No there is no way to differentiate the execution level of an application on a DLL by DLL basis. This is a process wide setting. You'd have to invoke another process within your application that runs the code in that DLL with elevated privs.

One option you do have though is to use either the rundll or rundll32 program to run the DLL directly. This is a standalone windows program designed to load and run a particular DLL. You could elevate the rundll process and get the isolation you desire.

Googling for rundll will give you plenty of advice on how to use it :).

JaredPar
Can a DLL be executed as it's own process or does it have to be launched from an EXE?
epotter
@epotter I updated my answer a bit on this subject.
JaredPar
+2  A: 

Elevation is per-process, so you can't have a DLL elevated by itself. You need to look at hosting the DLL in a separate, elevated process; or you can look at the elevation COM moniker, and do it that way.

Roger Lipscombe