2009-07-31 43 views

Trả lời

13

Điều gì về điều này solution.


EDIT: liên kết dẫn đến giải pháp này (nhẹ refactored cho mức độ dễ đọc và để loại bỏ sử dụng with):

// The example runs 'chkdsk.exe c:\' and displays the output to Memo1. 
// Put a TMemo (Memo1) and a TButton (Button1) on your form. Put this 
// code in the OnCLick event procedure for Button1: 

procedure TForm1.RunDosInMemo(DosApp:String;AMemo:TMemo) ; 
const 
    ReadBuffer = 2400; 
var 
    Security : TSecurityAttributes; 
    ReadPipe, 
    WritePipe : THandle; 
    start  : TStartUpInfo; 
    ProcessInfo : TProcessInformation; 
    Buffer  : Pchar; 
    BytesRead : DWord; 
    Apprunning : DWord; 

begin 
    Security.nlength := SizeOf(TSecurityAttributes) ; 
    Security.binherithandle := true; 
    Security.lpsecuritydescriptor := nil; 
    if Createpipe (ReadPipe, WritePipe, @Security, 0) then 
    begin 
    Buffer := AllocMem(ReadBuffer + 1) ; 
    FillChar(Start,Sizeof(Start),#0) ; 
    start.cb := SizeOf(start) ; 
    start.hStdOutput := WritePipe; 
    start.hStdInput := ReadPipe; 
    start.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW; 
    start.wShowWindow := SW_HIDE; 
    if CreateProcess(nil, 
        PChar(DosApp), 
        @Security, 
        @Security, 
        true, 
        NORMAL_PRIORITY_CLASS, 
        nil, 
        nil, 
        start, 
        ProcessInfo) then 
    begin 
     repeat 
     Apprunning := WaitForSingleObject(ProcessInfo.hProcess,100); 
     Application.ProcessMessages; 
     until (Apprunning <> WAIT_TIMEOUT) ; 
     repeat 
     BytesRead := 0; 
     ReadFile(ReadPipe,Buffer[0], 
     ReadBuffer,BytesRead,nil) ; 
     Buffer[BytesRead]:= #0; 
     OemToAnsi(Buffer,Buffer) ; 
     AMemo.Text := AMemo.text + String(Buffer) ; 
     until (BytesRead < ReadBuffer) ; 
    end; 
    FreeMem(Buffer) ; 
    CloseHandle(ProcessInfo.hProcess) ; 
    CloseHandle(ProcessInfo.hThread) ; 
    CloseHandle(ReadPipe) ; 
    CloseHandle(WritePipe) ; 
    end; 
end; 

procedure TForm1.Button1Click(Sender: TObject) ; 
begin 
    RunDosInMemo('chkdsk.exe c:\', Memo1) ; 
end; 
+3

đề xuất tốt đẹp cho những người theo liên kết, nhưng một liên kết không phải là câu trả lời thực sự tốt, hãy xem: http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links -elsewhere-really-good-answers – Argalatyr

+0

Tôi đã đăng mã từ liên kết. – Mick

+0

@Mick: oops - trong khi bạn đang làm điều đó trong một câu trả lời mới, tôi đã chỉnh sửa cũ. Nếu ai đó cảm thấy chỉnh sửa của tôi nên được cuộn lại, hãy làm như vậy. – Argalatyr

2

Tôi thường sử dụng này chuyển đang FPC: http://www.stack.nl/~marcov/processdelphi.zip

Nó chứa một lớp để kiểm soát các chương trình bên ngoài (nó là lớp được sử dụng bởi Lazarus để gọi trình biên dịch cmdline và các chương trình khác).

Tài liệu ở đây, nhưng cổng delphi hơi cũ, vì vậy không phải tất cả các thuộc tính được ghi lại có thể tồn tại trong phiên bản trên.

http://www.freepascal.org/docs-html/fcl/process/index.html

+0

Điều này có vẻ tuyệt vời! –

+0

Trong các phiên bản FPC gần đây, một vài thủ tục đơn giản sửa một số kịch bản đơn giản nhất đã được thêm vào dưới dạng "RunCommand() –

1

Chỉ cần một bổ sung nhỏ để Marco câu trả lời, bằng cách sử dụng đơn vị TProcess được giải thích kỹ lưỡng here

Tôi tin rằng đây là cách dễ nhất để làm điều đó. Chúc may mắn!

Các vấn đề liên quan