Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

execl() question? 1

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
I've never really used any of the exec() functions before, but I decided to try it out and found something strange.
The MSDN has the syntax of execl() like this:
Code:
int _execl( const char *cmdname, const char *arg0, ... const char *argn, NULL );
However, if I want to use execl() I need to call it like this:
Code:
int main()
{
	string prog( "c:/temp/hello.bat" );
	string arg1( "Chris" );
	_execl( prog.c_str(), prog.c_str(), arg1.c_str(), NULL );
	cout << "Should never reach here!" << endl;

	return 0;
}
Why do I need to pass the name of the program that I'm executing twice when the syntax only lists it as the first parameter? If I run:
Code:
_execl( prog.c_str(), arg1.c_str(), NULL );
It will tell me that "Chris" is not a recognized command...
 
It's a common convention (or tradition) in C and C++ to pass exe name via argv[0] pointer to main(). You may follow it or may not - it's not an error to pass any string after 1st arg of execl. In your case you call .bat file, not a C or C++ program, so no matter what's your 2nd parameter value.

However, see your cmd file (hello.bat). It's source of the error diagnostic message.
 
Thanks! There must be some strange dependency in .bat files that requires arg[0] to always be the same as the .bat name. When I ran the same code using an exe that just spits out all the argv values, I can pass anything as the second parameter.
 
> There must be some strange dependency in .bat files that requires arg[0] to always be the same as the .bat name.
BAT files cannot be run as such, they can only be run by a script interpreter (command.com or cmd.exe).

What in effect gets executed is
[tt]cmd.exe /c c:/temp/hello.bat Chris[/tt]
Where the blue bit is simply a blind copy of the original argv. In the case where it doesn't work, you just end up with
[tt]cmd.exe /c Chris[/tt]
which isn't what you wanted, and thus the "command not found" becomes apparent.

I wonder if execl() just looks at the suffix first, and sees .bat before trying anything else?
For example, does this even work (speculate mode)
Code:
_execl( "foo.bat", prog.c_str(), arg1.c_str(), NULL );
It just recognises the .bat, then immediately goes to find the command interpreter and pass argv[] to it, without bothering to see if "foo.bat" actually exists anywhere?

--
 
I don't know, but I found yet another strange thing when I ran:
Code:
execl( "hello.bat", "PrintArgs.exe", "abcd", NULL );
where "PrintArgs.exe" is just:
Code:
int main( int argc, char* argv[] )
{
   for ( int i = 0; i < argc; ++i )
   {
      cout << "Arg[" << i << "]: " << argv[i] << endl;
   }
}
It acts as though it skipped right over the .bat file and just executes the "PrintArgs.exe".
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top