管道重定向
来源:互联网 发布:网络信息资源检索 编辑:程序博客网 时间:2024/04/27 13:22
摘自:http://community.csdn.net/Expert/topic/4511/4511617.xml?temp=.4288599
(没试验过)
管道重定向:
unit recon;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{R *.dfm}
function CreateDOSProcessRedirected(const CommandLine, InputFile,
OutputFile, ErrMsg :string):boolean;
const
ROUTINE_ID = '[function: CreateDOSProcessRedirected ]';
var
OldCursor : TCursor;
pCommandLine : array[0..MAX_PATH] of char;
pInputFile,
pOutPutFile : array[0..MAX_PATH] of char;
StartupInfo : TStartupInfo;
ProcessInfo : TProcessInformation;
SecAtrrs : TSecurityAttributes;
hAppProcess,
hAppThread,
hInputFile,
hOutputFile : THandle;
begin
Result := False;
{ Check for InputFile existence }
if not FileExists(InputFile)
then
raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 +
'Input file * %s *' + #10 +
'does not exist' + #10 + #10 +
ErrMsg, [InputFile]);
{ Save the cursor }
OldCursor := Screen.Cursor;
Screen.Cursor := crHourglass;
{ Copy the parameter Pascal strings to null terminated
strings }
StrPCopy(pCommandLine, CommandLine);
StrPCopy(pInputFile, InputFile);
StrPCopy(pOutPutFile, OutputFile);
TRY
{ Prepare SecAtrrs structure for the CreateFile calls.
This SecAttrs structure is needed in this case because
we want the returned handle can be inherited by child
process. This is true when running under WinNT.
As for Win95, the documentation is quite ambiguous }
FillChar(SecAtrrs, SizeOf(SecAtrrs), #0);
SecAtrrs.nLength := SizeOf(SecAtrrs);
SecAtrrs.lpSecurityDescriptor := nil;
SecAtrrs.bInheritHandle := True;
{ Create the appropriate handle for the input file }
hInputFile := CreateFile(
pInputFile,
{pointer to name of the file }
GENERIC_READ or GENERIC_WRITE,
{access (read-write) mode }
FILE_SHARE_READ or FILE_SHARE_WRITE,
{share mode }
@SecAtrrs,
{pointer to security attributes }
OPEN_ALWAYS,
{how to create }
FILE_ATTRIBUTE_NORMAL
or FILE_FLAG_WRITE_THROUGH,
{ file attributes }
0 ); {andle to file with attributes to copy }
{ Is hInputFile a valid handle? }
if hInputFile = INVALID_HANDLE_VALUE
then
raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 +
'WinApi function CreateFile returned an' +
'invalid handle value' + #10 +
'for the input file * %s *' + #10 + #10 +
ErrMsg, [InputFile]);
{ Create the appropriate handle for the output file }
hOutputFile := CreateFile(
pOutPutFile,
{pointer to name of the file }
GENERIC_READ or GENERIC_WRITE,
{access (read-write) mode }
FILE_SHARE_READ or FILE_SHARE_WRITE,
{share mode }
@SecAtrrs,
{pointer to security attributes }
CREATE_ALWAYS,
{ how to create }
FILE_ATTRIBUTE_NORMAL
or FILE_FLAG_WRITE_THROUGH,
{file attributes }
0 );
{handle to file with attributes to copy }
{ Is hOutputFile a valid handle? }
if hOutputFile = INVALID_HANDLE_VALUE
then
raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 +
'WinApi function CreateFile returned an' +
'invalid handle value' + #10 +
'for the output file * %s *' + #10 + #10 +
ErrMsg, [OutputFile]);
{ Prepare StartupInfo structure }
FillChar(StartupInfo, SizeOf(StartupInfo), #0);
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW or
STARTF_USESTDHANDLES;
StartupInfo.wShowWindow := SW_HIDE;
StartupInfo.hStdOutput := hOutputFile;
StartupInfo.hStdInput := hInputFile;
{ Create the app }
Result := CreateProcess(nil,
{ pointer to name of executable module }
pCommandLine,
{ pointer to command line string }
nil,
{ pointer to process security attributes }
nil,
{ pointer to thread security attributes }
True,
{ handle inheritance flag }
HIGH_PRIORITY_CLASS,
{ creation flags }
nil,
{ pointer to new environment block }
nil,
{ pointer to current directory name }
StartupInfo,
{ pointer to STARTUPINFO }
ProcessInfo);
{ pointer to PROCESS_INF }
{ wait for the app to finish its job and take the
handles to free them later }
if Result
then
begin
WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
hAppProcess := ProcessInfo.hProcess;
hAppThread := ProcessInfo.hThread;
end
else
raise Exception.Create(ROUTINE_ID + #10 + #10 +
'Function failure' + #10 + #10 +
ErrMsg);
FINALLY
{ Close the handles.
Kernel objects, like the process and the files
we created in this case, are maintained by a usage
count. So, for cleaning up purposes, we have to
close the handles to inform the system that we don't
need the objects anymore }
if hOutputFile <> 0 then CloseHandle(hOutputFile);
if hInputFile <> 0 then CloseHandle(hInputFile);
if hAppThread <> 0 then CloseHandle(hAppThread);
if hAppProcess <> 0 then CloseHandle(hAppProcess);
{ Restore the old cursor }
Screen.Cursor:= OldCursor;
END;
end; { CreateDOSProcessRedirected }
procedure TForm1.Button1Click(Sender: TObject);
var
t:tstringlist;
begin
t:=tstringlist.create;
t.SaveToFile('e:/temp/ttt1.txt'); // 以下执得 dir < ttt1.txt > ttt2.txt
CreateDOSProcessRedirected('cmd.exe /c dir ','e:/temp/ttt1.txt','e:/temp/ttt2.txt','');
t.LoadFromFile('e:/temp/ttt2.txt');
deletefile( 'e:/temp/ttt1.txt');
deletefile( 'e:/temp/ttt2.txt');
memo1.Lines:=t; //可对t处理后再显示
t.free;
end;
end.
- 管道重定向
- 重定向与管道
- 管道重定向符“|”
- 重定向 与 管道
- 管道重定向符“|”
- 管道和重定向
- linux重定向、管道
- 管道与重定向
- 重定向与管道
- 重定向和管道
- 管道和重定向
- 管道和重定向
- 管道和重定向
- 重定向和管道
- shell重定向,管道
- 管道与重定向
- linux 管道重定向
- 管道符、重定向
- 在b/s开发中经常用到的javaScript技术
- 宝洁招聘的题目
- 某"面霸"疯狂应聘IT名企的经历 另附试题
- 实习题目之一:订票系统(source)
- 猜数字的小程序源码
- 管道重定向
- Spring AOP [转贴]
- 一个算法源码
- 在C#中应用哈希表(Hashtable)
- Access 2003 与 MS SQL 2000 [sql]
- 北京的雪
- 时间真能忘记痛苦吗?
- 一些常用的vb函数/过程[操作FSO方面]
- vb操作Word[两个过程]