Calling external programs from D

Tydr Schnubbis fake at address.dude
Wed Apr 19 15:48:55 PDT 2006


Regan Heath wrote:
>>>  The common cause of the "4invalid UTF-8 sequence" error is trying to   
>>> output non-ascii characters to the windows console. Can you post your   
>>> current code here.
>>
>> Sure.  As you can see it's full of weird characters...
>>
>> import lib.process;
>> import std.stdio;
>>
>> void main()
>> {
>> 	Process proc;
>> 	// ping my router
>> 	proc = new Process("ping 192.168.0.1");
>> 	writefln(proc.readLine());
>> 	writefln(proc.readLine());
>> 	writefln(proc.readLine());
>> 	writefln(proc.readLine());
>> }
>>
>>
>> I have only made readLine public, and fixed the four calloc calls, no  
>> other changes have been made to your files.
>>
>> Compile with: dmd test.d lib/process.d lib/pipestream.d
>>
>> This prints:
>> Pinging Error: 4invalid UTF-8 sequence
>>
>>
>> If I ping google.com instead, I get this:
>> Ping request could not find host google.com. Please check the name and  
>> try again
>> .
>>
>> Error: ReadFile: The pipe has been ended.
>>
>> ---------
>>
>> Does both of these work for you?  I have no idea what would cause any of  
>> these problems.  I have winxp SP2 US.  CreateProcessA is in  
>> kernel32.dll, of which I have version 5.1.2600.2180  
>> (xpsp_sp2_rtm.040803-2158).  dmd version 0.154, got the same results  
>> with 0.148.
> 
> It's quite odd, try this:
> 
> import lib.process;
> import std.stdio;
> import std.string;
> import std.c.string;
> 
> extern(C) extern char **_environ;
> 
> void main()
> {
> 	Process proc;
> 	proc = new Process();
> 	for(int i = 0; _environ[i]; i++) {
> 		proc.addEnv(toString(_environ[i]).dup);
> 	}
> 	//proc.execute("ping www.google.com");
> 	proc.execute("ping 192.168.0.1");
> 	while(true) writefln("%s",proc.readLine());
> }
> 
> without the addEnv calls above I get the behaviour you're describing. With  
> them it works.
> 
Works for me too, thanks!


> Without them, and using printf I can see that ping responds with:
> 
> "Pinging °ÿ with 32 bytes of data:"
> 
> note the weird characters there. At first I thought maybe the command line  
> I was passing to CreateProcessA was temporary and being collected by the  
> GC, so I changed process.d to use:
> 
> cmd = strdup(std.string.toStringz(command));
> 
> where cmd is a member of Process - so will persist as long as it does.  
> That made no difference. I have no idea why it's doing that, perhaps it  
> reads it's args in a strange way?? I might write a debug program and run  
> that passing different args etc to see if I can replicate the odd  
> behaviour and figure out where it comes from.

Not sure if this helps:
http://www.digitalmars.com/techtips/windows_utf.html



More information about the Digitalmars-d-learn mailing list