The NEW Build Your Own Arcade Controls

Main => Software Forum => Topic started by: Dave Dribin on May 27, 2002, 07:54:08 pm

Title: Start win programs: start /wait, CreateProcess()
Post by: Dave Dribin on May 27, 2002, 07:54:08 pm
Hello,

This is mainly a question for other front-end coders, but Windows knowledgable people may be interested, too.  When Game Launcher launches a game, it creates a batch file that basically looks like this:

cd \home\to\emu
emu.exe \path\to\rom.xxx
cd \glaunch\home

The problem I'm finding is that many Windows based emus will run and the return to the MS-DOS prompt right away.  This causes GL to start running again while the emu is also running, basically having both run in parallel.... not good.  I've found that if I modify the batch file to do this, it works:

cd \home\to\emu
start /wait emu.exe \path\to\rom.xxx
cd \glaunch\home

Now it would seem obvious to just stick "start /wait" in front of *all* emus.  The problem I've found is that this doesn't with DOS-based emus like Nesticle.  It completely confuses Win98 to the point that I have to reboot.  Does anyone know why "start /wait" would not work with DOS-based programs?

How are other front-ends launching programs?  Do you guys use batch files with "start /wait" or do you call CreateProcess() directly?  I've tried using CreateProcess() plus a WaitSingleObject(), and that works with most Windows-based emus I've found.  However, I can't get it to work with DOS-based emus.

TIA,

-Dave
Title: Re: Start win programs: start /wait, CreateProcess
Post by: richattri on May 28, 2002, 10:12:32 am
David,

Just brainstorming here. Maybe you could split your call into three phases:

setup target dir
spawn
restore target dir

You could use the Win32 function GetCurrentDirectory to save off the GL directory, then use SetCurrentDirectory just before spawning to set the default path. Then, instead of using CreateProcess, use the standard routines in process.h:

int spawnv(int mode, const char *cmd, char *const *argv)

You can use mode _P_WAIT to wait synchronously for the child process to terminate, or use _P_NOWAIT which will return the process identifier. You can then use the _cwait() at a later time to wait for that process to die.

Would that work? Or, is there a specific reason you actually want GL to exit while the emulator is running? If this is the case, maybe an intermediate daemon task could be created which would actually be responsible for spawning, waiting, and respawning GL.

Rich
Title: Re: Start win programs: start /wait, CreateProcess
Post by: Howard_Casto on May 28, 2002, 08:54:18 pm
I call the process, do a infinate wait until process is killed, and then clean up anything i need to.  Batch files are NOT the way to go in windows. Accessing win api's directly is much more powerful.  If you need any sample code let me know.  
Title: Re: Start win programs: start /wait, CreateProcess
Post by: Dave Dribin on May 29, 2002, 10:15:33 am
Quote
David,

Just brainstorming here. Maybe you could split your call into three phases:

setup target dir
spawn
restore target dir

You could use the Win32 function GetCurrentDirectory to save off the GL directory, then use SetCurrentDirectory just before spawning to set the default path. Then, instead of using CreateProcess, use the standard routines in process.h:

That's basically what I tried, except I used CreateProcess() followed by a WaitForSingleObject().  What's the difference between spawn() and CreateProcess()?

Quote
Would that work? Or, is there a specific reason you actually want GL to exit while the emulator is running? If this is the case, maybe an intermediate daemon task could be created which would actually be responsible for spawning, waiting, and respawning GL.

It turns out it's easier to have GL exit while the emulator is running from a portability point of view.  Plus, this gives GL the chance to release any resources that an emulator may need.  For example, I'm not sure if having 2 full-screen DirectX apps running at the same time works well.  I just decided to avoid the issue.

GL is separated into two separate processes, glaunch.exe and _gl32.exe, working just how you mention.  glaunch.exe is the daemon process.  It is very small and always runs.  _gl32.exe is a DirectX app that does the whole menu thing.  When you select a game, _gl32.exe creates a batch file and exits.  glaunch.exe runs the batch file, waiting for the emulator to finish, and then calls _gl32.exe, again.

-Dave
Title: Re: Start win programs: start /wait, CreateProcess
Post by: Dave Dribin on May 29, 2002, 10:28:27 am
Quote
I call the process, do a infinate wait until process is killed, and then clean up anything i need to.  Batch files are NOT the way to go in windows. Accessing win api's directly is much more powerful.  If you need any sample code let me know.  

I've tried the Windows API and that doesn't work with DOS applications, like Nesticle.  Oh, it launches the program just fine.  But somehow, it gets sent to the background, and the user has to Alt-Tab to go to the Nesticle window.  There must be some way to bring that process to the foreground (or put my process in the background), because it works fine from a batch file.

Plus CreateProcess() does not work with batch files whithout pre-pending the command with "command.com /c", AFAIK.  This means someone cannot use a batch file as an emulator or as a pre-/post-command.  This seems unnecessarily limiting for the user.  Batch files can be very useful for pre-/post- commands, and I know some users have batch files as wrappers around some emulators, too.

Yeah, I'll take sample code, anyways.  Seeing a different way to do this would be very helpful.  You could send it to dave-ml@dribin.org if you'd rather take this offline.

Thanks,

-Dave

Title: Re: Start win programs: start /wait, CreateProcess
Post by: )p( on May 30, 2002, 02:53:32 pm
Quote
Hello,

This is mainly a question for other front-end coders, but Windows knowledgable people may be interested, too.
Title: Re: Start win programs: start /wait, CreateProcess
Post by: Dave Dribin on May 30, 2002, 07:00:41 pm
Quote
Before you find a real solution you could in the meantime just check the exe if it is win32 and then make a batchfile with the /wait option...it is what I did a while back when I also did use a batch file to quit the fe while running a game...

How can you detect a Win32 exe vs. a DOS exe?

-Dave
Title: Re: Start win programs: start /wait, CreateProcess
Post by: Dave Dribin on May 31, 2002, 07:41:24 am
Quote
How can you detect a Win32 exe vs. a DOS exe?

Ok, to answer my own question.... You can detect a Windows file by trying to find the PE File Signature.

http://caolan.wvware.com/~caolan/publink/winresdump/winresdump/doc/pefile.html

I can use this to put "start /wait" in front of Windows executables only.  I haven't tried it yet, but I don't see why it wouldn't work.

-Dave