范例01:仅设置的环境变量有效,不使用本地系统环境变量
//设置环境变量,不使用系统环境变量
SysexecSetenvs(array("test=test","test1=test1"),0);
return SysexecGetenvs();
//结果:
范例02:设置环境变量,添加到系统环境变量之后
SysexecSetenvs(array("test=test","test1=test1"),1);
return SysexecGetenvs();
//部分结果截图:
范例03:设置子进程中的环境变量
//创建读写管道
SysexecNewpipe();
path:="D:\\TinySoftNG\\AnalyseNG.NET\\TSL.exe";
doS:=array('C:\\TinySoftNG\\AnalyseNG.NET\\tsl.exe','C:\\DoTSL\\otherTest\\TestEnv.tsl');
SysexecSetenvs(array('Test=D:\\Test','Test1=D:\\Test1'),0);
b1:=SysexecGetenvs();
//第一个子进程:非阻塞执行,返回进程句柄
hand:= SysExec(path,doS,nil,0,code);
//获取SysExec进程所在管道的输出内容
t1:='';
while t1='' do
begin
t1:= SysexecReadpipe(hand); //循环读取进程的输出内容
sleep(100); //100毫秒读取一次
end
return t1;
返回:子进程环境变量设置成功
其中,TestEnv.tsl脚本的内容如下:
t:=Sysgetenvs();
echo "sys: ",tostn(t),'\r\n';
t:=Sysexecgetenvs();
echo "sysExec: ",tostn(t),'\r\n';
范例04:设置多个子进程中的环境变量
//创建读写管道
SysexecNewpipe();
path:="D:\\TinySoftNG\\AnalyseNG.NET\\TSL.exe";
SysexecSetenvs(array('Test=D:\\Test','Test1=D:\\Test1'),0);
b1:=SysexecGetenvs(); //第一个子进程:非阻塞执行,返回进程句柄
hand:= SysExec(path,'tsl',nil,array('wait':false,'in':'a:="第一个进程"\r\n'),code);
//通过管道获取子进程中的当前环境变量
SysexecWritepipe(hand,"sys1:=Sysgetenvs()\r\n");
sleep(500); //等待命令执行
//获取SysExec进程所在管道的输出内容
t1:='';
while t1='' do
begin
t1:= SysexecReadpipe(hand); //循环读取第一个进程的内容
sleep(100); //100毫秒读取一次
end
SysexecNewpipe(); //再次创建管道
//重设子进程的环境变量组
SysexecSetenvs(array('Test=D:\\AAATest','BBB=D:\\Test1'),0);
b2:=SysexecGetenvs();
//第二个子进程:
hand2:= SysExec(path,'tsl',nil,array('wait':false,'in':'b:="第二个进程"\r\n'),code);
//通过管道获取子进程中的当前环境变量
SysexecWritepipe(hand2,"sys2:=Sysgetenvs()\r\n");
sleep(500); //等待命令执行
//获取SysExec进程所在管道的输出内容
t2:='';
while t2='' do
begin
t2:= SysexecReadpipe(hand2); //循环读取第二个进程的内容
sleep(100); //100毫秒读取一次
end
SysTerminate(code,hand);//终止进程
SysTerminate(code,hand2);//终止进程
return t1+t2;
返回:对Sysexec进程设置的环境变量有生效
SysExec、Sysexecgetenvs