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

发布于 2024-10-11 03:07:41 字数 603 浏览 2 评论 0原文

我正在尝试使用命令行实用程序(它适用于来自 dos 命令行的测试)从我的 Delphi 代码将 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;

在代码中放置断点并单步执行时,它到达了该

if fileExists(changeFileExt(fFileName, '.txt')) then  

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

我做错了什么?

I am trying to dump a PDF to text using a command line utility (It works with tests from dos command line) from my Delphi code.

Here is my code

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;

When placing breakpoints in code and stepping through, it makes it to the

if fileExists(changeFileExt(fFileName, '.txt')) then  

line but returns false, so the Shellexecute was called but no file was ever dumped

What have I done wrong?

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

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

发布评论

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

评论(2

戏剧牡丹亭 2024-10-18 03:07:41

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

运行程序并等待其终止,然后再检查输出文件。 ShellExecute 没有返回足够的信息来执行此操作,因此您应该尝试 CreateProcess。有几个例子说明了如何做到这一点。试试这个:

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

ShellExecute doesn't wait for the invoked program to finish running. You're probably checking for the file too soon. The file simply hasn't been created yet.

Run the program and wait for it to terminate before you check for the output file. ShellExecute doesn't return enough information for you to do that, so you should try CreateProcess instead. There are several examples of how to do that. Try this:

How can I wait for a command-line program to finish?

夢归不見 2024-10-18 03:07:41

事实证明,将填充路径添加到可执行文件使其工作得很好

uses
  Forms, ShellAPI, SysConst, SysUtils;

procedure Pdf2Text(const fFileName: string; const Lines: TStrings);
var
  H: HWND;
  PdfToTextPathName: string;
  ReturnValue: Integer;
  TxtFileName: string;
begin
  H := 0;
  PdfToTextPathName := ExtractFilePath(Application.ExeName) + 'pdftotext.exe'; // full path
  if FileExists(PdfToTextPathName) then
  begin
    ReturnValue := ShellExecute(0,'open', PWideChar(PdfToTextPathName), PWideChar(fFileName), nil, SW_SHOWNORMAL);
    if ReturnValue <= 32 then
      RaiseLastOsError();
    // note: the code below this line will crash when pdftotext.exe does not finish soon enough; you should actually wait for pdftotext.exe completion
    TxtFileName := ChangeFileExt(fFileName, '.txt');
    if FileExists(TxtFileName) then
      Lines.LoadFromFile(TxtFileName)
    else
      raise EFileNotFoundException.CreateRes(@SFileNotFound);
  end;
end;

编辑:一些代码清理有助于尽早发现错误,特别是在测试概念证明时。

It turns out, that adding the fill path to the execuatble made it work just fine

uses
  Forms, ShellAPI, SysConst, SysUtils;

procedure Pdf2Text(const fFileName: string; const Lines: TStrings);
var
  H: HWND;
  PdfToTextPathName: string;
  ReturnValue: Integer;
  TxtFileName: string;
begin
  H := 0;
  PdfToTextPathName := ExtractFilePath(Application.ExeName) + 'pdftotext.exe'; // full path
  if FileExists(PdfToTextPathName) then
  begin
    ReturnValue := ShellExecute(0,'open', PWideChar(PdfToTextPathName), PWideChar(fFileName), nil, SW_SHOWNORMAL);
    if ReturnValue <= 32 then
      RaiseLastOsError();
    // note: the code below this line will crash when pdftotext.exe does not finish soon enough; you should actually wait for pdftotext.exe completion
    TxtFileName := ChangeFileExt(fFileName, '.txt');
    if FileExists(TxtFileName) then
      Lines.LoadFromFile(TxtFileName)
    else
      raise EFileNotFoundException.CreateRes(@SFileNotFound);
  end;
end;

Edit: Some code cleanup helps big time to catch errors early on, especially when testing a proof of concept.

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