delphi cmd(4个例子都是通过管道取得)
- //K8执行DOS并返回结果
- function RunDosCommand(Command: string): string;
- 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: string;
- begin
- osVer.dwOSVersionInfoSize := Sizeof(TOSVERSIONINFO);
- GetVersionEX(osVer);
- if osVer.dwPlatformId = VER_PLATFORM_WIN32_NT then
- 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
- else
- CreatePipe(hReadPipe, hWritePipe, nil, 1024);
- 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, Command);
- 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);
- // if PeekNamedPipe(hReadPipe, nil, 0, nil, @Avail, nil) then
- if PeekNamedPipe(hReadPipe, @Dest[0], 1024, @Avail, nil, nil) then
- begin
- if Avail > 0 then
- begin
- TmpList := TStringList.Create;
- try
- FillChar(Dest, SizeOf(Dest), 0);
- ReadFile(hReadPipe, Dest[0], Avail, BytesRead, nil);
- TmpStr := Copy(Dest, 0, BytesRead - 1);
- TmpList.Text := TmpStr;
- Result := tmpstr;
- finally
- TmpList.Free;
- end;
- end;
- end;
- if wrResult <> WAIT_TIMEOUT then ExitCode := 1;
- end;
- GetExitCodeProcess(PI.hProcess, ExitCode);
- CloseHandle(PI.hProcess);
- CloseHandle(PI.hThread);
- end;
- finally
- CloseHandle(hReadPipe);
- CloseHandle(hWritePipe);
- end;
- end;
- //002
- function RunCommand(const cmd: string): string;
- var
- hReadPipe,hWritePipe:THandle;
- si:STARTUPINFO;
- lsa:SECURITY_ATTRIBUTES;
- pi:PROCESS_INFORMATION;
- cchReadBuffer:DWORD;
- pOutStr, pCMD:PChar;
- res, strCMD:string;
- begin
- strcmd := 'cmd.exe /k ' + cmd;
- pOutStr := AllocMem(5000);
- lsa.nLength := SizeOf(SECURITY_ATTRIBUTES);
- lsa.lpSecurityDescriptor := nil;
- lsa.bInheritHandle := True;
- if not CreatePipe(hReadPipe, hWritePipe, @lsa, 0) then Exit;
- FillChar(si, SizeOf(STARTUPINFO), 0);
- si.cb:=sizeof(STARTUPINFO);
- si.dwFlags:=(STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);
- si.wShowWindow:=SW_HIDE;
- si.hStdOutput:=hWritePipe;
- if not CreateProcess(nil, PChar(strCMD), nil, nil, true, 0, nil, nil, si, pi) then Exit;
- while(true) do
- begin
- if not PeekNamedPipe(hReadPipe, pOutStr, 1, @cchReadBuffer, nil, nil) then break;
- if cchReadBuffer <> 0 then
- begin
- if not ReadFile(hReadPipe, pOutStr^, 4096, cchReadBuffer, nil) then break;
- pOutStr[cchReadbuffer]:=chr(0);
- //if @Show <> nil then Show(pOutStr);
- res := res + pOutStr;
- end else if(WaitForSingleObject(pi.hProcess ,0) = WAIT_OBJECT_0) then break;
- Sleep(10);
- Application.ProcessMessages;
- end;
- pOutStr[cchReadBuffer]:=chr(0);
- CloseHandle(hReadPipe);
- CloseHandle(pi.hThread);
- CloseHandle(pi.hProcess);
- CloseHandle(hWritePipe);
- FreeMem(pOutStr);
- Result := res;
- end;
- //003
- procedure CmdExecAndView(FileName: string; memo: TMemo);
- procedure _AddInfo(mmInfo:TMemo; S: string; var line: string);
- var
- i, p: Integer;
- begin
- if mmInfo.Lines.Count > 800 then
- mmInfo.Lines.Clear;
- //去掉 \r
- for i := 0 to Length(S) - 1 do
- if S[i] = #13 then S[i] := ' ';
- line := line + S;
- // \n 断行
- p := Pos(#10, line);
- if p > 0 then
- begin
- // \n 前面的加入一行,后面的留到下次
- mmInfo.Lines.Add(Copy(line, 1, p - 1));
- line := Copy(line, p + 1, Length(line) - p);
- end;
- end;
- var
- hReadPipe, hWritePipe: THandle;
- si: STARTUPINFO;
- lsa: SECURITY_ATTRIBUTES;
- pi: PROCESS_INFORMATION;
- cchReadBuffer: DWORD;
- ph: PChar;
- fname: PChar;
- line: string;
- begin
- fname := allocmem(1024);
- ph := AllocMem(1024);
- lsa.nLength := sizeof(SECURITY_ATTRIBUTES);
- lsa.lpSecurityDescriptor := nil;
- lsa.bInheritHandle := True;
- if CreatePipe(hReadPipe, hWritePipe, @lsa, 0) = false then
- Exit;
- fillchar(si, sizeof(STARTUPINFO), 0);
- si.cb := sizeof(STARTUPINFO);
- si.dwFlags := (STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);
- si.wShowWindow := SW_HIDE;
- si.hStdOutput := hWritePipe;
- si.hStdError := hWritePipe;
- StrPCopy(fname, FileName);
- if CreateProcess(nil, fname, nil, nil, true, 0, nil, nil, si, pi) = False then
- begin
- FreeMem(ph);
- FreeMem(fname);
- Exit;
- end;
- CloseHandle(hWritePipe);
- while (true) do
- begin
- if not PeekNamedPipe(hReadPipe, ph, 1, @cchReadBuffer, nil, nil) then break;
- if cchReadBuffer <> 0 then
- begin
- if ReadFile(hReadPipe, ph^, 512, cchReadBuffer, nil) = false then break;
- ph[cchReadbuffer] := chr(0);
- _AddInfo(memo, ph, line);
- end
- else if (WaitForSingleObject(pi.hProcess, 0) = WAIT_OBJECT_0) then break;
- Application.ProcessMessages;
- Sleep(200);
- end;
- ph[cchReadBuffer] := chr(0);
- _AddInfo(memo, ph, line);
- CloseHandle(hReadPipe);
- CloseHandle(pi.hThread);
- CloseHandle(pi.hProcess);
- FreeMem(ph);
- FreeMem(fname);
- end;
- //004
- var
- hReadPipe,hWritePipe:THandle;
- si:STARTUPINFO;
- lsa:SECURITY_ATTRIBUTES;
- pi:PROCESS_INFORMATION;
- mDosScreen:String;
- cchReadBuffer:DWORD;
- ph:PChar;
- fname:PChar;
- i,j:integer;
- begin
- fname:=allocmem(255);
- ph:=AllocMem(5000);
- lsa.nLength :=sizeof(SECURITY_ATTRIBUTES);
- lsa.lpSecurityDescriptor :=nil;
- lsa.bInheritHandle :=True;
- if CreatePipe(hReadPipe,hWritePipe,@lsa,0)=false then
- begin
- ShowMessage('Can not create pipe!');
- exit;
- end;
- fillchar(si,sizeof(STARTUPINFO),0);
- si.cb :=sizeof(STARTUPINFO);
- si.dwFlags :=(STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);
- si.wShowWindow :=SW_HIDE;
- si.hStdOutput :=hWritePipe;
- StrPCopy(fname,EditFilename.text);
- if CreateProcess( nil, fname, nil, nil, true, 0, nil, nil, si, pi) = False then
- begin
- ShowMessage('can not create process');
- FreeMem(ph);
- FreeMem(fname);
- Exit;
- end;
- while(true) do
- begin
- if not PeekNamedPipe(hReadPipe,ph,1,@cchReadBuffer,nil,nil) then break;
- if cchReadBuffer<>0 then
- begin
- if ReadFile(hReadPipe,ph^,4096,cchReadBuffer,nil)=false then break;
- ph[cchReadbuffer]:=chr(0);
- Memo1.Lines.Add(ph);
- end
- else if(WaitForSingleObject(pi.hProcess ,0)=WAIT_OBJECT_0) then break;
- Sleep(100);
- end;
- ph[cchReadBuffer]:=chr(0);
- Memo1.Lines.Add(WideCharToString(ph));
- CloseHandle(hReadPipe);
- CloseHandle(pi.hThread);
- CloseHandle(pi.hProcess);
- CloseHandle(hWritePipe);
- FreeMem(ph);
- FreeMem(fname);
- end;
http://blog.csdn.net/earbao/article/details/21160033
delphi cmd(4个例子都是通过管道取得)的更多相关文章
- Delphi完成的断点续传例子 转
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- delphi 三层架构简单例子(经测试成功)
delphi 三层架构简单例子(经测试成功) 转载 2013年12月19日 09:48:57 1100 所谓三层: (1) 客户端 (2) 服务器端 (3) 数据库 在数据访问时,使得客户端必须通过服 ...
- 和S5933比较起来,开发PLX9054比较不幸,可能是第一次开发PCI的缘故吧。因为,很多PCI的例子都是对S5933,就连微软出版的《Programming the Microsoft Windows Driver Model》都提供了一个完整的S5933的例子。 在这篇有关DDK的开发论文里。
和S5933比较起来,开发PLX9054比较不幸,可能是第一次开发PCI的缘故吧.因为,很多PCI的例子都是对S5933,就连微软出版的<Programming the Microsoft Wi ...
- delphi cmd
今天看到有人在问用代码执行CMD命令的问题,就总结一下用法,也算做个备忘. Delphi中,执行命令或者运行一个程序有2个函数,一个是winexec,一个是shellexecute.这两个大家应该都见 ...
- Delphi Dll 动态调用例子(3)-仔细看一下
http://blog.163.com/bxf_0011/blog/static/35420330200952075114318/ Delphi 动态链接库的动态和静态调用 为了让人能快速的理解 静态 ...
- 解决每次从cmd进入sqlplus,都得重新设置pagesize、linesize的问题
https://blog.csdn.net/u012127798/article/details/34146143/ Oracle里的set零零碎碎的,这里整理归纳一下 SQL> set tim ...
- Delphi 操作Flash D7~XE10都有 导入Activex控件 shockwave
http://www.cnblogs.com/devcjq/articles/2906224.html Flash是Macromedia公司出品的,用在互联网上动态的.可互动的shockwave.它的 ...
- 转:Delphi 回调函数及例子
http://anony3721.blog.163.com/blog/static/5119742010866050589/ { http://anony3721.blog.163.com/blog/ ...
- Windows自带的驱动程序例子都在哪里?
MSDN官方说明:https://msdn.microsoft.com/windows/hardware/drivers/samples/index 各个操作系统驱动例子: Windows10 :h ...
随机推荐
- switch的方便用法
int ch = getch(); switch(ch) { case '0' ... '9': if (in_count) { count = count * 10 + (ch - '0'); } ...
- PHP - 设置地址栏小图标
效果: 1/把icon图标直接放到根目录. 2/在header标签中写下: <link rel="icon" type="image/x-icon" hr ...
- 2783: [JLOI2012]树( dfs + BST )
直接DFS, 然后用set维护一下就好了.... O(nlogn) ------------------------------------------------------------------ ...
- 异步的两种写法: async 与 BeginInvoke
现在要实现异步只要用关键字async/await就可以轻松实现,在此之前需要用到委托/回调等一堆东西. 对一下是对比写法: class Program { delegate string SendMe ...
- Ch02 从零开始实例学习3
提纲:---------------------------- 演练2-3:添加控制器 知识点2-3:控制器的职责 知识点2-4:控制器的类别与方法 ------------------------- ...
- 谈论Java原子变量和同步的效率 -- 颠覆你的生活
我们认为,由于思维定式原子变量总是比同步运行的速度更快,我想是这样也已经,直到实现了ID在第一次测试过程生成器不具有在这样一个迷迷糊糊的东西. 测试代码: import java.util.Array ...
- HDU 2544 最短
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2544 解析: 首先数据量为V<=100 那么这里使用不论什么基础的最短路的算法都不会超时! 常见数 ...
- JAVA 操作 DBF 文件数据库
1.依赖夹包 javadbf-[].4.1.jar jconn3.jar 2.添加属性文件 jdbc.properties jdbc.driverClassName=com.sybase.jdbc3. ...
- 上一篇括号配对让人联想起catalan数,顺便转载一篇归纳的还不错的文章
转载请注明来自souldak,微博:@evagle 怎么样才是合法的组合? 只要每一时刻保证左括号的数目>=右括号的数目即可. 直接递归就行,每次递归加一个括号,左括号只要还有就能加,右括号要保 ...
- 小型Mp3播放器
准备三张图片,名字分别为: play.pause.stop. 将一个名为Mp3的文件放入res/raw文件夹中. 在main.xml中: <LinearLayout xmlns:android= ...