flutterwindowsdartadminuac

Run Dart process as admin on Windows


I am working on a program for doing post-imaging tasks on Windows computers. Several of the things it needs to do require running subprocesses as admin. It seems like there are solutions for Linux and macOS but not Windows.

This is one of the things I am trying to run. final ProcessResult userResult = Process.runSync("runas", ["net", "user", "/add", "it_admin"], runInShell: true);

When I look at the output, I get the following error: Encountered an error trying to run "uname -m"

I have tried running the same process without "runas" to no avail. final ProcessResult userResult = Process.runSync("net", ["user", "/add", "it_admin"], runInShell: true);

I have tried adding the following to the runner.exe.manifest

<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
          level="requireAdministrator"
          uiAccess="false"/>
       </requestedPrivileges>
    </security>
  </trustInfo>

and both of the following to CmakeLists.txt SET_TARGET_PROPERTIES(PROPERTIES LINK_FLAGS "/MANIFESTUAC:\"level='requireAdministrator' uiAccess='false'\" /SUBSYSTEM:WINDOWS") SET_TARGET_PROPERTIES(${BINARY_NAME} PROPERTIES LINK_FLAGS "/MANIFESTUAC:\"level='requireAdministrator' uiAccess='false'\" /SUBSYSTEM:WINDOWS")

Any help is greatly appreciated!


Solution

  • There are some reports about bugs in Process.runAsync or Process.run. Can you try using Process.start?

      Process.start(
        'powershell.exe',
        [
          'Start-Process',
          '-Verb',
          'RunAs',
          '-FilePath',
          'powershell.exe',          
        ],
      );
    

    And here you can open notepad as Admin using runSync:

      ProcessResult process = Process.runSync(
        'powershell.exe',
        [
          'Start-Process',
          '-Verb',
          'RunAs',
          '-FilePath',
          'notepad.exe',
        ],
      );