views:

892

answers:

3

Hi, I'm having a strange problem with the following code, I'm writing a game launcher in VB.NET:

Dim gameProcess As Process = New Process()
gameProcess.StartInfo.UseShellExecute = False
gameProcess.StartInfo.FileName = TextBox2.Text
gameProcess.Start()
Debug.Print("Game started")
gameProcess.WaitForExit()
Debug.Print("Game stopped")

This code is working fine with most programs, but with some of them (Age of Empires e.g.), I get the following:

Game started

Game stopped

I see the game running for a split second in the task manager, but it immediately closes! Does anyone know why? When I run the game from Windows, it works fine.

I've also tried with Shell, same problem.

And I've tried with cmd.exe and the /C argument, same problem (note that when I type cmd.exe /C path_to_game_exe in the Windows Run Dialog, the game also starts fine), it's only when I launch it from the VB.NET app that it gives problems.

My last idea was to write a temporary batch file and starting that one, but that seems like an ugly solution.

+2  A: 

A lot of games use a launcher app that starts the game in separate process and then closes.

Joel Coehoorn
Not in this case though, there is no launcher exe for this game, and when I manually start the game, the process name is and stays the same as the one I started (so it does not start a separate process).
Macuyiko
A: 

It could be the copy protection that detects the debugger on your system and exits the process.

Try compiling the game in Release mode and run your application when Visual Studio is not running.

ZippyV
Should've mentioned that I tried compiling in Release mode (I thought the debugger might be doing something strange in general). No dice :(.
Macuyiko
+2  A: 

Another option you can try: set the working directory property in the startinfo. You can find this info by looking at the properties of the games' shortcut in your startmenu.

http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory.aspx

ZippyV
That's it, thanks so much! The funny thing is that this entered my mind at one time, but I never bothered to look into it because most programs were starting fine. I assumed the working directory of a new process was set to the directory of the executable itself.Fixed it with: gameProcess.StartInfo.WorkingDirectory = _ System.IO.Path.GetDirectoryName(TextBox2.Text)
Macuyiko