Reading the output file of ShellExecute() in Delphi 2010?
I use the ShellExecute command to run an exe file which gets an input text file and returns an output text file. I've written it like this:
ShellExecute(mainFormHandle, 'open', 'Test.exe',
'input.txt output.txt', nil, sw_shownormal);
//Read the output file...
S_List.LoadFromFile('output.txt');
Writeln(S_List[0])
I provide the input.txt file before running this command. In each run of my program, the input file changes and so does the o开发者_开发问答utput file.
The problem is this: I can't see the changes in the output file! The line written in the console is from the previous file, not the newly changes one. I mean, the file in the explorer is changed but the file that I read is still the old file.
It seems a little weird, but I was wondering is there any way to refresh the output file before reading it? Or I am missing something here?
Thanks in advance.
ShellExecute
does not wait for your program to finish work. This is what happens:
- Test.exe starts
- you read in output.txt
- Test.exe writes new output.txt
Try something like this:
var
StartUpInfo : TStartUpInfo;
ProcessInfo : TProcessInformation;
CreationFlags : Cardinal;
begin
FillChar(StartUpInfo, SizeOf(TStartupInfo),0);
StartupInfo.cb := SizeOf(TStartupInfo);
CreationFlags := Normal_Priority_Class;
if CreateProcess(nil, 'test.exe input.txt output.txt',
nil, nil, False, CreationFlags,
nil, 0, StartupInfo, ProcessInfo) then
begin
WaitforSingleObject(ProcessInfo.HProcess, INFINITE);
CloseHandle(ProcessInfo.HProcess);
//Read the output file...
S_List.LoadFromFile('output.txt');
end;
With WaitForSingleObject
you can wait until a process finishes work.
精彩评论