1

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.

ManxJason
  • 928
  • 12
  • 33
  • Maybe one of the top 3 links here can help ► [https://www.google.ie/webhp?hl=en&sa=X&ved=0ahUKEwiB8rv01sTSAhVCC8AKHeG_BukQPAgD#safe=active&hl=en&q=c%23+get+process+id&*](https://www.google.ie/webhp?hl=en&sa=X&ved=0ahUKEwiB8rv01sTSAhVCC8AKHeG_BukQPAgD#safe=active&hl=en&q=c%23+get+process+id&) or maybe this SO can help ► [http://stackoverflow.com/questions/3003975/how-to-get-the-current-processid](http://stackoverflow.com/questions/3003975/how-to-get-the-current-processid) – Nope Mar 07 '17 at 15:22
  • Process.Start from System.Diagnostics returns a Process object that has an id property. That is the process id I believe (https://msdn.microsoft.com/en-us/library/system.diagnostics.process(v=vs.110).aspx) –  Mar 07 '17 at 15:24
  • Not in general. This may, for example, start a launcher instead, which then goes on to run the actual game executable. You can work around that by tracing child process launches, but in general, you should wonder why you need this. Of course, there must be a way to detect the running game in this case because Steam has one, but that might involve hooking into the Steam API itself. – Jeroen Mostert Mar 07 '17 at 15:25
  • @JeroenMostert It doesn't involve the Steam API, since it also works for non-Steam games you launch through Steam, as well as Steam games with no integration. That said, there's plenty of things Steam does that might be important here - e.g. the Steam overlay injection or just keeping a process id of whatever Steam is launching. – Luaan Mar 07 '17 at 15:27
  • 1
    @Luaan: yes, but does it also work for non-Steam games that use launchers? This, I don't know. (The overlay is not directly involved, I know it works fine with the overlay disabled.) – Jeroen Mostert Mar 07 '17 at 15:28
  • and maybe this SO post can help ► [**getting PID of process started by Process.start()**](http://stackoverflow.com/questions/12892268/getting-pid-of-process-started-by-process-start) – Nope Mar 07 '17 at 15:29
  • @Fran You didn't read the question correctly.. I'm not looking to obtain the id of the process that's invoked from Process.Start. I'm looking to get the process Id of a process that's started via the original process (from Steam). – ManxJason Apr 25 '17 at 10:19
  • @ManxJason Hence I posted no answer but only links I thought **might** be of help :) – Nope Apr 25 '17 at 11:34

4 Answers4

3

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.

Soroush Falahati
  • 2,196
  • 1
  • 27
  • 38
  • Sadly this isn't always reliable. I ran into a particular game (ABZU) where once you load the game and get the splash screen the 'Running' reg key sets to 1, but then once the actual game starts it sets that back to 0. – Sean O'Neil Dec 04 '19 at 06:44
  • If it resets to 0 this means that Steam thinks that the game in question is no longer running. This should also get reflected in your user's status. If this is the case; the game developer should fix the problem. I wonder how the review team at Valve failed to notice that. Unless this isn't a real Steam game but a shortcut. – Soroush Falahati Dec 05 '19 at 18:56
  • It's a real Steam game not a non-steam shortcut. [ABZU](https://store.steampowered.com/app/384190/ABZU/). Oddly the option to quit the game from Steam is still available so it knows the game was still running, just the registry is wrong. As you say I'm sure it's a mistake made by developer and/or Valve. But the fact that I tested like 5 games and this happened on one of them is troubling. – Sean O'Neil Dec 06 '19 at 00:24
  • If Steam is aware that an app is running you should be able to check the `RunningAppID` value located at `HKCU\SOFTWARE\Valve\Steam` key to see what game Steam thinks is running at the moment. Maybe the Id of game changes after the splash screen for some reason. – Soroush Falahati Dec 06 '19 at 14:59
  • Good call. RunningAppID works as expected with the game in question. – Sean O'Neil Dec 07 '19 at 10:22
2

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
            }
        }
    }
}
John L
  • 21
  • 1
1

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.

Luaan
  • 62,244
  • 7
  • 97
  • 116
0

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).

Wolfgang Radl
  • 2,319
  • 2
  • 17
  • 22