如何获得dos输出.在delphi2009中使用vista

前端之家收集整理的这篇文章主要介绍了如何获得dos输出.在delphi2009中使用vista前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我用delphi来获取dos输出.
什么原因导致 http://delphi.about.com/cs/adptips2001/a/bltip0201_2.htm代码无法在vista上使用delphi2009?但它适用于XP中的D7.我不知道要修改哪个部分才能使其正常工作.
DelphiDabbler has a solution,虽然我没有亲自测试过:
  1. function GetDosOutput(CommandLine: string; Work: string = 'C:\'): string;
  2. var
  3. SA: TSecurityAttributes;
  4. SI: TStartupInfo;
  5. PI: TProcessInformation;
  6. StdOutPipeRead,StdOutPipeWrite: THandle;
  7. WasOK: Boolean;
  8. Buffer: array[0..255] of AnsiChar;
  9. BytesRead: Cardinal;
  10. WorkDir: string;
  11. Handle: Boolean;
  12. begin
  13. Result := '';
  14. with SA do begin
  15. nLength := SizeOf(SA);
  16. bInheritHandle := True;
  17. lpSecurityDescriptor := nil;
  18. end;
  19. CreatePipe(StdOutPipeRead,StdOutPipeWrite,@SA,0);
  20. try
  21. with SI do
  22. begin
  23. FillChar(SI,SizeOf(SI),0);
  24. cb := SizeOf(SI);
  25. dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
  26. wShowWindow := SW_HIDE;
  27. hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
  28. hStdOutput := StdOutPipeWrite;
  29. hStdError := StdOutPipeWrite;
  30. end;
  31. WorkDir := Work;
  32. Handle := CreateProcess(nil,PChar('cmd.exe /C ' + CommandLine),nil,True,PChar(WorkDir),SI,PI);
  33. CloseHandle(StdOutPipeWrite);
  34. if Handle then
  35. try
  36. repeat
  37. WasOK := ReadFile(StdOutPipeRead,Buffer,255,BytesRead,nil);
  38. if BytesRead > 0 then
  39. begin
  40. Buffer[BytesRead] := #0;
  41. Result := Result + Buffer;
  42. end;
  43. until not WasOK or (BytesRead = 0);
  44. WaitForSingleObject(PI.hProcess,INFINITE);
  45. finally
  46. CloseHandle(PI.hThread);
  47. CloseHandle(PI.hProcess);
  48. end;
  49. finally
  50. CloseHandle(StdOutPipeRead);
  51. end;
  52. end;

猜你在找的Windows相关文章