Console Application Help
-
Hi,
I am developing a console application for windows Vista/7 that takes arguments at runtime. The Console App requires Admin privileges to install services, so I am embedding a manifest file with "requireAdministrator" parameter in it. The Console App works fine and installs and uninstalls the services.
1.> The problem I am facing is when I execute the app from command prompt with arguments, it opens another console window with the desired output. How can I make the console app to print the output in the same window.
2.> Also I am calling this console app from a GUI app, which should obtain the desired output and print in textedit, but it does not gets the output from the console app as the console app is build with Admin privileges. If the console app is built without Admin privileges then the GUI app obtains the output from console app and its print in textedit.Thanks in advance.
-
I fear what you want to do is not possible, because "elevated" and "non-elevated" processes live in different "domains". It is called "integrity levels" in UAC jargon.
bq. In UAC, it limits access between processes running with standard user privilege and elevated processes running with full administrative rights in Admin Approval Mode.
bq. Windows 7 protects processes by marking their integrity levels. Integrity levels are measurements of trust. A "high" integrity application is one that performs tasks that modify system data, such as a disk partitioning application, while a "low" integrity application is one that performs tasks that could potentially compromise the operating system, such as a Web browser. Applications with lower integrity levels cannot modify data in applications with higher integrity levels.
http://msdn.microsoft.com/en-us/library/bb625962.aspx
http://technet.microsoft.com/en-us/library/dd835561(v=ws.10).aspxWorkaround would be to run both, the elevated and the non-elevated process from an "admin" console.
In this case both processes would effectively run with "elevated" rights... -
Hi,
Actually what I want to do is to install services from my GUI app.
Can I build my GUI app with Admin Privileges, so that it can install these services OR I have to install services by building another console app with Admin Privileges? -
If it is okay for you that your GUI app always runs with Admin privileges (will trigger an UAC dialog on application startup and will additionally require Non-Admin users to enter an Admin password!), then you can simply set the UAC Execution Level to "requireAdministrator". Then your GUI app will either run with full Admin privileges or not run at all. This means you will be able to install services and you also will be able to call other apps (Console or GUI doesn't matter) that need an UAC Execution Level of "requireAdministrator". Calling an app that requires elevation from a process that already has been elevated will NOT trigger an UAC dialog again.
Another option would be to let your "main" GUI app run with an UAC Execution Level of "asInvoker", which will avoid the UAC dialog and will allow everybody to start your GUI app. Of course then your GUI app won't be able to install services itself, because it has not been elevated. And it also won't be able to run other apps that need an UAC Execution Level of "requireAdministrator" via CreateProcess() or QProcess. That's because CreateProcess() will simply fail, when you try to run a child-process that needs elevation from a non-elevated parent-process. Bummer! But you still can use ShellExecute() or ShellExecuteEx() instead - they will of course trigger an UAC dialog at the point where the child-process is about to be created. But it works.
So my suggestion would be: Make a GUI app that runs with "asInvoker" Execution Level and make an additional Console app that performs the "administrative" tasks and that runs with "requireAdministrator" Execution Level. The Console app would be called from the GUI app via ShellExecute() or ShellExecuteEx() when needed.
<HINT> Actually it would be possible that a non-elevated instance of the GUI app creates a second elevated instance of itself in the background. The second instance can then do the "administrative" tasks that the first instance can't do. You can explicitly elevate an application (if its Manifest doesn't enforce elevation) by creating it via ShellExecute() and using the "runas" verb. This way you don't need a second (console) app. </HINT>