How to change Program Name is UAC? - c#

I'm running msiexec.exe with the following code.
Process setupProc = new Process();
setupProc.StartInfo.UseShellExecute = true;
setupProc.StartInfo.CreateNoWindow = true;
setupProc.StartInfo.FileName = "msiexec.exe";
setupProc.StartInfo.Arguments = String.Format("/i \"{0}\" /qn {1}",
sSetupFilePath, installerProperties);
setupProc.StartInfo.Verb = "runas";
setupProc.Start();
I want to change Program Name in the UAC, Is this possible at all to change program name.
Program name coming as "C:\Windows\SysWOW64\msiexec.exe with the arguments I provide to run.
I just want to get rid of the arguments coming the rhe msiexec.exe.
I did lot of rnd but could not get possible solution and I think it is not possible.

You should explain what your actual goal is. For example, if you wish to launch an MSI install from a standalone process, then give that process an elevation manifest so it will request elevation (your program name) and run elevated, and then use the CreateProcess version of your code, UseShellExecute=false.
Or if your code is already running elevated just set UseShellExecute=false.
In both cases msiexec will run elevated and not show elevation requests because you are firing it off in CreateProcess mode.
BTW, if you're running this from a custom action of an MSI install there's a good chance it will fail - this is really not recommended at all.

Related

Killing process as admin via another process

I'm trying to kill some processes by their names (specific names that I already know) In C#. I find them and kill them with Process.Kill(), but sometimes on some processes i get 'access denied'. I assume it is because I'm not running them as an admin.
I created a batch that does the same, and if I run it as an admin it kills them all, otherwise not.
I can run the batch as an admin via the c# code, i.e:
var psi = new ProcessStartInfo();
psi.Verb = "runas"; //This suppose to run the command as administrator
//Then run a process with this psi
My question is, is this really a way to solve the access problem? Is there a better way? If I run my C# code as an admin, does Process.Kill() suppose to have the same result as doing it with the batch file?
What you are talking about are Elevated rights.
You need the programm that finds the programms and sends out the kills to always run Elevated. The most reliable way to do that, is to add this requirement to the Programm Manifest. It is something the UAC will read to help you.
The second most reliable way is to check if you got the rights. And if not, have the programm try to (re)start itself elevated. I did write some sample code for this a while back:
using System;
using System.Diagnostics;
using System.IO;
namespace RunAsAdmin
{
class Program
{
static void Main(string[] args)
{
/*Note: Running a batch file (.bat) or similar script file as admin
Requires starting the interpreter as admin and handing it the file as Parameter
See documentation of Interpreting Programm for details */
//Just getting the Absolute Path for Notepad
string windir = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
string FullPath = Path.Combine(windir, #"system32\notepad.exe");
//The real work part
//This is the programm to run
ProcessStartInfo startInfo = new ProcessStartInfo(FullPath);
//This tells it should run Elevated
startInfo.Verb = "runas";
//And that gives the order
//From here on it should be 100% identical to the Run Dialog (Windows+R), except for the part with the Elevation
System.Diagnostics.Process.Start(startInfo);
}
}
}
Note that regardless how you try to elevate, Eleveation can fail in very rare OS settings.

Launch MSI installer in silent mode from code

I have a task to develop an update agent that launches an msi file after downloading it, the installation has to be invisible to the user.
But i have a problem with launching it with no UI. I tried using /q and /qn but it doesn't work, it only works with UI options.
internal static class MSI_runner
{
public static bool RunInstallMSI(string sMSIPath)
{
try
{
Console.WriteLine("begin");
//Starting to install application
Process process = new Process();
process.StartInfo.FileName = "msiexec.exe";
process.StartInfo.Arguments = string.Format(" /q /i \"{0}\" REINSTALLMODE=amus ", sMSIPath);
Console.WriteLine("start");
process.Start();
process.WaitForExit();
Console.WriteLine("end");
return true;
}
catch
{
// "There was a problem installing the application!
return false; //Return False if process ended unsuccessfully
}
}
}
The most likely reason is that the install requires elevation, so there are a few things that this affects, but there are some guesses here because your "but it doesn't work" isn't very specific.
When you run it in UI mode it probably asks for elevation. An administrator would just get an elevation prompt, a limited user would be asked to enter admin credentials. Either way, it runs elevated. When you run it silently the elevation prompt is not shown (silent means silent) and therefore it fails silently too because it requires elevated privileges.
Your code almost certainly defaults to ProcessStartInfo.UseShellExecute=true, so any credentials of the process will not be used to launch the MSI. In situations where the MSI needs elevation and you want to install it silently you must give your exe a manifest for elevation (so it prompts) or runs elevated some other way. You also need UseShellExecute to be false so that the launch is a CreateProcess type of launch where the process privileges are inherited into the process you launch. In addition, there's also no real need to launch anything and get into this type of issue. If your process is elevated then just call MsiInstallProduct passing the path to the MSI and the command line. Then you know that the install runs with your privileges.
https://msdn.microsoft.com/en-us/library/windows/desktop/aa370315(v=vs.85).aspx
Assuming that your code is working, and as an aside, it's not clear what type of upgrade you are expecting. There's no mention of whether your MSI has a new ProductCode, ProductVersion etc. The normal method of applying a small update by reinstalling a new MSI with REINSTALLMODE is here:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa367575(v=vs.85).aspx
and REINSTALLMODE should be vomus.
try cactch (Exception ex) and see what ex.message is.
check here for msiexec params :
http://www.advancedinstaller.com/user-guide/msiexec.html

Process.Start(); as another user works but explorer.exe iexplore.exe throws exception

I'm having problems with a program and its buttons (I know, I'm awesome lol) the buttons can be "programmed" to run programs. They also can be set to run as admin (different credentials).
If I set up simply notepad or cmd or explorer it runs like charm. But if I start iexplore it has got no admin rights.
I had problems before with running explorer.exe the solution was that I had to run it by typing the full path C:\windows\explorer.exe to be able to run it but that I solved it by setting up the VB2015 compiler (?) to Platform target: x64.
My other problem is that if I try to run dsa.msc or generally anything ends with msc it throws the following exception, even if I set up the full path to the syswow64 (or the system32) folder like c:\windows\syswow64\dsa.msc
"The specified executable is not a valid application for this OS platform."
Running the C:\Windows\System32\mmc.exe "services.msc" (or syswow64, with or without the /computer= switch) throws
"The requested operation requires elevation." which I have since I'm able to run services.msc (and all other msc-s from command line with the same user rights)
Thank you.
A beginner.
Basically you don't need to run the host app as administrator! There is a variable (inside your Process instance) called StartInfo (which is an instance of the ProcessStartInfo Class), where Verbs could be used as followed:
Process p = new Process()
{
StartInfo = new ProcessStartInfo("E:\\Users\\Temp\\app.exe")
{
Verb = "runas"
}
};
p.Start();
This will prompt the user to run the app.exe as administrator.
Edit
Running a Process as a defined user:
Process p = new Process()
{
StartInfo = new ProcessStartInfo("E:\\Users\\Temp\\app.exe")
{
Verb = "runas",
Arguments = "/user:Vira"
}
};
For more information about those RUNAS Arguments, click me! :)

run shell command (manage-bde) as administrator from C#

I need to run "manage-bde" shell command from C# code.
The main application process is already running as administrator and is Elevated.
I used code from : UAC self-elevation example on MS website for confirming the app process is elevated.
(http://code.msdn.microsoft.com/windowsdesktop/CSUACSelfElevation-644673d3)
However, when I try to run manage-bde from the C# code, I get "System can't find file specified".
Process p = new Process();
p.StartInfo.FileName = "C:\\Windows\\System32\\manage-bde.exe";
p.StartInfo.UseShellExecute = true;
p.Start();
As a workaround, I tried to create a batch file that runs the command.
string batchFileName = DateTime.Now.Ticks + ".bat";
StreamWriter writer = new StreamWriter(batchFileName);
writer.WriteLine("manage-bde");
writer.Flush();
writer.Close();
Process p = new Process();
p.StartInfo.FileName = batchFileName;
p.StartInfo.UseShellExecute = true;
p.Start();
The batch file is written , and executed successfully; However, the command "manage-bde" is not recognized.
I changed the code to use the verb "runas" and use admin password and that works, but I want the batch file to work without the need for providing the admin password. The current logged in user is already administrator on the computer but the batch file is not getting executed with the existing admin privileges . I need the batch file to execute and manage-bde to run successfully.
Your help or advice will be very highly appreciated :)
ps: some commands other than manage-bde work fine without need for admin runas.
The reason of the behavior I encountered was the Windows File System Redirector.
In most cases, whenever a 32-bit application attempts to access %windir%\System32, the access is redirected to %windir%\SysWOW64
https://msdn.microsoft.com/en-us/library/windows/desktop/aa384187%28v=vs.85%29.aspx
My application build was 32 bits. Whenever it tried to access System32 windows automatically redirected it to SysWow64 which does not contain "manage-bde.exe". I changed the build to 64 bits and then the application could access manage-bde.exe from System32
Even if you're running as the Administrator user, you're not fully elevated if UAC is running. Meaning that you'll have either the UAC prompt come up or you'll be prompted for a password.
The only real way you could get around that is to run your application elevated first, or to write a service that runs with elevated permissions to start your new process.
The alternative of course is to disable UAC, but that is undesirable in most situations.

Process.Start() not spawning new process under the same user

I was always under the impression that when you're running a process as (domain\user) mydomain\myuser, when using Process.Start() it would start this new process using the same credentials - mydomain\myuser.
The issue I'm having is that my Process.Start() call seems to be creating a process under the SYSTEM account which is causing me permission issues in the started process (which must run under an admin account due to the work it does). If it changes things - I'm spawning this process (a custom built exe) from within a windows installer.
Any suggestions? I've read about windows group policies (possibly) having an impact on this, but if I'm honest, it's lost on me.
EDIT: a little snippet:
Where exename and commandLine are parameters for this method body:
ProcessStartInfo procInfo = new ProcessStartInfo(exeName, commandLine);
procInfo.WorkingDirectory = workingDirectory;
procInfo.UseShellExecute = false;
procInfo.CreateNoWindow = true;
Process process = Process.Start(procInfo);
Process.WaitForExit();
return process.ExitCode;
Either set procInfo.UseShellExecute to true, or execute cmd as a process with your exe as a parameter to the cmd command. When UseShellExecute is set to false, here are a lot of interesting side effects: UseShellExecute
Your impression is true. Process.Start() will always start the new process under current user's credentials - unless you provide alternative credentials in the ProcessStartInfo or use one of the overloads that take credentials.
There must be another problem - share a snippet of your code.
UPDATE
OK! You did not mention anything about installer. All MSI installers will be running under system since they will be run by "Windows Installer" which you can check and they run under SYSTEM.

Categories