runtime: Process.ProcessName throws for services.exe when accessed from a non-admin user

Process.ProcessName throws when the process is an admin process (like “services.exe”) and the current app is running as a low privilege user. This is a regression from .NET 6, and it breaks Microsoft.Extensions.Hosting.WindowsServices when running as a non-admin user.

This could possibly be a regression from #59672.

Repro Steps

  1. Build a console app as below:
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>
using System.Diagnostics;

namespace ConsoleApp1
{
    internal class Program
    {
        const int ServicesProcessId = 756;

        static void Main(string[] args)
        {
            var services = Process.GetProcessById(ServicesProcessId);
            Console.WriteLine(services.ProcessName);
        }
    }
}
  1. Look in Task Manager for the services.exe process and replace the ServiceProcessId with the process ID for your current machine.

  2. Run the app as you, it prints

services
  1. Run the app as a non-admin, low privilege user
Unhandled exception. System.InvalidOperationException: Process has exited, so the requested information is not available.
   at System.Diagnostics.Process.get_ProcessName()
   at ConsoleApp1.Program.Main(String[] args) in C:\Users\eerhardt\source\repos\WindowsService1\ConsoleApp1\Program.cs:line 12
  1. Change the TargetFramework to net6.0, and run as the same non-admin, low privilege user:
services

Original Report

I come back to the issue #67093, which is closed. The error still occurs with preview.4 and todays nightly build, but only on Windows Server 2016, not on my local Windows 11 developer machine. The error is:

Application: CloudManagementTool.WorkerService.exe
CoreCLR Version: 7.0.22.27203
.NET Version: 7.0.0-preview.5.22272.3
Description: The process was terminated due to an unhandled exception.
Exception Info: System.TypeInitializationException: The type initializer for 'CloudManagementTool.WorkerService.WorkerService' threw an exception.
 ---> System.InvalidOperationException: Process has exited, so the requested information is not available.
   at System.Diagnostics.Process.get_ProcessName()
   at Microsoft.Extensions.Hosting.WindowsServices.WindowsServiceHelpers.IsWindowsService()
   at ConsoleApp1.Program.<Main>(String[] args) in D:\Test\Program.cs:line 13

A reproducing sample program is quite short, but it only crashes on Windows Server 2016 and when registered and executed as a service. Starting the same code directly from the console works fine.

public class Program
{
	public static async Task Main(string[] args)
	{
		var isService = WindowsServiceHelpers.IsWindowsService();
		if (isService)
		{
			// ...

This issue becomes critical now, because it will make the 7.0 version unsable for windows services on Windows Server 2016.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (17 by maintainers)

Commits related to this issue

Most upvoted comments

I’ve created a PR with my suggestion for a solution. It will try the new way, and fallback to the old implementation. Please take a look at #70073.

Closed by #70073

Oh this is terrible, I am sorry. The new direct/efficient way, publically documented, requires certain documented access. https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-queryfullprocessimagenamew The old way doesn’t require as much access. Is it acceptable to try the new way, and fallback upon access denied? Or might as well just have one slower but uniform way? Win32 is unfortunately not always what we’d like. 😦

@jaykrell, @eerhardt , @SteveDunn - I just tested, the exception also occurs on Windows 11 and my local developer machine - as long as I use a dedicated local non-admin user. The information about Windows Server maybe came into this because it took some time for me to figure out the local user thing. So it should be possible to debug this on a normal developer machine.

Application: ConsoleApp3.exe
CoreCLR Version: 7.0.22.22904
.NET Version: 7.0.0-preview.4.22229.4
Description: The process was terminated due to an unhandled exception.
Exception Info: System.InvalidOperationException: Process has exited, so the requested information is not available.
   at System.Diagnostics.Process.get_ProcessName()
   at Microsoft.Extensions.Hosting.WindowsServices.WindowsServiceHelpers.IsWindowsService()
   at Program.<Main>$(String[] args) in C:\Users\CSchuette\source\repos\ConsoleApp3\Program.cs:line 6
   at Program.<Main>(String[] args)
Name der fehlerhaften Anwendung: ConsoleApp3.exe, Version: 1.0.0.0, Zeitstempel: 0x626c4e6c
Name des fehlerhaften Moduls: KERNELBASE.dll, Version: 10.0.22000.708, Zeitstempel: 0x89f799f7
Ausnahmecode: 0xe0434352
Fehleroffset: 0x000000000004474c
ID des fehlerhaften Prozesses: 0x7c3c
Startzeit der fehlerhaften Anwendung: 0x01d8757b623d30eb
Pfad der fehlerhaften Anwendung: C:\Users\CSchuette\source\repos\ConsoleApp3\bin\Release\net7.0\publish\win-x64\ConsoleApp3.exe
Pfad des fehlerhaften Moduls: C:\WINDOWS\System32\KERNELBASE.dll
Berichtskennung: a124e10b-23dd-48b6-9996-0de115e40030
Vollständiger Name des fehlerhaften Pakets: 
Anwendungs-ID, die relativ zum fehlerhaften Paket ist: