Run C# WPF application with uiAccess = true in manifest at startup

1

The application needs to stay on top of metro, hence the need for the uiAccess flag. This is a recent change in the application. In the previous version, where the uiAccess flag was not set, we could set the application to run on user access using this scheduled task:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2014-04-23T11:31:11.9188616</Date>
    <Author>MU15\Utente</Author>
  </RegistrationInfo>
  <Triggers>
    <LogonTrigger>
      <Enabled>true</Enabled>
    </LogonTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>MU15\Utente</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
    <AllowHardTerminate>false</AllowHardTerminate>
    <StartWhenAvailable>true</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>false</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
    <Priority>7</Priority>
    <RestartOnFailure>
      <Interval>PT1M</Interval>
      <Count>3</Count>
    </RestartOnFailure>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>"C:\Program Files (x86)\path\to\application.exe"</Command>
    </Exec>
  </Actions>
</Task>

We recently added the following application manifest:

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app" />
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="requireAdministrator" uiAccess="true" />
      </requestedPrivileges>
      <applicationRequestMinimum>
        <defaultAssemblyRequest permissionSetReference="Custom" />
        <PermissionSet ID="Custom" SameSite="site" Unrestricted="true" />
      </applicationRequestMinimum>
    </security>
  </trustInfo>
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
    </application>
  </compatibility>
</assembly>

The scheduled task now fails with error 0x800702e4: the requested operation requires elevation.

We tried putting a shortcut in %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup, with no success.

Is there a way to run an application with the uiAccess flag set?

c#
windows
scheduled-tasks
asked on Stack Overflow Feb 15, 2016 by ale

1 Answer

2

The problem is that the Task Scheduler uses by default CreateProcess, which won't work for running processes with a manifest that requires elevation: you'd need to use ShellExecuteEx for that.

There are probably other ways to do this, but at least one of them, with the task scheduler, is configure the task to stop other existing instances if the task is already running.

Via the GUI: Settings -> "If task is already running (etc.)", set to "Stop the existing instance", or in the XML:

<MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
answered on Stack Overflow Feb 15, 2016 by Jcl

User contributions licensed under CC BY-SA 3.0