Calling external programs from D

Regan Heath regan at netwin.co.nz
Wed Apr 5 16:01:16 PDT 2006


On Wed, 05 Apr 2006 19:22:25 +0200, Tydr Schnubbis <fake at address.dude>  
wrote:
>>> On Wed, 05 Apr 2006 00:36:45 -0700, kris <foo at bar.com> wrote:
>>>> Tydr Schnubbis wrote:
>>>>> I'm making a gui app with DWT, and I need a way to run an external,   
>>>>> command line tool and get its output.  Either directly from the  
>>>>> tool's  stdout, or by having it write to a file first.
>>>>>  I'm working on windows.  The system() function is unusable because  
>>>>> it  opens a new command line window when it starts the tool.  That  
>>>>> you can  get it to close the window again really fast by using  
>>>>> 'start' is not  good enough
>>>>>  It doesn't have to be a portable way.  If someone can tell me how  
>>>>> to  call _popen or something in msvcrt.dll, I would be happy.
>>>>
>>>> I understand Regan posted a module to do exactly what you want.  
>>>> Maybe  he'll see this, or you may be able to dig it up again from the  
>>>> archives  (or via google upon the digitalmars site). Reckon the  
>>>> keywords would be    something like ~ win32 pipe process regan
>>>  Here they/it is :)
>>> Regan
>>  Thanks!
>>  But it doesn't work for me.  Seems that it blocks the new process from  
>> acessing the network.  Any ideas what to do?
>
> Here's a minimal test to show the problem:
>
> import lib.process;
> import std.stdio;
>
> void main()
> {
> 	Process proc;
> 	proc = new Process("ping google.com");
> 	writef(proc.readLine());
> }
>
> Compile: "dmd test.d lib/process.d lib/pipestream.d"

> Output: "Ping request could not find host google.com. Please check the  
> name and try again."

(Cross posted to digitalmars.D so more people see it)

(You have to move the readLine function from private to public in  
pipestream, that was obviously a mistake on my part)

I get the same result. Odd. I thought at first it might be because ping  
uses enviroment variables to find/use the DNS service, but adding the  
current enviroment variables to the new process makes no difference:

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");
	writef(proc.readLine());
}

I also tried using CreateProcessAsUser with the handle returned by:
   OpenProcessToken(GetCurrentProcess(), ..etc..

it made no difference (not that I expected it to, but you never know).

Does anyone have any idea why ping cannot access the DNS service?

Regan



More information about the Digitalmars-d-learn mailing list