PDA

View Full Version : Executing External Applications using Exec



DarknessX
08-05-2007, 12:54 AM
System: WinXP
Language: Pascal
OOP: No
Compiler/IDE: FPC
Libraries/API: None.

Update:
Even though there are spaces in the path to my exe, it doesn't change anything, as I ran it from C:\ and it still didn't do it. (Yes, I moved the text file with it).

OK, well, after much experimenting, I found:
http://freepascal.org/docs-html/rtl/dos/exec.html

I knew about this before, but this means basically to call an external app, I do this:


SwapVectors; {Not sure why, but I do remember this being used before..}
Exec(path,com); {Path to file, then commands to be run on the file...}
SwapVectors;


However, when I run this code:

SwapVectors;
Exec('time.txt','');
SwapVectors;
readln;

It doesn't run the app, but it waits on the readln... I'm confused. What do I need to do?

Or, is there another, just as easy way to do this?

LP
08-05-2007, 01:33 AM
On Win32 platform you can use ShellExecute() from Win32 API to run the application, e.g.:


// hWindow is the handle of your app's window
ShellExecute(hWindow, 'open', 'c:\Windows\cmd.exe', nil, nil,
SW_SHOWNORMAL) ;

Chebmaster
08-05-2007, 06:17 AM
You can also use TProcess from "process" unit. While it's piping mechanism is broken (i.e. you cannot get the program output using it), the non-piped mode works just fine. You can control if your program should wait for it, or run it in parallel.

But if the called process uses something like ReadLn, there is no help.

DarknessX
09-05-2007, 12:16 AM
Well, with messing around, I made:

ShellExecute(hwindow, 'open', 'openal.txt', nil, nil, SW_SHOWNORMAL) ;
readln;

work. Couldn't figure out what unit to use, so I randomly started testing some, and found Windows was what I needed. This should help :P