当我使用 ShellExecute 时,为什么我的命令没有像我预期的那样运行?
我正在尝试使用命令行实用程序(它适用于来自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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 tryCreateProcess
instead. There are several examples of how to do that. Try this:事实证明,将填充路径添加到可执行文件使其工作得很好
编辑:一些代码清理有助于尽早发现错误,特别是在测试概念证明时。
It turns out, that adding the fill path to the execuatble made it work just fine
Edit: Some code cleanup helps big time to catch errors early on, especially when testing a proof of concept.