在Delphi 2010中读取ShellExecute()的输出文件?

发布于 2024-09-07 20:32:29 字数 497 浏览 1 评论 0原文

我使用 ShellExecute 命令运行一个 exe 文件,该文件获取输入文本文件并返回输出文本文件。我是这样写的:

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])

我在运行此命令之前提供了 input.txt 文件。在我的程序的每次运行中,输入文件都会发生变化,输出文件也会发生变化。

问题是这样的:我看不到输出文件中的更改!控制台中写入的行来自前一个文件,而不是新更改的文件。我的意思是,资源管理器中的文件已更改,但我读取的文件仍然是旧文件。

这看起来有点奇怪,但我想知道有没有办法在读取输出文件之前刷新它?或者我在这里遗漏了一些东西?

提前致谢。

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 output 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぽ尐不点ル 2024-09-14 20:32:29

ShellExecute 不会等待您的程序完成工作。发生的情况是这样的:

  • Test.exe 开始
  • 读取 output.txt
  • Test.exe 写入新的 output.txt

尝试如下操作:

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;

使用 WaitForSingleObject 您可以等待进程完成工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文