Monday, June 14, 2010

Using Named Pipe and self-elevation feature of Vista in a console application

Project Description
NPipeWithElevatedProc, make it easier for console application users, running programs with administrator privileges. The processing messages are always shown on the calling console window. The comunication is via named pipe. It is developed in C++, with VisualStudio 2010.


Where you can use it
Suppose you have to realize a console application, that requires administrator privileges for doing something and the user need run it indifferently from a console window (running or not as administrator).
If a user run the application in a normal window, using only the self-elevation, you can execute the program in an other elevated console window... but when it starts, likely the calling's windows remains opened and you will have to write something to user... the new one, when the program shall be over, probably shall close itself. Moreover, if the user starts the application in console running as administrator, the behaviour shall be different.
Target of this project is supply a solution at this problem: using a named pipe, the new elevated application instance, sends processing phase messages to the calling program, that display them as it was doing the operation by itself; the real process doing the privileged operations shall do it in background, with an hidden console; and the user will not notice nothing else the "Yes", he will have eventually given to the Consent UI of the UAC of Vista and earlier version of Windows.

The code description
At the entry point, the application checks the arguments for knowing if the user execute it or that is the elevated one. In the first case, it proceeds, checking the privileges (it runs as administrator?). If so, nothing to do, besides the normal operations. If the application needs to be elevated, the following steps are done:
1) a named pipe is created;
2) the application is re-executed with an argument "child", to signal that it shall be the elevated instance;
3) after this, the main process application, waits for a client connected to the named pipe; when it shall succeed, it display on screen the messages it shall have received.
4) The elevated instance, for the "child" argument, knows what it is, and the first thing it does is connecting to the named pipe that the calling program has created; after this, it continues with the privileged operations, sending back to the caller, the messages signaling the processing phase it has done.
5) When all is done, this elevated instance disconnects from the pipe and close; the main program instance notices this disconnection and close the pipe and itself.
6) As already written, the user can only see the "Yes", he will have eventually given to the Consent UI of the UAC of Vista and earlier version of Windows.

This is all ..
If you have questions, suggestions, .. please contact me or post a message.

Read more: Codeplex

Posted via email from .NET Info