doctorjellybean
Programmer
- May 5, 2003
- 145
I have several DOS batch files which I want to create applications for. What I basically need, are 2 things:
1) Execute a DOS command from within the application
2) Display the output in a Memo instead of a console window
I Googled a bit, and found this article:
The code (as used in this article), using a Memo and Button:
If I use this example, the following appears in the memo:
If I run chkdsk in a normal command window, it works.
Is there something wrong or missing from the above code?
1) Execute a DOS command from within the application
2) Display the output in a Memo instead of a console window
I Googled a bit, and found this article:
The code (as used in this article), using a Memo and Button:
Code:
procedure 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
With Security do begin
nlength := SizeOf(TSecurityAttributes) ;
binherithandle := true;
lpsecuritydescriptor := nil;
end;
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 {button 1 code}
RunDosInMemo('chkdsk.exe c:\',Memo1) ;
end;
If I use this example, the following appears in the memo:
The drive, the path, or the file name is not valid.
If I run chkdsk in a normal command window, it works.
Is there something wrong or missing from the above code?