tags:

views:

148

answers:

2

My application only very occasionally requires privilege elevation... I need to reference some 3rd party COM components that only work correctly when run as administrator.

I would like my application to request privilege elevation only when it needs it... Generally, I don't want my application to run as administrator unless I need to use the 3rd party COM components.

I see that CoCreateAsAdmin could potentially solve the problem, but the component author doesn't set up the required registry entries, and I'm not sure how to use CoCreateAsAdmin in C# and in conjuction with Runtime-Callable-Wrapper that is created by tlbimp.

Another solution would be to spawn another process, but I have no experience with this yet... I don't want to create a completely separate application... I would be happy to create an assembly that runs in a separated elevated process if someone can show me how to make it work.

Thanks...

+1  A: 

Look at these articles:
http://victorhurdugaci.com/using-uac-with-c-part-1/
http://victorhurdugaci.com/using-uac-with-c-part-2/
http://victorhurdugaci.com/using-uac-with-c-part-3/

Romain Hippeau
This is helpful, but doesn't provide an example of what I want to do. If I create a separate process to run the privileged operation, is there an easy way for it to communicate back to the process that is running as a standard user?
Cameron Peters
+1  A: 

Process privileges are based on the user token that the process is running under, which is only assigned when the process starts. Therefore there isn't any way to elevate your process after it's already started. The only reliable way to elevate at runtime is to spawn a new process.

Fortunately, this is pretty easy. Use the Process object to execute the command-line that will register and launch the COM server. Make sure that the app manifest of the .exe you're running marks it as requiring elevation, and the user will automatically be prompted before it runs.

JSBangs