fOpen 带参数无法正常工作

发布于 2025-01-02 06:58:50 字数 1609 浏览 1 评论 0原文

晚上好!

我目前正处于 Firemonkey 中新应用程序开发的第一阶段。这是我第一次使用 Firemonkey,但我已经有几年使用 Delphi 的经验了。

到目前为止,我已经有了一个外部应用程序,它可以从 exe 中提取图标,然后将其作为 BMP 保存到文件中。我已经编写了将参数传递给该应用程序的功能(它提供了一个字符串值来告诉它要从中提取的 .exe 在哪里)。它是用 VCL 构建的,因为我想主要使用 firemonkey 框架来实现 3D 功能,但如果我愿意的话,也允许我在将来扩展到 Mac 上。外部应用程序工作正常,因此不需要调整其中的代码。

我遇到的问题是从我的 firemonkey 应用程序传递参数。我正在使用自定义的“fOpen”单元,它允许传递参数;

unit fOpen;

interface

uses
{$IFDEF MSWINDOWS}
Winapi.ShellAPI, Winapi.Windows;
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
  Posix.Stdlib;
{$ENDIF POSIX}

type
  TMisc = class
class procedure Open(sCommand, Params: string);
end;
implementation

class procedure TMisc.Open(sCommand, Params: string);
begin
{$IFDEF MSWINDOWS}
  ShellExecute(0, 'OPEN', PChar(sCommand), PChar(Params), '', SW_SHOWNORMAL);
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
  _system(PAnsiChar('open ' + AnsiString(sCommand)));
{$ENDIF POSIX}
end;
end.

正如您所看到的,我已将“Params”变量添加到 MSWINDOWS isdef 中。

在我的 firemonkey 表单上,有 2 个按钮。其中 1 个执行 TOpenDialog 并将所选文件名输出到 TEdit 中。另一个按钮执行外部应用程序(提取图标的应用程序),然后加载保存到 TImageControl 中的位图图像。

这是我尝试使用的代码;

fOpen.TMisc.Open('HypExIcon.exe', '"' + edit1.Text +'"');

问题在于,应该使用参数执行文件的第一行没有实际上并没有按预期工作。它甚至根本不执行该文件(即没有迹象表明应用程序应该启动)。但是,如果我将其更改为...

fOpen.TMisc.Open('HypExIcon.exe', 'C:\Windows\Notepad.exe');

...它的功能如下预期,执行外部应用程序并创建参数字段中包含的图标。我尝试过使用 PChar('"' + Edit1.text + '"') 以及类似的 PWideChar 函数,但它们都无法解决问题。

我相信这是我在这里缺少的非常简单的东西。我非常习惯在 VCL 中使用 PCharPWideChar 来处理此类事情,这让我感到困惑。

有什么想法吗?

Good evening!

I'm currently in the first stages of development with a new application in Firemonkey. It's my first run in with Firemonkey, but i've got a few years experience with Delphi.

So far, i've got an external application that extracts an icon from an exe and then saves it to a file as a BMP. I've coded in the ability to pass a parameter to this application (which provides a string value to tell it where the .exe to extract from is). It was built in VCL as i want to use the firemonkey framework mostly for the 3D features, but also allow me expansion onto Mac at a future date if i so wish. The external application is working correctly, so the code there doesn't need tweaking.

The issue i'm having is passing the parameter from my firemonkey application. I'm using a customised "fOpen" unit which allows passing of parameters;

unit fOpen;

interface

uses
{$IFDEF MSWINDOWS}
Winapi.ShellAPI, Winapi.Windows;
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
  Posix.Stdlib;
{$ENDIF POSIX}

type
  TMisc = class
class procedure Open(sCommand, Params: string);
end;
implementation

class procedure TMisc.Open(sCommand, Params: string);
begin
{$IFDEF MSWINDOWS}
  ShellExecute(0, 'OPEN', PChar(sCommand), PChar(Params), '', SW_SHOWNORMAL);
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
  _system(PAnsiChar('open ' + AnsiString(sCommand)));
{$ENDIF POSIX}
end;
end.

As you can see, i've added the "Params" variable to the MSWINDOWS isdef.

On my firemonkey form, there's 2 buttons. 1 of them executes a TOpenDialog and outputs the selected filename into a TEdit. The other button executes the external application (the one that extracts the icon) and then loads the bitmap image that was saved into a TImageControl.

Here's the code i'm trying to use;

fOpen.TMisc.Open('HypExIcon.exe', '"' + edit1.Text +'"');

The issue is that the first line, which should execute the file with the parameter, doesn't actually work as intended. It doesn't even execute the file at all (i.e. there's no indication of the application launching as there should be). However, if i change it to this...

fOpen.TMisc.Open('HypExIcon.exe', 'C:\Windows\Notepad.exe');

...it functions as would be expected, executing the external application and creating the icon included in the parameter field. I've tried using PChar('"' + Edit1.text + '"'), as well as a similar PWideChar function, but neither of them remedy the issue.

I'm confident it's something extremely simple that i'm missing here. I'm so used to using PChar or PWideChar for these sorts of things in VCL, that it's thrown me off.

Any ideas?

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

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

发布评论

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

评论(1

娇柔作态 2025-01-09 06:58:50

正如我们在评论中解决的那样,问题在于文件对话框正在背后更改工作目录。您可以而且应该设置 ofNoChangeDir 选项。

但是,我建议您在启动进程时不要依赖相对路径。启动进程时,您确实应该严格控制进程的位置。我会这样做:

AppPath := ExtractFilePath(Application.ExeName);
fOpen.TMisc.Open(AppPath+'HypExIcon.exe', ...

As we worked out in the comments, the problem is that the file dialog is changing the working directory behind your back. You can, and should, set the ofNoChangeDir option.

However, I recommend that you do not rely on relative paths when starting processes. When starting processes you really ought to maintain tight control over the location of the process. I would do it like this:

AppPath := ExtractFilePath(Application.ExeName);
fOpen.TMisc.Open(AppPath+'HypExIcon.exe', ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文