Main Restorations Software Audio/Jukebox/MP3 Everything Else Buy/Sell/Trade
Project Announcements Monitor/Video GroovyMAME Merit/JVL Touchscreen Meet Up Retail Vendors
Driving & Racing Woodworking Software Support Forums Consoles Project Arcade Reviews
Automated Projects Artwork Frontend Support Forums Pinball Forum Discussion Old Boards
Raspberry Pi & Dev Board controls.dat Linux Miscellaneous Arcade Wiki Discussion Old Archives
Lightguns Arcade1Up Try the site in https mode Site News

Unread posts | New Replies | Recent posts | Rules | Chatroom | Wiki | File Repository | RSS | Submit news

  

Author Topic: looking for directions with some C# programming  (Read 1201 times)

0 Members and 1 Guest are viewing this topic.

ideft

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 131
  • Last login:February 11, 2012, 04:06:09 pm
  • "Mister, I suspect you to be a dope fiend"
looking for directions with some C# programming
« on: January 28, 2009, 10:58:06 pm »
It has been awhile since I programmed anything and was looking to learn a bit about C#.  So I wanted to make a program run the the background and once a hotkey is pressed then it will execute.  So I think I have the hot key figured out using a keyboard hook.  But I also want to know the emulator being run(easy enough by searching the process tree).  Now how to get the name of the game being run????  Is there a way to intercept or hook the command line that is being sent from the frontend to the emulator??  I was thinking of using SetWindowsHookex with the hook type of WH_CALLWNDPROC, but I am not to sure if this would intercept the command line.  Any help would be great or suggestions or a point in the right direction(as I have no direction to follow).   

headkaze

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2943
  • Last login:August 14, 2023, 02:00:48 am
  • 0x2b|~0x2b?
Re: looking for directions with some C# programming
« Reply #1 on: January 29, 2009, 07:04:31 am »
Getting the command line from a running app is not that difficult. The problem is once you have it, then how do you know what format it's supposed to be in so you can grab the game name? I faced this same dillema with CPWizard.

The way I solved it was to have a command line option to send the name of the game before the emulator is launched.

You will have to make your app run as a single instance by using a mutex to check if it's already running and send the command line over to the already running process using some sort of IPC method (Eg. There are several methods for IPC in Windows - read this) and then exit the second instance.

« Last Edit: January 30, 2009, 12:38:59 am by headkaze »

ideft

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 131
  • Last login:February 11, 2012, 04:06:09 pm
  • "Mister, I suspect you to be a dope fiend"
Re: looking for directions with some C# programming
« Reply #2 on: January 29, 2009, 08:46:20 pm »

The way I solved it was to have a command line option to send the name of the game before the emulator is launched.


not sure of how to do that or even if I understand it... your post just blew my mind.
the more I think about this the more confusing it seems. so did you intercept the command line and modify it??  and how did you tell it to return just the game name??  would this be feasible on more than one emulator??




So.. I use a mutex to check if my app is running, if it is not then run the code, correct??

With ipc I would just use string arg[] and that would fill arg[1], arg[2] with the paramaters being passed?

would all command lines get passed through this??

My thought on this if tI could get the command line in a string then I could search the string and pull the game name from there.  Since most of the time the game name comes after the .exe, I could just look for the .exe and then put the next characters into a new string until I hit a space.  Sounded simple enough to me.

Sorry for all the questions!!!


headkaze

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2943
  • Last login:August 14, 2023, 02:00:48 am
  • 0x2b|~0x2b?
Re: looking for directions with some C# programming
« Reply #3 on: January 30, 2009, 12:58:11 am »
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

Code: [Select]
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.

Code: [Select]
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.