Link to home
Start Free TrialLog in
Avatar of LenB
LenB

asked on

Running a DOS program from within a DELPHI program

I need to know how to run a DOS program (32-bit) from within a DELPHI 3.0 program.

Usually, the DOS program is run in a DOS window and accepts a command line parameter.  I need my program to automatically create such a window and to call the program together with its command line parameter.

I know about the CreateProcess function but I can't get it to work.  I would appreciate being able to look at a working example if you have one and don't mind parting with it.

Thanks in advance.

Len

Avatar of ronit051397
ronit051397

WinExec('c:\something.exe', SW_SHOW);
This command is 16bit based but it will work with delphi3.
I only know something about it for Delphi 2.0 & Delphi 1.0 but I think Delphi 3.0 won't be that different from Delphi 2.0, I can give you some code, but there are a lot of components around with the full source and an example, so I think you can look at them first. If you still want some code, I can give it to you, but look at the next lines and proposals first :

This question is already asked for a 16bit environment. But there is also the answer for 32bit environments. It's a little bit difficult for 16bit. Because of that they proposed a component called XSpawn from HyperAct. It's freeware now and because of that they don't give support anymore. I took it from their site and i'm using it. The great thing of this component is that you can use it on every OS (Windows 3.1/3.11/95/NT, OS/2)

But if you just want a component (with source) for 32bit and windows 95 and NT you can also use the following component :

http://SunSITE.icm.edu.pl/delphi/ftp/d10free/execfl32.zip

I've also used this component, it's great with a good documentation. It's written by Kevin Savko


Hope you're something with this, if not just give a comment and I'll see what I can do.

                                                 ZifNab


Oh yes, something else. The component of Kevin Savko doesn't support the error codes of your dos program. I mean, if the DOS program gives an error when it exits, you can't get it. But that isn't a big deal, because I've changed the code a little bit, and now you can get the error code from the program. So if you want the changed code, just ask!
                              c.u. ZifNab

WinExec is for compatibility with 16bit programs but it call CreateProcess so can run all program's. Why you can't use this.
Try WinExec( 'your.exe param1 param2', SW_SHOW ) this work fine!
ASKER CERTIFIED SOLUTION
Avatar of ZifNab
ZifNab

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ZifNab - Yes you are wrong !  WinExec is for dos program's too and work fine win WinNT too it is very easy for runnig program's. You must go to CreateProcess only if You need acces  to this new process (for example exit code or when you need terminate this process).
Mirek & Len,

Mirek,

Thanks for correcting my mistake!
But as I can read from your comment, you need to use CreateProcess if you want the exit codes of a DOS program. Am I correct, because if not I'll get a heart attack? Then I've always  worked too much. And all that work can be combined in one code-line. Please don't say, that I've worked too much!!!

Len,

If you don't need the exit codes, if you just want to start a program, then please reject my answer and use mireks answer. That's far more easier!!! Otherwise, look at the examples I gave  you and see if they work out for you.

c.u.

ZifNab
Hi ZifNab.
Sorry for my comment. You'r code is ok, but LenB don't say so must wait for terminate process and When reject answer from ronit he(/she) dont say why.
Of course you made mistake with words about WinExec.

p.s. Nice to see New Expert. Here are many qestions awaiting answers.
Avatar of LenB

ASKER

Thanks to all three Experts who commented on my question.

I apologize for not being specific enough about what I wanted.


The reason I was after a solution involving CreateProcess is as follows:

The DOS program is not interactive but processes instructions from a text file given as a command line parameter.  It can take a long time to run (2 to 60 minutes - depending on the instruction file).  Whilst running, it will be writing some stuff (text and numeric parameters) to its DOS window.  I would like the DOS window to be invisible, and to be able to capture this text and use dialog boxes to display the parameters as the DOS process runs.  

The DOS program does a lot of least-squares refinements on a lot of data, and if the Esc key is pressed, it will neatly jump to the next cycle.  If the Ctrl-C key is pressed, it will treat the current refinement as the last cycle and terminate neatly.
So, I would like to be able to feed either an Esc character or a Ctrl_C to the DOS window if the user wants to cut the run short (by pressing an appropriate button in a dialog box) - but I don't necessarily want the ability to kill the process outright.

I didn't think I would be able to do all this with a simple call to WinExec - but maybe I can?  I'm pretty new to Windows programming.