3

I am developing an application that should not require elevated privileges unless the user wants to change a network adapter's IP address. I would like to allow the application to run as invoked however require and maintain elevated privileges after a button is pressed.

With that said, my ElevateProcess() is getting the following exception: Process was not started by this object, so requested information cannot be determined.

My question is: how do I elevate "this" (already running) winform application to admin on a button press. enter image description here

    private void submitIPChangesBtn_Click(object sender, EventArgs e)
    {
        GeneralHelper.ElevateProcess();
        HelperNetwork.setIP("192.168.0.23", "255.255.255.0");
    }

Elevate process function:

    public static Process ElevateProcess()
    {
        Process source = Process.GetCurrentProcess();

        //Create a new process
        Process target = new Process();
        target.StartInfo = source.StartInfo;
        target.StartInfo.FileName = source.MainModule.FileName;
        target.StartInfo.WorkingDirectory = Path.GetDirectoryName(source.MainModule.FileName);

        //Required for UAC to work
        target.StartInfo.UseShellExecute = true;
        target.StartInfo.Verb = "runas";

        try
        {
            if (!target.Start())
                return null;
        }
        catch (Win32Exception e)
        {
            //Cancelled
            if (e.NativeErrorCode == 1223)
                return null;
            throw;
        };
        return target;
    }
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
JCoder
  • 75
  • 5
  • Just to clarify, you must be using .NET Core, right? I think this is expected behavior - https://github.com/dotnet/runtime/issues/29561 – Yehor Androsov May 14 '22 at 19:12
  • @user7313094 my target framework is .net 5.0. Maybe I am missing something (and have edited my title to focus the question) but I am trying to figure out why I am getting an exception while elevating "this " applications privileges via a call to ElevateProcess() – JCoder May 15 '22 at 18:35
  • 1
    A process is elevated, when you start it, you can't elevate a running process. However I suggest you rethink your application by launching child processes (elevated) that do the job that needs elevation. That way your main program can still run under the normal user. If that isn't possible, you can just as well require elevation for your main program, when it starts. – Poul Bak May 16 '22 at 00:10

1 Answers1

0

I am not sure I like this solution (as it may become more complicated as other parts of the application may be pending and just killing the application is less than ideal.

With that said, what I did was pass arguments into the ElevateProcess(...) function and when the process is successfully launched with admin privileges the new process will have to parse the arguments and act on them accordingly. Additionally, to prevent the exception I just needed to comment out the line:

target.StartInfo = source.StartInfo;

enter image description here

Again, I am not a huge fan of needing to kill the current process and starting a one. If there is any way to bring the current process to admin, I would greatly appreciate the recommendation.

JCoder
  • 75
  • 5
  • To get elevated permissions, you must create a new process. But rather that re-starting your main application, you could start some kind of RPC server that only performs those methods that require higher permissions. For example you might stream requests / responses as json via redirecting standard in / out. – Jeremy Lakeman May 16 '22 at 02:48
  • I suggest you to avoid code screenshot and instead copy past it in code sample or snippet – Cédric May 16 '22 at 10:18
  • @JeremyLakeman Thank you for your constructive feedback. I like that idea. It would require some architectural changes to my general approach in the future, but I believe it is a better solution and will have other benefits if I adopt an approach like this. Thanks again. – JCoder Feb 26 '23 at 23:18