3

The short story is I'm writing a front-end for the emulator MAME as a study in WPF and C#.

The GUI is set, it's reading cfg's properly, everything is fine except for actually launching MAME.

From the command line (Windows 7) I can type the following and have the emulator launch properly.

c:\MAME\Emulator\mame.exe mslug.zip

That launches the emulator with no issue exactly as designed. However I have tried all of the following.

Process Mame = new Process(emulatorPath);
Mame.StartInfo.Arguments = romSelected;
Mame.Start();

I tried the above with both the variable and putting Mame.StartInfo.Arguments = "mslug.zip"

ProcessStartInfo Mame = new ProcessStartInfo(emulatorPath);
Mame.Arguments = romSelected;
Process.Start(Mame);

I tried this also with both the variable and putting "mslug.zip" in it's place.

Finally I tried the following.

Process.Start(@"c:\Mame\emulator\mame.exe", "mslug.zip");

And it acts the same as the previous attempts.

If I don't try passing arguments to it, the program launches fine and just tells me there was no rom. Any of the above methods of passing arguments all resulted in a quick command prompt blip showing the same info it would show if it the rom's zip file was empty.

From what I've read about Process.Start and the like, what I've typed above should equate to opening the command line and entering the command I started this post with. But if that were the case then this should have worked with no issue. I'm not sure if I'm doing something wrong or if there is a better way to go about this.

Note: I also went through the Windows GUI and created a shortcut to mame.exe and edited it's properties to pass mslug.zip as an argument and it worked as well, so it doesn't require it to be done through the command line as far as I can tell.

As an asside, I have debug textboxes in the gui of the app that are updated with the variables used in my code to verify that the variables are correct.

Update:
I wanted to add that the program (for those not familiar) relies on the filename of the rom you try to launch. Meaning passing the argument mslug.zip causes the program to goto it's own rom directory (currently C:\mame\emulator\roms) and search for mslug.zip. I can run that command from any directory in my system and get the same result. I can also pass the path to the rom like

c:\mame\emulator\mame.exe c:\mame\emulator\roms\mslug.zip

That will also work regardless of where I run it. And I have tried that within my code, both by passing the paths as variables and by passing them like

string romSelected = @"c:\mame\emulator\roms\mslug.zip";

Both fail in the same fashion.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Aloehart
  • 337
  • 1
  • 4
  • 14
  • 2
    What about **working directory**? From command line you're in the directory where `mslug.zip` is, from your application you're in its working directory (`bin/debug` directory, for example). – Adriano Repetti Feb 05 '15 at 15:35
  • have you tried a google search there are plenty of examples on how to do this properly on line as well as `SO` http://stackoverflow.com/questions/15061854/how-to-pass-multiples-arguments-in-processstartinfo I wonder if you've stepped thru the code and evaluated all params and variables etc.. – MethodMan Feb 05 '15 at 15:35
  • @MethodMan: The question you are pointing to is about multiple parameters. Here there is just one. The code from OP is okay. – Patrick Hofman Feb 05 '15 at 15:37
  • same principal applies in theory @PatrickHofman – MethodMan Feb 05 '15 at 15:38
  • @MethodMan you suggest there is something wrong with the code. There isn't. – Patrick Hofman Feb 05 '15 at 15:39
  • Put the file you're trying to load (the zip) in the current working directory, or set the path to the file, and it will load properly. Your code is not the issue from the looks of it, it should work fine. – Scott Powell Feb 05 '15 at 15:41
  • if there is something incorrect with the Path, then there would be something wrong with the code in regards to interpreting the `Path` anyway I'm not going to worry about this trivial pursuit – MethodMan Feb 05 '15 at 15:41
  • @AdrianoRepetti Actually from the command line I'm not in the directory for mslug.zip. I should have mentioned this originally and have edited the post accordingly. MAME looks for specific filenames and checks it's own directories for the file. I can run the same command from any directory on my system and get it to run with no issue. But I have tried passing the full path to the rom as an argument from within C# and still had the issue while also not having the issue when doing the same for the command line. – Aloehart Feb 05 '15 at 15:48

3 Answers3

2

The code you use works fine. It does send the right argument to the program. No doubt.

Some things that could go wrong:

  • The file doesn't exist;
  • The file doesn't exist in the current working directory, aka the place where mame is looking for it (you can try using an non-existing file name in the command line as a test. Is the error the same, then probably the file can't be found. Try to use the full path of the file);
  • Some permission problems, the error is obscured by the program.

As Sinatr suggested, you could set the current working directory using Directory.SetCurrentDirectory. Be aware that the current working directory of this program is influenced too, so you might want to consider what you are doing.

You'd better set the working directory of the process you start, using Process.WorkingDirectory.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • That's what I thought, but the emulator doesn't actually require a path. It is designed to take the file name and check it's roms folder. I can run the command from any directory and get the same result from comand line. – Aloehart Feb 05 '15 at 15:41
  • 2
    What about emulator itself working directory? Maybe it can't find `rom`, because it starts searching from wrong base directory due to how you start it. Try to [set it](https://msdn.microsoft.com/en-us/library/system.io.directory.setcurrentdirectory(v=vs.110).aspx). – Sinatr Feb 05 '15 at 15:45
  • @Sinatr If you would, go ahead and add that to your original answer as that fixed the issue. Seems odd it would have that issue but I'm glad to have learned something new. Thanks so much for the help :) – Aloehart Feb 05 '15 at 15:53
  • 1
    @PatrickHofman thanks for updating and your suggestion. I re-wrote it to set Process.WorkingDirectory as you suggested since the front-end does have external cfg's it needs to work with. Thanks again for the help! – Aloehart Feb 05 '15 at 16:17
1

Only assigning StartInfo.Arguments with the filename does not work. You must prepend an action like -File. This will generate 2 arguments for the receiving App (args[0]="-File", args[1]=filename).

Here's what worked for me:

ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
{
    FileName = appFilename,
    Arguments = "-File " + viewFilename, // space char after -File
};
Process.Start(startInfo);
Matt
  • 169
  • 2
  • 6
0

Now I finally got this to work! The magic lies in the pre string to the args.

example:

/C Run Command and then terminate
/K Run Command and then return to the CMD prompt.

described here: http://ss64.com/nt/cmd.html

The snippet below executes the DIR command using CMD.exe

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
// /K Run Command and then return to the CMD prompt.
startInfo.Arguments = "/K dir";
process.StartInfo = startInfo;
process.Start();
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Daniel Nelson
  • 1,968
  • 1
  • 12
  • 11