How to launch a full-trust process as elevated from UWP app? - c#

I am trying to launch a full-trust process program from my UWP app. I am using the PackageManager in my full-trust process to locate UWP packages to launch files or links, so I need to run it as an admin.
When I try running the full-trust process, it opens and works properly on my machine (I am running an admin account). However, when I look up the process in the Task Manager, it does not run elevated.
I'm just worried that it may not work on any other machine (non-admin) out there. Here's my code I'm using in Package.appxmanifest, courtesy of Stefan Wick (https://stefanwick.com/2018/10/07/app-elevation-samples-part-3/):
<desktop:Extension Category="windows.fullTrustProcess" Executable="Assets\NitishTest.exe">
<desktop:FullTrustProcess>
<desktop:ParameterGroup GroupId="Parameters" Parameters="parameters" />
</desktop:FullTrustProcess>
</desktop:Extension>
<uap3:Extension Category="windows.appExecutionAlias"
Executable="Assets\NitishTest.exe"
EntryPoint="Windows.FullTrustApplication">
<uap3:AppExecutionAlias>
<desktop:ExecutionAlias Alias="NitishTest.exe" />
</uap3:AppExecutionAlias>
</uap3:Extension>
I am not using a Windows Application Packaging Project, however. Could this be the reason why it's not working properly elevated?

Related

UWP app fails to start on login using the StartupTask APIs

NOTE: I've already looked at this related question, this is not a duplicate
I'm working on a UWP app which also uses the Desktop Bridge (the app package contains the UWP app and a WinForms component), and I'm trying to add the auto startup feature, so far without success.
Here's what I did:
Some research. Looked at this question mentioned before, this blog post from MS and at the official docs as well.
Followed the docs by editing the Package.appxmanifest file to add the uap5 namespace, and then the uap5:Extension node as instructed, setting my app .exe file in the Executable property, and Windows.FullTrustApplication in the EntryPoint property.
Bonus: just in case, I also tried to replace the uap5 namespace with desktop, as some code samples used that one instead. Same result, the app doesn't start at all.
Included the APIs to get the startup task and request it to be setup.
Deployed the app, proceeded to use those APIs, got the confirm window and tapped "allow".
Opened the Task Managed and double checked that the app name was there under the "Startup" tab, with the "Enabled" label correctly shown next to it.
Logged out and back in
At this point nothing happens, except from the mouse pointer showing the loading ring for half a second after logging in. Opened the Windows event viewer and found an error, which was indicating the failed auto startup for the app. Tried again a few times and sure enough, every time the app didn't start and another identical error popped up in the event viewer. This is the error info:
Application name: <my app>.exe, versione: 1.0.0.0, timestamp: 0x5a68410c
Module: KERNELBASE.dll, versione: 10.0.17134.407, timestamp: 0x99042cc0
Exception code: 0xe0434352
Offset: 0x000000000003a388
Process ID: 0x1c4c
Path: C:\Users\<my username>\Documents\GitHub\<my app>\<my app>.Package\bin\x64\Debug\AppX\<my app>.exe
Module path: C:\Windows\System32\KERNELBASE.dll
[...]
I'm not sure what I'm doing wrong here, I've followed the docs step by step and I do see the app listed in the Task Manager, but it just fails to start this way.
Any help would be appreciated, thanks in advance! 😄
EDIT: it seems the problem is related to the Desktop Bridge functionality. I have a UWP app and a packaging project, and I've added the startup task to both the .appxmanifest files (with different Ids). I do this as I use the packaging project to create the x86/x64 builds, and the UWP project directly for the ARM/ARM64 builds.
If I only deploy the UWP app (standalone, without the package) and enable the startup task, the app runs fine. But, if I deploy the packaging project and enable that startup task, the startup fails. I do see the startup task in the Task Manager in both cases. As mentioned earlier, when the startup task fails for the packaging project, I see those errors in the event viewer.
EDIT #2: did some more tests with the Desktop Bridge app. It seems that after it fails to start, the Windows event logger gets two more errors listed there for each attempt. One is the error in the "Application Error" category mentioned earlier, and another one is an error in the "AppModel-Runtime" category, with the following info:
Failed with 0x490 modifying AppModel Runtime status for package for user (current status = 0x0, desired status = 0x20).
EDIT #3: as requested, here's the .appxmanifest file for the packaging project. Note that as mentioned earlier, I tried adding the startup task both using the desktop namespace as well as the newer uap5 namespace. Again, in both cases I can retrieve the task and prompt the user to enable it, and I do see it listed in the Task Manager, but the application still fails to start with the usual error.
When setting up a StartupTask extension in your appxmanifest it is important to understand that the declaration is different for Win32 components vs UWP components. This is documented here:
https://learn.microsoft.com/en-us/uwp/api/Windows.ApplicationModel.StartupTask
UWP Components:
<Package xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5" ...>
...
<Applications>
<Application ...>
...
<Extensions>
<uap5:Extension Category="windows.startupTask">
<uap5:StartupTask
TaskId="MyStartupId"
Enabled="false"
DisplayName="Test startup" />
</uap5:Extension>
</Extensions>
</Application>
</Applications>
Win32 Components:
<Package xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5"...>
...
<Applications>
<Application ...>
...
<Extensions>
<uap5:Extension
Category="windows.startupTask"
Executable="MyDesktopBridgeApp.exe"
EntryPoint="Windows.FullTrustApplication">
<uap5:StartupTask
TaskId="MyStartupId"
Enabled="false"
DisplayName="My Desktop Bridge App" />
</uap5:Extension>
</Extensions>
</Application>
</Applications>

Msi setup showing File in use message on uninstallation

I have developed an msi setup using WIX which consists of a desktop application as well as a windows service and both are running on C# .Net 3.5 framework. My windows service starts only when any user logs into the system which triggers the desktop application to start. The windows service is made to run as Local System. The msi setup is getting installed successfully at Win-8, Win-7 and Windows-XP but showing "File In Use" message while doing uninstallation even the service is not removed from the SCM. I have given the below codes at the OnStop() method of the service and inside the WIX page respectively.
onStop() method:
Process[] workers = Process.GetProcessesByName("filename");
workers[0].WaitForExit(1000);
workers[0].Kill();
workers[0].Dispose();
workers[0].Close();
Product.wxs inside WIX:
<ServiceInstall Id="ServiceInstaller" Name="Servicename"
DisplayName="service display name" Description="service description"
Start="auto" Account="LocalSystem" ErrorControl="normal"
Type="ownProcess"></ServiceInstall>
<ServiceControl Id="ServiceInstallerControl" Name="Servicename"
Start="install" Stop="both" Remove="uninstall" Wait="yes" />
The service is not removed from the SCM at all and the below popup message is displaying at the time of uninstallation process.
I have worked around with the WIX to solve the problem but unable to do so.
Any kind of help in this regard will be highly appreciated.
I'd recommend you to create custom action that handles the uninstall.You can create a batch file with the uninstall details , and then simply execute the batch file from the custom action( as a process).Its good practice to create installation and uninstall batch files with your service.

Trying to attach to a process to debug a service

I'm trying to connect to a C# service. I can view the service in Windows Task Manager, but it does not appear in the "Available processes" List. I've tried running VS2012 as my normal user, as administrator, with "Show processes from all users" and Managed (v4.5, v4.0) code instead of the default. The service is built in Debug mode and then installed using an installer(Wix). On every demonstration of this, I've seen "Show Processes In All Sessions" but this is not shown in my Attach to process dialogue.
I managed to connect from a command line using "vsjitdebugger.exe -p 7012" but this wants to debug in a new instance, and should I use that new instance, I can't see any of the code.
Any advice would be really appreciated.

windows mobile 6.1 autostart application after clean boot

I have an c# application that I am installing during cleanboot on a windows mobile 6.1 device. It installs correctly, however, I need to get it to autostart after cleanboot.
I always have to warmboot the handheld after cleanboot to make it start.
How can I avoid this?
I asume, you are using a CAB file to install your C# app during cleanboot.
You may use a setup.dll inside the cab to start your app after the cab files have been installed using the DLL's Install_Exit function call (setupdll sample for example here: http://www.codeproject.com/Articles/7724/Creating-Pocket-PC-Application-Setup-Packages-Usin). But MS states you cannot rely on calling files of the cab install itself.
Another approach would be a seond cab that is installed after your C# app install and just calls your app, even using a setup dll.
See also http://msdn.microsoft.com/en-us/library/bb158796.aspx for howto pack several cabs into one install.
The problem with cleanboot installers and AutoStart is, that the OS already has run \Windows\StartUp and also other autostarts when the cab has been installed. Therefor you normally need a reboot that then executes your app using StartUp (or HKLM\Init).
You may also use a scripting engine to install the cab and then launch your installed app. For example use MortScript, which supports being used as "AutoStart.exe" inside SD Card\2577 or similar dir (Volume name\2577). The the script can call wceload.exe to install your app and afterwards start the executable of the installed app. Something like using a batch.
As this is an Intermec install, you can use \Flash File Store\UserAutoInstall_sstransferagent.xml to automate the install and launch of the app.
For example: Your cab is at \Flash File Store\MyInstall\MyCab.cab and installs MyApp.exe to \Program Files\MyApp:
<?xml version="1.0"?>
<Devices>
<Device Type="" Family="" Model="" Boot="">
<Files SrcDir="\Flash File Store\UserAutoInstall">
<File SrcName=""
DestName="wceload.exe"
DestDir="\Windows"
Run="true"
CmdLine='"\Flash File Store\MyInstall\MyCab.cab" /silent /verifyconfig /nodelete'
/>
<File SrcName=""
DestName="MyApp.exe"
DestDir="\Program Files\MyApp\MyApp.exe"
Run="NoWait"
CmdLine=''
/>
</Files>
</Device>
</Devices>
regards

Installing C# Windows Service on Windows 7

I have a batch file that I have been using to install my C# Windows Services for awhile now, never had a problem until Windows 7. I have attempted to run the batch file with Administrator privileges. I have attempted to run the command prompt with admin privs, navigate to the windows service EXE and run InstallUtil there. Still doesn't work.
After reading some other suggestions I tried moving my files out of the /bin folder and running them from another location but that also didn't work.
The batch file looks like this
#ECHO OFF
REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%
echo Installing IEPPAMS Win Service...
echo ---------------------------------------------------
InstallUtil /i IEPPAMS_WinService1.exe
echo ---------------------------------------------------
echo Done.
and I have a install log file that I dump info to. If I just double click the .bat file I get
Running a transacted installation.
Beginning the Install phase of the
installation. See the contents of the
log file for the
C:\Users\Justin\Desktop\service
test\IEPPAMS_WinService1.exe
assembly's progress. The file is
located at
C:\Users\Justin\Desktop\service
test\IEPPAMS_WinService1.InstallLog.
An exception occurred during the
Install phase.
System.InvalidOperationException:
Cannot open Service Control Manager on
computer '.'. This operation might
require other privileges. The inner
exception
System.ComponentModel.Win32Exception
was thrown with the following error
message: Access is denied.
The Rollback phase of the installation
is beginning. See the contents of the
log file for the
C:\Users\Justin\Desktop\service
test\IEPPAMS_WinService1.exe
assembly's progress. The file is
located at
C:\Users\Justin\Desktop\service
test\IEPPAMS_WinService1.InstallLog.
The Rollback phase completed
successfully.
The transacted install has completed.
When I run the .bat file with admin privileges nothing is written to the log file, and the service is still not installed.
Any thoughts? Is there a new way to install services in Windows 7?
Right click on the batch file and run it as Administrator.
You are most likely running into battle with the new security model (User Account Control) from Windows Vista and Windows 7. Even if you are running as an account that has Admin rights you will still need to elevate to do some (most) administrative activities. (Yes it is possible to disable this feature, but don't)
UAC (MSDN)
UAC (Wikipedia)
InstallUtil (MSDN)
Edit... The correct commandline is InstallUtil YourApp.exe. The /i does not look to be a vaild switch for InstallUtil.
So I was able to fix the problem by typing in the command line the entire path to InstallUtil and it worked. So after navigating to the folder that had my EXE I typed the following:
C:\Windows\Microsoft.NET\Framework\v4.0.21006\installutil.exe
IEPPAMS_WinService1.exe
Not sure why I have to do that in Windows 7 now when I never had to in XP, but oh well. Thanks for all the suggestions!
When I run the .bat file with admin privileges nothing is written to the log file, and the service is still not installed.
First off, you HAVE to run as admin permissions.
Second, when you "Run as Administrator", it actually changes the directory to c:\windows\system32 as the initial directory ( no idea why ), which would probably explain why running as admin causes no log file. Manually change to the path IEPPAMS_WinService1.exe resides in that the start of your script.

Categories

Resources