0

I have a file MyUtilities.exe.

When I run: Process.Start("MyUtilities.exe","SomeParameter");

the exit code of that process is 0 (OK) if it is run WITHOUT admin privileges. If I run that code as an administrator exit code is 1!

Since I did not create MyUtilities.exe I cannot modify it.

In the end I need to run Process.Start("MyUtilities.exe","SomeParameter"); as an administrator And have it return an exit code = 0. The way I managed to do that was by changing its compatibility to:

enter image description here

(Right click on the file->Properties->Compatibility->Run this program as an administrator)

after changing that now I am able to run Process.Start("MyUtilities.exe","SomeParameter"); as an administrator and have it return an exit code of 0.

So my question is how can I change the compatibility of that file with code so that I don't have to tell users to right click on the file then change the settings.

Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • Anything in here helps? http://stackoverflow.com/questions/2532769/how-to-start-a-process-as-administrator-mode-in-c-sharp – Crono Feb 24 '14 at 18:17
  • 1
    Is it essential to call MyUtilities.exe directly? Would it be an option to call MyUtilitiesStarter.exe which calls MyUtilities.exe? – Linky Feb 24 '14 at 18:37
  • I don't understand. Do you want to start the other process elevated or not elevated. – David Heffernan Feb 24 '14 at 19:35
  • I don't mind if it is called elevated or not. What I do care is that exit code is 0. If I run my c# code as NON administrator then exit code is 0 if that exe is run as non admin as well. If I am running my c# code as admin then myUtilities.exe also has to run as admin in order to get exit code = 0. In other words c# and myUtilites have to be running with same priviledges in order to get exit code = 0. Because my c# code will be runing as an admin I guess the answer is yes I need myUtilities.exe to be runing as admin priviledges too. – Tono Nam Feb 24 '14 at 19:47
  • 3
    Your analysis is flawed. When an elevated process starts another process, that child process runs elevated. You do need to diagnose the problem. – David Heffernan Feb 24 '14 at 20:59

2 Answers2

8

This looks pretty simple. Just add a value to the registry.

Key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers -or- HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers

Name: f:\ull\path\to\executable.exe

Value: RUNASADMIN

Here's how you would do this in code. If you write to HKCU, the calling process will not need to be running as Administrator.

static void SetRunAsAdmin(string exeFilePath)
{
    var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers", true);
    if (key == null)
        throw new InvalidOperationException(@"Cannot open registry key HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers.");
    using (key)
        key.SetValue(exeFilePath, "RUNASADMIN");
}
Michael Gunter
  • 12,528
  • 1
  • 24
  • 58
  • 1
    If the OP can't programmatically write to the registry, he's in over his head. :) – Michael Gunter Feb 24 '14 at 19:21
  • That's not the point. The process could be executed in a security context that wouldn't allow it to write to the registry but still allow an outer process to be run (popping up the "do you want to run as admin blabla" dialog). – Crono Feb 24 '14 at 19:27
  • 1
    That seems bogus. Just set UseShellExecute to true and use the runas verb. That said, it's not at all clear to me that this is even what the asker wants to do. – David Heffernan Feb 24 '14 at 19:35
  • OP concluded his post with a simple question: "how can I change the compatibility of that file with code?" This is how you do that. – Michael Gunter Feb 24 '14 at 19:38
  • It might be what was asked but its clearly not the answer. And run as is much cleaner. And you presumably need to avoid registry redirection. – David Heffernan Feb 24 '14 at 21:00
  • The idea here is that you can change the compatibility property of run as admin. This answer is how to do that and it works. – Chris May 14 '19 at 19:48
1

If the calling process is executed with administrator privilege, and it start to spawn "MyUtilites.exe", it will also launch as Administrator. Just go to your project's properties and look for the manifest file written in XML format. You'll see "asInvoker" in that file, change it to "requireAdministrator".