您的位置:程序门 -> delphi -> windows sdk/api



在windows界面下访问dos,并返回结果?(在线等)


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


在windows界面下访问dos,并返回结果?(在线等)[已结贴,结贴人:gavin_java616]
发表于:2007-04-16 10:20:35 楼主
求详细代码,在windows界面下,能直接访问dos界面,并返回结果.达到重定向的效果~~谢谢!!
发表于:2007-04-16 16:47:381楼 得分:0
大侠们,急啊!等了一天了,都没的反映的!知道的来一段代码啊~~谢谢了~~~~~
发表于:2007-04-18 09:17:552楼 得分:0
up
发表于:2007-04-18 09:30:043楼 得分:0
去盒子   www.2ccc.com   搜索一下dos应该有的
发表于:2007-04-18 13:28:564楼 得分:0
真的需要直接访问dos界面?如果是图形界面怎么办?

如果需要接收dos程序的标准输出,可以用管道。
有个控件,很好用的
http://www.google.cn/search?complete=1&hl=zh-cn&q=stdioredirect&meta=
发表于:2007-04-18 14:47:505楼 得分:2
用命名管道
俺写的
procedure   rundoscommand(command:   string;   output:   tstringlist;   timeout:   integer);
var
    hreadpipe:   thandle;
    hwritepipe:   thandle;
    si:   tstartupinfo;
    pi:   tprocessinformation;
    sa:   tsecurityattributes;
//     sd   :   tsecuritydescriptor;
    bytesread:   dword;
    dest:   array[0..1023]   of   char;
    cmdline:   array[0..512]   of   char;
    tmplist:   tstringlist;
    avail,   exitcode,   wrresult:   dword;
    osver:   tosversioninfo;
    tmpstr:   ansistring;
    all,   send:   string;
    tc:   integer;
begin

    begin
//         initializesecuritydescriptor(@sd,   security_descriptor_revision);
//         setsecuritydescriptordacl(@sd,   true,   nil,   false);
        sa.nlength   :=   sizeof(sa);
        sa.lpsecuritydescriptor   :=   nil;   //@sd;
        sa.binherithandle   :=   true;
        createpipe(hreadpipe,   hwritepipe,   @sa,   0);
    end;

    try

        fillchar(si,   sizeof(si),   0);
        si.cb   :=   sizeof(tstartupinfo);
        si.wshowwindow   :=   sw_hide;
        si.dwflags   :=   startf_useshowwindow;
        si.dwflags   :=   si.dwflags   or   startf_usestdhandles;
        si.hstdoutput   :=   hwritepipe;
        si.hstderror   :=   hwritepipe;
        strpcopy(cmdline,   'cmd.exe   /c   '   +   command);

        tc   :=   0;
        if   createprocess(nil,   cmdline,   nil,   nil,   true,   normal_priority_class,   nil,   nil,   si,   pi)   then
            begin
                exitcode   :=   0;

                while   exitcode   =   0   do
                    begin

                        wrresult   :=   waitforsingleobject(pi.hprocess,   500);
                        inc(tc,   500);

                        bytesread   :=   1024;

                        while   bytesread   >   0   do
                            if   peeknamedpipe(hreadpipe,   @dest[0],   1024,   @avail,   nil,   nil)   then
                                begin
                                    if   avail   >   0   then
                                        begin

                                            try
                                                fillchar(dest,   sizeof(dest),   0);
                                                readfile(hreadpipe,   dest[0],   avail,   bytesread,   nil);
                                                tmpstr   :=   copy(dest,   0,   bytesread);
                                                all   :=   all   +   tmpstr;

                                            finally

                                            end;

                                        end
                                    else
                                        break;
                                end
                            else
                                break;


                        if   wrresult   <>   wait_timeout   then
                            begin
                                exitcode   :=   1;
                        //     output.add( '&ocirc;&euml;&eth;&eth;&iacute;ê&sup3;&eacute;&pound;&iexcl; ')
                            end

                        else
                            begin
                                if   tc   >   timeout   then
                                    if   terminateprocess(pi.hprocess,   0)   then
                                        begin
                                            send   :=   '&ocirc;&euml;&eth;&eth;&sup3;&not;&ecirc;±&pound;&not;&ccedil;&iquest;&ouml;&aelig;&frac12;á&ecirc;&oslash;&pound;&iexcl; '
                                        end
                                    else
                                        begin
                                            send   :=   '&ocirc;&euml;&eth;&eth;&sup3;&not;&ecirc;±&pound;&not;&ccedil;&iquest;&ouml;&aelig;&frac12;á&ecirc;&oslash;&ecirc;§°&uuml;&pound;&iexcl; '
                                        end;

                            end;
                    end;


                getexitcodeprocess(pi.hprocess,   exitcode);
                closehandle(pi.hprocess);
                closehandle(pi.hthread);
            end
        else
            output.add( '&ocirc;&euml;&eth;&eth;&sup2;&raquo;&auml;&uuml;&frac12;&oslash;&eth;&eth;&pound;&iexcl; ');

        output.text   :=   all;
        if   send   <>   ' '   then
            output.add(send);
        all   :=   ' ';
        tmpstr   :=   ' ';
        send   :=   ' ';
    finally
        closehandle(hreadpipe);
        closehandle(hwritepipe);
    end;
end;
发表于:2007-04-18 14:49:526楼 得分:3
procedure   rundoscommand(command:   string;   output:   tstringlist;   timeout:   integer);
var
    hreadpipe:   thandle;
    hwritepipe:   thandle;
    si:   tstartupinfo;
    pi:   tprocessinformation;
    sa:   tsecurityattributes;
//     sd   :   tsecuritydescriptor;
    bytesread:   dword;
    dest:   array[0..1023]   of   char;
    cmdline:   array[0..512]   of   char;
    tmplist:   tstringlist;
    avail,   exitcode,   wrresult:   dword;
    osver:   tosversioninfo;
    tmpstr:   ansistring;
    all,   send:   string;
    tc:   integer;
begin

    begin
//         initializesecuritydescriptor(@sd,   security_descriptor_revision);
//         setsecuritydescriptordacl(@sd,   true,   nil,   false);
        sa.nlength   :=   sizeof(sa);
        sa.lpsecuritydescriptor   :=   nil;   //@sd;
        sa.binherithandle   :=   true;
        createpipe(hreadpipe,   hwritepipe,   @sa,   0);
    end;

    try

        fillchar(si,   sizeof(si),   0);
        si.cb   :=   sizeof(tstartupinfo);
        si.wshowwindow   :=   sw_hide;
        si.dwflags   :=   startf_useshowwindow;
        si.dwflags   :=   si.dwflags   or   startf_usestdhandles;
        si.hstdoutput   :=   hwritepipe;
        si.hstderror   :=   hwritepipe;
        strpcopy(cmdline,   'cmd.exe   /c   '   +   command);

        tc   :=   0;
        if   createprocess(nil,   cmdline,   nil,   nil,   true,   normal_priority_class,   nil,   nil,   si,   pi)   then
            begin
                exitcode   :=   0;

                while   exitcode   =   0   do
                    begin

                        wrresult   :=   waitforsingleobject(pi.hprocess,   500);
                        inc(tc,   500);

                        bytesread   :=   1024;

                        while   bytesread   >   0   do
                            if   peeknamedpipe(hreadpipe,   @dest[0],   1024,   @avail,   nil,   nil)   then
                                begin
                                    if   avail   >   0   then
                                        begin

                                            try
                                                fillchar(dest,   sizeof(dest),   0);
                                                readfile(hreadpipe,   dest[0],   avail,   bytesread,   nil);
                                                tmpstr   :=   copy(dest,   0,   bytesread);
                                                all   :=   all   +   tmpstr;

                                            finally

                                            end;

                                        end
                                    else
                                        break;
                                end
                            else
                                break;


                        if   wrresult   <>   wait_timeout   then
                            begin
                                exitcode   :=   1;
                        //     output.add( '运行完成! ')
                            end

                        else
                            begin
                                if   tc   >   timeout   then
                                    if   terminateprocess(pi.hprocess,   0)   then
                                        begin
                                            send   :=   '运行超时,强制结束! '
                                        end
                                    else
                                        begin
                                            send   :=   '运行超时,强制结束失败! '
                                        end;

                            end;
                    end;


                getexitcodeprocess(pi.hprocess,   exitcode);
                closehandle(pi.hprocess);
                closehandle(pi.hthread);
            end
        else
            output.add( '运行不能进行! ');

        output.text   :=   all;
        if   send   <>   ' '   then
            output.add(send);
        all   :=   ' ';
        tmpstr   :=   ' ';
        send   :=   ' ';
    finally
        closehandle(hreadpipe);
        closehandle(hwritepipe);
    end;
end;


快速检索

最新资讯
热门点击