//进程管道操作入门 //相关范例:调用其他语言 » 批处理与命令行 import win.ui; /*DSG{{*/ var winform = win.form(text="管道演示";right=495;bottom=431;) winform.add( button={cls="button";text="findstr";left=26;top=169;right=134;bottom=202;dl=1;dt=1;z=1}; edit={cls="edit";text="aardio";left=224;top=172;right=480;bottom=204;dl=1;dr=1;dt=1;edge=1;z=6}; editInput={cls="edit";text='www.aardio.com\r\nwww.example.com';left=16;top=32;right=480;bottom=167;dl=1;dr=1;dt=1;edge=1;multiline=1;z=2}; editOutput={cls="richedit";left=16;top=212;right=480;bottom=416;db=1;dl=1;dr=1;dt=1;edge=1;hscroll=1;multiline=1;vscroll=1;z=3}; static={cls="static";text="输入字符串:";left=16;top=8;right=120;bottom=24;dl=1;dt=1;transparent=1;z=4}; static2={cls="static";text="启动参数:";left=136;top=177;right=208;bottom=193;align="right";dl=1;dt=1;transparent=1;z=5} ) /*}}*/ //响应控钮点击 winform.button.oncommand = function(id,event){ winform.button.disabledText = {"✶";"✸";"✹";"✺";"✹";"✷"}; import process.popen; /* 启动外部程序,隐藏黑窗口,并返回可读写的进程管道。 参数@2 可用字符串指定进程启动参数,多个参数用空格分格。 如果参数 @1 是表对象,或存在更多个非 null 参数,则合成所有参数为单个启动命令行参数。 */ var prcs,err = process.popen("findstr",winform.edit.text); //如果执行失败,则 返回值1 为null,返回值2 为错误信息,返回值3 为错误代码。 assert(prcs,err); //如果调用 UTF8 编码的程序,请添加下面的编码声明,否则会显示乱码。 //prcs.codepage = 65001 //写数据到进程输入流,相当于 prcs.write(winform.editInput.text,'\n'); prcs.print(winform.editInput.text); //关闭进程输入流 //prcs.writeClose(); /* 自进程获取输入数据,此函数会等待进程结束,但不会阻塞界面消息。 如果用参数@1指定模式串,则返回匹配后的文本。 */ winform.editOutput.text = prcs.readAll(); //关闭进程管道,prcs.readAll() 默认会自动调用 prcs.close() //prcs.close() winform.button.disabledText = null; } winform.show() win.loopMessage();