New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
StartInfo of Process throws with Process was not started by this object, so requested information cannot be determined. #33525
Comments
If you are trying to get the environment variables for your current process, you can use If you are trying to do this for a different process other than the current one, I don't believe there is a way to do that. See #14329 for more background. |
I think I shouldn't say environment varaibles, but rather it's process related variables. How do I associated those to a process? |
What information are you trying to get from the
So if you are trying to do this with a If you are doing this for a process your own code did start using |
Hi @soyang2828, would you mind sharing reproduction steps, sdk version and operating system? I couldn't reproduce the exception in a couple of simple examples that I tried. My understanding of |
@eiriktsarpalis you can reproduce it like so: using System.Diagnostics;
public class Program {
static void Main() {
foreach(var p in Process.GetProcesses()) {
_ = p.StartInfo;
}
}
} In .NET Framework, this does not throw an exception (although it may not work as some people expect it to). In .NET Core 3.1, this throws an exception. |
Based on what I'm reading, accessing environment variables of other processes is not supported by the windows api. TBH I'd be surprised if the opposite were the case. In that sense I'd probably treat the current behaviour as by-design, though happy to be corrected on this. |
Thanks @vcsjones for the repro. Here is a usage code snippet. What do you suggest we do here?
|
@soyang2828 that code is not doing anything useful. It is simply reading the defaults off of a new ProcessStartInfo. If you create a You can see the details here: https://referencesource.microsoft.com/#System/services/monitoring/system/diagnosticts/ProcessStartInfo.cs,173 To answer your specific question, you can replace that code with // Copy current process properties to worker process
using (Process currentProcess = Process.GetCurrentProcess())
{
... workerProcessInfo.FileName = Path.Combine(Path.GetDirectoryName(currentProcess.MainModule.FileName), WorkerProcess.ExecutableName);
workerProcessInfo.CreateNoWindow = false;
workerProcessInfo.ErrorDialog = false;
workerProcessInfo.ErrorDialogParentHandle = null; However note that either way, the code is not doing what the comment claims it is doing. I'm closing this as by-design, but marking 'breaking change' so it can get into docs. |
Actually it's already documented in Process.StartInfo as a breaking change, and I opened dotnet/docs#18310 as well. |
If this is not supported, what is the recommended way to pass around the environment variables of a process?
Here is our callstack:
System.InvalidOperationException
HResult=0x80131509
Message=Process was not started by this object, so requested information cannot be determined.
Source=System.Diagnostics.Process
StackTrace:
at System.Diagnostics.Process.get_StartInfo()
at Microsoft.Office.Audit.Schema.RecordSerializer..cctor() in H:\subrepo2\src\sources\dev\common\src\ComplianceAuditSchema\RecordSerializer.cs:line 112
The text was updated successfully, but these errors were encountered: