std.process: bug or my fault?

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Wed Jul 1 01:39:51 PDT 2009


novice2 wrote:
> I want to use Phobos
> std.process.execv(in string pathname, in immutable(char)[][] argv)
> to execute external programm with some parameter. But i found, that can't pass parameter properly - they not passed.
> Here very short example - caller.exe shoud call called.exe with parameters:
> 
> /*** begin file caller.d */
> import std.process;
> 
> void main()
> {
>   string[] args = ["a1", "b2", "c3"];
>   std.process.execv("called.exe", args);
> }
> /*** end file caller.d */
> 
> 
> /*** begin file called.d */
> import std.file;
> import std.string;
> 
> void main(string[] args)
> {
>   string s = std.string.join(args, " ");
>   std.file.write("called.log", s);
> }
> /*** end file called.d */
> 
> 
> caller.exe execute called.exe with args "a1 b2 c3",
> then called.exe save this arguments to called.log file.
> 
> but in my environment(Windows XP sp3, DMD 2.030 and DMD 1.045) i have only "called.exe b2 c3" in log file.
> "a1" argument missed.
> 
> Where i made mistake?
> Can anyone reproduce?
> 
> thanks.


Hi,

std.process.execv simply turns the array of arguments in to an array of 
C-style null-terminated strings and passes it to the C standard 
library's execv function. Here's a snippet from the glibc documentation 
on that function:

"The argv argument is an array of null-terminated strings that is used 
to provide a value for the argv argument to the main function of the 
program to be executed. The last element of this array must be a null 
pointer. By convention, the first element of this array is the file name 
of the program sans directory names."

Hence, the "correct" way to do this would be to define your array as:
   string[] args = [ "called.exe", "a1", "b2", "c3" ];

Apparently, the Windows C stdlib automatically replaces args[0] with the 
executable name. This does not happen on Linux, where called.log 
contains "a1 b2 c3", but not "called", after I compile and run your program.

In my opinion, Phobos should do The Right Thing automatically, but 
currently this isn't the case.

-Lars


More information about the Digitalmars-d-learn mailing list