Board index » cppbuilder » exec

exec


2006-11-15 05:21:23 AM
cppbuilder30
I am not having much luck in finding examples on how to use the exec command
to run a program (and get it's return value).
This is what I currently have which does not work ...
Str = "-cfg " + CfgFile;
char *fname = FileEx.c_str();
char *args[] = { Str.c_str() };
Result = execv ( fname, args );
Any ideas?
 
 

Re:exec

"newsgroups.borland.com" < XXXX@XXXXX.COM >writes:
Quote
I am not having much luck in finding examples on how to use the exec
command to run a program (and get it's return value). This is what
I currently have which does not work ...

Str = "-cfg " + CfgFile;
char *fname = FileEx.c_str();
char *args[] = { Str.c_str() };
Result = execv ( fname, args );


Any ideas?
Hmmm, are you sure execv is what you want to call? I don't have the
docs for BCB handy, but typically the exec family of functions does
not spawn a child process... it REPLACES the current process with
another. That is, when you call execv(), if the call succeeds it DOES
NOT RETURN.
Perhaps you want to use the system() function instead?
--
Chris (TeamB);
 

Re:exec

I have a couple of concerns about the code you show and wish you had shown
the declaration and initiialization of the variables 'CfgFile' and 'FileEx'.
This declaration is a problem:
char *args[] = { Str.c_str() };
args here is an array of pointers to char. You have declared it as a single
element array. The calling argument list is usually a set of pointers where
the first pointer is the address of a char* string containing the name of
the program and following that is a set of one or more pointers each
containing the address of a char* string which is a calling argument such as
would be placed on the command line. The last pointer in the array should
have the value NULL.
The spawn* family of functions are the ones designed for running another
program while your program remains in memory. The exec* family of functions
are also for running another program but may end your program when you make
the call. I think what you want is spawn* and not exec*.
I think this might be closer to what you want: (I have not run it through
the compiler)
char *args[] = { FileEx.c_str(), "-cfg", CfgFile.c_str(), NULL };
int Result = execv (P_WAIT, fname, args );
. Ed
Quote
mkreuter wrote in message
news:455a3352$ XXXX@XXXXX.COM ...

I am not having much luck in finding examples on how to use the exec
command to run a program (and get it's return value).
This is what I currently have which does not work ...

Str = "-cfg " + CfgFile;
char *fname = FileEx.c_str();
char *args[] = { Str.c_str() };
Result = execv ( fname, args );
Any ideas?
 

{smallsort}

Re:exec

Ok ... what do I use to launch an executable (with options) and receive it's
return code?
I read somewhere else that system() does not return the application's exit
code ...
If exec stops execution of the main program ... what is left?
"Chris Uzdavinis (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote
"newsgroups.borland.com" < XXXX@XXXXX.COM >writes:

>I am not having much luck in finding examples on how to use the exec
>command to run a program (and get it's return value). This is what
>I currently have which does not work ...
>
>Str = "-cfg " + CfgFile;
>char *fname = FileEx.c_str();
>char *args[] = { Str.c_str() };
>Result = execv ( fname, args );
>
>
>Any ideas?


Hmmm, are you sure execv is what you want to call? I don't have the
docs for BCB handy, but typically the exec family of functions does
not spawn a child process... it REPLACES the current process with
another. That is, when you call execv(), if the call succeeds it DOES
NOT RETURN.

Perhaps you want to use the system() function instead?

--
Chris (TeamB);
 

Re:exec

"newsgroups.borland.com" < XXXX@XXXXX.COM >wrote:
Quote
Ok ... what do I use to launch an executable (with options) and receive it's
return code?
I read somewhere else that system() does not return the application's exit
code ...
Did you read this in the Microsoft MSDN pages? Because MSDN disagrees,
and I'd take Microsoft's word over some random web page most of the
time.
Quote
If exec stops execution of the main program ... what is left?
system()
WinExec().
The spawn() family.
CreateProcess() followed by GetExitCodeProcess().
Hmm, lots of things.
Alan Bellingham
--
ACCU Conference: 11-14 April 2007 - Paramount Oxford Hotel
 

Re:exec

"newsgroups.borland.com" < XXXX@XXXXX.COM >writes:
Quote
Ok ... what do I use to launch an executable (with options) and receive it's
return code?
I read somewhere else that system() does not return the application's exit
code ...
Where did you read that? (Your sources are not reliable!) It's part
of the standard C library, and it's documented--in the standard--to
return the exit code.
If it doesn't do so in BCB, then that's a problem with BCB, but I
can't check for myself as I don't have it handy right now.
--
Chris (TeamB);
 

Re:exec

Chris Uzdavinis (TeamB) < XXXX@XXXXX.COM >wrote:
Quote
Where did you read that? (Your sources are not reliable!) It's part
of the standard C library, and it's documented--in the standard--to
return the exit code.
Just checking the BCB5 help file:
------------------------------------------------------------------------
Return Value
If command is a NULL pointer, system returns nonzero if a command
processor is available.
If command is not a NULL pointer, system returns 0 if the command
processor was successfully started.
If an error occurred, a -1 is returned and errno is set to one of the
following:
ENOENT Path or file function not found
ENOEXEC Exec format error
ENOMEM Not enough memory
------------------------------------------------------------------------
Quote
If it doesn't do so in BCB, then that's a problem with BCB, but I
can't check for myself as I don't have it handy right now.
I do suspect that in this instance, it's dodgy documentation, and that
it'll return the exit code from the executed application.
Alan Bellingham
--
ACCU Conference: 11-14 April 2007 - Paramount Oxford Hotel
 

Re:exec

FYI:
The RTL source shows that if there is no error it returns the exit code of
the process.
. Ed
Quote
Alan Bellingham wrote in message
news: XXXX@XXXXX.COM ...

>If it doesn't do so in BCB, then that's a problem with BCB, but I
>can't check for myself as I don't have it handy right now.

I do suspect that in this instance, it's dodgy documentation, and that
it'll return the exit code from the executed application.
 

Re:exec

"Ed Mulroy" < XXXX@XXXXX.COM >wrote:
Quote
FYI:
The RTL source shows that if there is no error it returns the exit code of
the process.
Thanks. I suspected dodgy docs, and I was right.
Alan Bellingham
--
ACCU Conference: 11-14 April 2007 - Paramount Oxford Hotel
 

Re:exec

I did get spawn to work .... but now I have a momentary command prompt
screen showing - which is kind of annoying .... any way to get this to work
without the command prompt coming up?
"Ed Mulroy" < XXXX@XXXXX.COM >wrote in message
Quote
I have a couple of concerns about the code you show and wish you had shown
the declaration and initiialization of the variables 'CfgFile' and
'FileEx'.

This declaration is a problem:
char *args[] = { Str.c_str() };

args here is an array of pointers to char. You have declared it as a
single element array. The calling argument list is usually a set of
pointers where the first pointer is the address of a char* string
containing the name of the program and following that is a set of one or
more pointers each containing the address of a char* string which is a
calling argument such as would be placed on the command line. The last
pointer in the array should have the value NULL.

The spawn* family of functions are the ones designed for running another
program while your program remains in memory. The exec* family of
functions are also for running another program but may end your program
when you make the call. I think what you want is spawn* and not exec*.

I think this might be closer to what you want: (I have not run it through
the compiler)

char *args[] = { FileEx.c_str(), "-cfg", CfgFile.c_str(), NULL };
int Result = execv (P_WAIT, fname, args );

. Ed

>mkreuter wrote in message
>news:455a3352$ XXXX@XXXXX.COM ...
>
>I am not having much luck in finding examples on how to use the exec
>command to run a program (and get it's return value).
>This is what I currently have which does not work ...
>
>Str = "-cfg " + CfgFile;
>char *fname = FileEx.c_str();
>char *args[] = { Str.c_str() };
>Result = execv ( fname, args );
>Any ideas?


 

Re:exec

newsgroups.borland.com wrote:
Quote
I did get spawn to work .... but now I have a momentary command prompt
screen showing - which is kind of annoying .... any way to get this to work
without the command prompt coming up?
CreateProcess();
 

Re:exec

None of the C/C++ functions support showing a new command window when you
run a console mode program from within a GUI one. The Windows API does
support doing that.
Use CreateProcess and in the STARTUPINFO structure specify SW_HIDE as the
value of wShowWindow and STARTF_USESHOWWINDOW as the value for dwFlags.
It might go something like this (beware of typos - I have not run this
through the compiler)
// function to run a program
// - calling arg is not 'const' because CreateProcess' command
// line parameter is not const
// - command_line must have the program name as the first item
// - return value is the exit code of the program or minus one if
// it failed to run
DWORD RunProgram(AnsiString command_line)
{
STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi;
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
BOOL creatproc_result =
CreateProcess(
NULL, // take application name from command line
command_line.c_str(),
NULL, // use our process security attributes
NULL, // use our thread security attributes
TRUE, // inherit handles
0, // use default creation flags
NULL, // use our environment
NULL, // use our current directory
&si, // ptr to STARTUPINFO structure
&pi); // ptr to PROCESS_INFORMATION structure
DWORD return_value;
if (!createproc_result) // if the call failed
{
return_value = -1; // return result of minus one
MessageBox( // complain
HWND_DESKTOP,
cmd_line,
" Error processing this command ",
MB_ICONSTOP);
}
else do // wait for the process to finish and capture exit code
{
GetExitCodeProcess(pi.hProcess, &return_value);
} while (return_value == STILL_ACTIVE);
return return_value;
} // end RunProgram(AnsiString &)
and in your current code:
const AnsiString space = " ";
AnsiString cmd_line = FileEx + space + "-cfg" + space + CfgFile;
DWORD Result = RunProgram(cmd_line);
. Ed
Quote
mkreuter wrote in message
news:455b6351$ XXXX@XXXXX.COM ...

I did get spawn to work .... but now I have a momentary command prompt
screen showing - which is kind of annoying .... any way to get this to
work without the command prompt coming up?