Dragon222
asked on
System call in Delphi/Kylix
Hi!
How to make a system call in Delphi/Kylix AND get the system responce into the application? (working under Windows and Linux)
How to make a system call in Delphi/Kylix AND get the system responce into the application? (working under Windows and Linux)
ASKER
Well...
If I run System('ls') in my Kylix application, how do I then get te output from ls back into my application (like into some Memo component)?
(Ok I can do like this, System('ls > myfile') and then read from this temporary file, but this is ugly...)
If I run System('ls') in my Kylix application, how do I then get te output from ls back into my application (like into some Memo component)?
(Ok I can do like this, System('ls > myfile') and then read from this temporary file, but this is ugly...)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Tanks, GloomyFriar!
But please, for a novice like me, can you write down the few lines of code that I need... without some example I'm as lost as before...
But please, for a novice like me, can you write down the few lines of code that I need... without some example I'm as lost as before...
I have no the sample code in Delphi. If it helps below is code in C (for Windows)
// The steps for redirecting child process's STDOUT:
// 1. Save current STDOUT, to be restored later.
// 2. Create anonymous pipe to be STDOUT for child process.
// 3. Set STDOUT of the parent process to be write handle to
// the pipe, so it is inherited by the child process.
// 4. Create a noninheritable duplicate of the read handle and
// close the inheritable read handle.
// Save the handle to the current STDOUT.
hSaveStdout = GetStdHandle(STD_OUTPUT_HA NDLE);
// Create a pipe for the child process's STDOUT.
if (! CreatePipe(&hChildStdoutRd , &hChildStdoutWr, &saAttr, 0))
ErrorExit("Stdout pipe creation failed\n");
// Set a write handle to the pipe to be STDOUT.
if (! SetStdHandle(STD_OUTPUT_HA NDLE, hChildStdoutWr))
ErrorExit("Redirecting STDOUT failed");
// Create noninheritable read handle and close the inheritable read
// handle.
fSuccess = DuplicateHandle(GetCurrent Process(), hChildStdoutRd,
GetCurrentProcess(), &hChildStdoutRdDup , 0,
FALSE,
DUPLICATE_SAME_ACCESS);
if( !fSuccess )
ErrorExit("DuplicateHandle failed");
CloseHandle(hChildStdoutRd );
// Now create the child process.
if (! CreateChildProcess((LPCTST R)run)) /* Just a wrapper to CreateProcess */
ErrorExit("Create process failed");
// After process creation, restore the saved STDIN and STDOUT.
if (! SetStdHandle(STD_OUTPUT_HA NDLE, hSaveStdout))
ErrorExit("Re-redirecting Stdout failed\n");
// The steps for redirecting child process's STDOUT:
// 1. Save current STDOUT, to be restored later.
// 2. Create anonymous pipe to be STDOUT for child process.
// 3. Set STDOUT of the parent process to be write handle to
// the pipe, so it is inherited by the child process.
// 4. Create a noninheritable duplicate of the read handle and
// close the inheritable read handle.
// Save the handle to the current STDOUT.
hSaveStdout = GetStdHandle(STD_OUTPUT_HA
// Create a pipe for the child process's STDOUT.
if (! CreatePipe(&hChildStdoutRd
ErrorExit("Stdout pipe creation failed\n");
// Set a write handle to the pipe to be STDOUT.
if (! SetStdHandle(STD_OUTPUT_HA
ErrorExit("Redirecting STDOUT failed");
// Create noninheritable read handle and close the inheritable read
// handle.
fSuccess = DuplicateHandle(GetCurrent
GetCurrentProcess(), &hChildStdoutRdDup , 0,
FALSE,
DUPLICATE_SAME_ACCESS);
if( !fSuccess )
ErrorExit("DuplicateHandle
CloseHandle(hChildStdoutRd
// Now create the child process.
if (! CreateChildProcess((LPCTST
ErrorExit("Create process failed");
// After process creation, restore the saved STDIN and STDOUT.
if (! SetStdHandle(STD_OUTPUT_HA
ErrorExit("Re-redirecting Stdout failed\n");
what you mean with "system call" and "system response"?