当我使用ShellExecute时,为什么我的命令不像我预期的那样运行?

IEl*_*ite -2 delphi winapi command-line shellexecute delphi-2010

我试图从我的Delphi代码使用命令行实用程序(它与dos命令行中的测试一起使用)将PDF转储到文本.

这是我的代码

if fileexists(ExtractFilePath(Application.ExeName) + 'pdftotext.exe') then
begin
  ShellExecute(H,'open', 'pdftotext.exe', PWideChar(fFileName), nil, SW_SHOWNORMAL);
  if fileExists(changeFileExt(fFileName, '.txt')) then
    Lines.LoadFromFile(changeFileExt(fFileName, '.txt'))
  else
    ShowMessage('File Not found');
end;
Run Code Online (Sandbox Code Playgroud)

在代码中放置断点并单步执行时,它会使用它

if fileExists(changeFileExt(fFileName, '.txt')) then  
Run Code Online (Sandbox Code Playgroud)

但是返回false,因此调用了Shellexecute,但是没有转储任何文件

我做错了什么?

Rob*_*edy 7

ShellExecute不等待被调用的程序完成运行.您可能过早检查文件.该文件尚未创建.

在检查输出文件之前运行程序并等待它终止.ShellExecute没有返回足够的信息让你这样做,所以你应该尝试CreateProcess.有几个例子说明如何做到这一点.试试这个:

我如何等待命令行程序完成?