Many emulators don't just have the name of the game after the exe in the command line. But if you do want the command line use this code
SelectQuery selectQuery = new SelectQuery(String.Format("select * from Win32_Process where ProcessId={0}", pid));
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery))
{
foreach (ManagementObject process in searcher.Get())
return (string)process.Properties["CommandLine"].Value;
}
To get the ProcessId you will need the hWnd (handle to window). To get the hWnd use
FindWindow to search the title or class (which you can get using a tool like Spy++), and then get the ProcessId from the hWnd using
GetWindowThreadProcessId. You can also get the hWnd from the exe by enumerating the windows. You do that using
EnumWindows.
To do it via command line I just mean in Program.cs you have "static void Main(string[] args)" which you use to read in arguments sent in before launching the emulator. Eg. MyApp.exe "[GAMENAME]". Since I don't know what the purpose of your app is I can only assume this is for use on an arcade machine so you can have the FE software sent your app the name of game and that way you don't need to extract it from the command line.
Here is a method that will check if it's the first instance of the app running.
public bool IsFirstInstance()
{
string m_UniqueIdentifier;
string assemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName(false).CodeBase;
m_UniqueIdentifier = assemblyName.Replace("\\", "_").ToLower();
m_mutex = new Mutex(false, m_UniqueIdentifier);
if (m_mutex.WaitOne(1, true)) // FirstInstance
{
return true;
}
else
{
m_mutex.Close();
m_mutex = null;
return false;
}
return true;
}
If IsFirstInstance() returns false then there is already an instance running. So in Program.cs before you do anything you need to pass on the "string[] args" to the already running instance for processing. You have to do that via
IPC (Inter Process Communication). There are several ways to do this but the simplest would be to pass on the command line using
PostMessage and WM_COPYDATA. You can find an example of that
here.