views:

1348

answers:

2

I'm trying to modify a legacy Delphi 5 app so that it can be launched either from it's icon/via Explorer, or from the console (command-line). When it gets launched from the console, I want the program to detach itself from the console process, so that the console can continue to execute other instructions without waiting for my program to terminate.

I want to use it in a 'batch' file, such that I might have;

@echo off
rem step 1 - do some stuff here
rem
rem step 2 - launch my app
c:\myfolder\myapp
rem
rem step 3 - do some more stuff here

and that the console process moves on to step 3 straight after launching my app in step 2.

I'm sure I've done this before, many years ago, but I'm puzzled as to what exactly I did. I don't want to write a tiny console app 'launcher' for my main Windows app - I'm 95% sure that there was a way of doing this within a 'normal' Delphi GUI app.

I guess I could use vbscript or powershell or something to 'execute' my program with some kind of 'nowait' parameter but the client is familiar with batch files and I don't really want to upset the applecart by suggesting he change his scripts or install additional stuff - I'm making changes to the executable anyway and it would be great to tick this box for him too.

Anyone? :-)

+11  A: 

I think the START command is the one you're looking for. It starts a process separately to the console and it's part of cmd.exe so no extra software required.

But I was of the opinion that GUI apps did this anyway. Maybe Delphi is different to MSVC.

Open up a console and type "start /?".

As itowlson states in the comments, GUI application do generally detach themselves. It's the actual cmd.exe shell doing trickery in that it waits for it to finish if it's running from a cmd file.

So "notepad" from the prompt will start it in the background but "notepad" within a cmd file will wait. Within the cmd file, you need to use:

start notepad.exe

or whatever your application is called (not notepad, presumably).

paxdiablo
GUI apps do this anyway *if you launch them from the console*. But if you launch a GUI app from a batch file, the batch file waits for the GUI app to terminate. E.g. try creating a batch file which just starts Notepad. It's a batch-file vs. interactive thing, not Delphi vs. MSVC. And yeah, it is a bit unintuitive *grin*.
itowlson
I didn't realize it was cmd.exe trickery, thanks, itowlson. I've had trouble with that before automating my work VM at home to automagically start VPN, Notes, firewalls and so on. I'll know better next time. Incorporated your comments into answer.
paxdiablo
Thanks guys - great answer, very helpful.
robsoft
+2  A: 

try: start "" c:\myfolder\myapp (with the empty quotes)

Shay Erlichmen
Thanks for the answer - but the empty quotes bit seems to be where you can give a 'title' to the program. You don't actually *need* them, it seems (from reading about syntax of start from various google hits).
robsoft
If you have to quote your executable path, you will need the empty quotes (or a title inside them). Otherwise, your path will be treated as the title.
aphoria
Ah, that makes sense. Thanks for the clarification!
robsoft
10x, I already forgot why I'm using the empty quotes
Shay Erlichmen