Reputation: 940
Is there a way to obtain the process name/id of a Steam game started via:
Process.Start("steam://rungameid/#");
As expected the above method returns Steam.exe.
Upvotes: 1
Views: 4317
Reputation: 21
This is an old question but I just got done doing the same thing (for a project somewhat similar to what you're working on). You can use ManagementObjects from System.Management to find the ParentId of a process. If you are launching Steam, you will have the process Id of Steam, although the way you're currently launching games, you will need to look up the process Id of Steam.
One thing you will find is each game will generally spawn 2-3 different child processes. The steam overlay, the webclienthelper, and then the game itself (and possibly another for the game's launcher). They should all be children to the Steam process, though, and all should show up in the found processes list.
It would look something like this.
List<Process> procs = Process.GetProcesses();
Process steam = procs.Find(x => x.ProcessName == "Steam");
int steamId = steam.Id;
foreach (var proc in procs)
{
using (ManagementObject mo = new ManagementObject($"win32_process.handle='{proc.Id}'"))
{
if (mo != null)
{
try
{
mo.Get();
int parentPid = Convert.ToInt32(mo["ParentProcessId"]);
if (parentPid == steamId)
{
Console.WriteLine($"{proc.ProcessName} is running as a child to {mo["ParentProcessId"]}");
}
}
catch (Exception ex)
{
// the process ended between polling all of the procs and requesting the management object
}
}
}
}
Upvotes: 2
Reputation: 2337
Every game has a key in the registry under HKCU\SOFTWARE\Valve\Steam\Apps
You should be able to check for the Installed
value of your game from there, then run the game using steam://rungameid/#
, and then wait for the Running
or Updating
values to change to 1
, and again for 0
.
This makes it possible to track a game execution, however, you still can't directly have the process id of the game.
Upvotes: 3
Reputation: 2349
As the new process will not be started as a child process of steam, it will be impossible to derive its id. What you could try though is to derive the currently running game via the API - and then search through all running processes for their names. The API should expose the game's name via the gameextrainfo
property, which should be identical to the process' name (untested, though).
Upvotes: 0
Reputation: 63772
In general, no.
Steam simply uses a custom URI prefix, which allows you to specify which application will service that request. But it doesn't create a new prefix for each game - instead, it's handled by steam.exe, which decides what application to actually run.
Steam itself of course does track the games it runs, but I assume it simply keeps track of their process IDs, perhaps with some influence from either Steam integration or the Steam overlay injection to track games that use a launcher. I don't think there's any simple way of keeping that information if you're not willing to mess around with other application's privates.
There may be some cases where you have other solutions; e.g. if you don't mind if there may be multiple Steam games running at the same time, you could try finding all processes that have the Steam overlay, but those are rather specific - they might work well for you, but fail for other users.
Upvotes: 1