dotnet / dotnet/dotnet-api-docs
More information on false return value of System.Diagnostics.Process.Start
- Dominant language
- C#
- Stars
- 949
- Forks
- 1.7k
- Avg merge
- 3d 27m
- Merged PRs (30d)
- 49
Description
Hi,
According to the documentations, [`System.Diagnostics.Process.Start`](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.start) can return `false` when a process is 'reused', but does not really give an indication when this happens. I'm currently implementing a feature where a process is started and I need to know if and how I should handle a `false` return
A relevant [thread on StackOverflow](https://stackoverflow.com/questions/33042010/in-what-cases-does-the-process-start-method-return-false) goes a bit deeper into the actual implementation, but it this thread cannot really give an example on when it will return false.
If I look at the [actual implementation on .NET myself](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs), it looks like `Start` calls `StartCore` after doing some sanity checks. `StartCore` is platform-dependent an behaves differently on whether or not ShellExecute is used.
If ShellExecute is used, eventually [`ShellExecuteXw`](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecuteexw) is [called](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Win32.cs#L156C56-L156C71) and [`true` is returned](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Win32.cs#L96) if `hProcess` is set on [`SHELLEXECUTEINFOW`](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-shellexecuteinfow). `hProcess` will be `NULL` when no process is launched when URLS/documents are opened and eg. the Web browser is already running. Could this be the 'reusing' of a process?
If ShellExecute is not used, the call eventually ends up at [either](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Windows.cs#L556) [`CreateProcessWithLogonW`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createprocesswithlogonw) when a custom user is used or [at](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Windows.cs#L556) [`CreateProcessA`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa) when this is not the case. Then `false` is returned if [an invalid process handle was returned by the underlying function](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Windows.cs#L650).
The case for ShellExecute is somewhat clear, but the case for non shell execute is not. At least not to me. Why would an invalid handle being returned by the kernel indicate that a process was reused?
Can some more information regarding this be provided? I've personally never heard of processes being 'reused' by the OS when starting a process. Maybe add an example for which the method will always return false?
Thanks in advance!
Contributor guide
Assessment
This issue has not been assessed yet.