C
C#2y ago
AngryAnt

❔ Gleaning process launch information from Process reference attained via GetProcessById?

Given a process ID I would like to determine all parameters necessary to relaunch that process (same parameters, environment, etc.). How could this be done? I am currently monitoring the process via Process.GetProcessById and waiting for its exit, after which I do some processing. At the end of that processing I would like to relaunch the previously monitored process.
18 Replies
phaseshift
phaseshift2y ago
Look at Process.StartInfo - it contains the launch args
Chiyoko_S
Chiyoko_S2y ago
Chiyoko_S
Chiyoko_S2y ago
mhhhhh no so the things I can think of would be the command line args / environment variables / elevation / working directory? I'm sure I'm missing something
AngryAnt
AngryAntOP2y ago
Yea that should pretty much cover it. I'm assuming it'll have to be some roundabout way given that StartInfo isn't just populated - I am just not sure which direction. I can attain the information with OS-specific external calls as well, but would definitely prefer a path through existing framework API.
Chiyoko_S
Chiyoko_S2y ago
I see someone using WMI to get the command line parameters
AngryAnt
AngryAntOP2y ago
WMI?
Chiyoko_S
Chiyoko_S2y ago
Stack Overflow
How to get Command Line info for a process in PowerShell or C#
e.g: if I run notepad.exe c:\autoexec.bat, How can I get c:\autoexec.bat in Get-Process notepad in PowerShell? Or how can I get c:\autoexec.bat in Process.GetProcessesByName("notepad"); in C#?
Chiyoko_S
Chiyoko_S2y ago
something like this C# has things to run powershell code and also things to retrieve the WMI stuffs
AngryAnt
AngryAntOP2y ago
Ah yes - that was the kind of OS-specific out-of-API workaround I had in mind.
Chiyoko_S
Chiyoko_S2y ago
https://superuser.com/a/1675035 also apparently it's not possible to get environment variables of other processes, at least easily
Super User
get a process's private environment variables in Windows using powe...
I have a process (testxx) running on my Database. Every time a user connects with the database the process will start separate session for. All processes include a private environment variable(Clie...
Chiyoko_S
Chiyoko_S2y ago
I guess security and stuffs
AngryAnt
AngryAntOP2y ago
Makes sense. I guess I could do a less-general-purpose relaunch by hardcoding those and just grabbing the parameters. But it doesn't look like there's an in-API way of getting those?
Chiyoko_S
Chiyoko_S2y ago
There's a component that used to be included in .NET Framework under System.Management, now it is available as a NuGet Package instead there should be examples of how to make a WMI query using it
AngryAnt
AngryAntOP2y ago
Thanks 🙂 That does look super interesting. Windows isn't really a priority as a platform, so I'll leave this as a note for later and conclude that a default API approach isn't available. Thank you walking through this with me 🙂
Chiyoko_S
Chiyoko_S2y ago
Something like that
AngryAnt
AngryAntOP2y ago
Very nice! Thanks 🙂
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server