delphi - 为什么当应用程序由 IDE 与启动器启动时,命令行参数的数量会发生变化?

标签 delphi command-line-arguments delphi-xe2 delphi-ide

考虑以下命令行参数

"alfa" "beta" "4"

当我为我正在处理的项目指定运行>参数...时,应用程序将在 Process Explorer 上显示为命令行:

"c:\myapp\myapp.exe" "alfa" "beta" "4"

ParamCount 显示 4 个参数。但是,当我从启动器应用程序(执行访问控制)启动相同的可执行文件时,Process Explorer 显示:

"alfa" "beta" "4"

ParamCount 显示 3 个参数。 命令行是从启动器应用程序中提取的。 理论上它是可行的,因为当从启动器启动时,应用程序可以完美地工作。从 IDE 启动时,它尝试对上面的 "4" 执行 StrToInt,但仅检索 "beta" 参数。

启动器应用程序的示例代码:

var
  StartupInfo: TSTARTUPINFO;
  ProcessInfo: PROCESS_INFORMATION;
  CurrentDirPath: String;
begin
  Result := 0;
  ZeroMemory(@StartupInfo, SizeOf(StartupInfo));
  StartupInfo.cb := SizeOf(StartupInfo);
  DirCorrente := ExtractFilePath(sExe);

  if CreateProcess(PChar(sExe), PChar(sParam), nil, nil, true,
    NORMAL_PRIORITY_CLASS, nil, PChar(CurrentDirPath),
    StartupInfo, ProcessInfo) then

sParam的内容是上面的命令行参数,sExe是可执行路径。 为什么会出现这种情况?

注意:我已经设计了如何更改命令行参数解释,使其对于这种边缘情况保持稳健 - 这里的要点是为什么 这种情况就会发生。

最佳答案

您的启动程序未正确调用CreateProcess。考虑一下 the documentation 的摘录(强调):

If both lpApplicationName and lpCommandLine are non-NULL, the null-terminated string pointed to by lpApplicationName specifies the module to execute, and the null-terminated string pointed to by lpCommandLine specifies the command line. The new process can use GetCommandLine to retrieve the entire command line. Console processes written in C can use the argc and argv arguments to parse the command line. Because argv[0] is the module name, C programmers generally repeat the module name as the first token in the command line.

忽略有关“C 程序员”的内容;它适用于为 Windows 编写程序的每个人,无论使用何种语言。

您的启动器正在为 lpApplicationName 和 lpCommandLine 参数提供值,但它不遵循将程序文件名重复作为命令行中第一个参数的约定。 Delphi 的 ParamStrParamCount 函数知道遵循约定,因此它们跳过命令行上的第一个标记。如果调用者没有遵循约定,那么接收者最终会认为预期的第二个参数实际上是第一个,第三个参数实际上是第二个,依此类推。

关于delphi - 为什么当应用程序由 IDE 与启动器启动时,命令行参数的数量会发生变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32236632/

相关文章:

windows - 在 Delphi 中下载文件

c++ - 命令行参数 - int 而不是 ascii

python - Docopt 选项配置中的文本如何换行?

delphi - Delphi XE2:如何在设计时使属性可设置?

c - N个十进制数需要多少字节

oracle - 报表生成器-多种语言的报表

delphi - TidHttpServer (Delphi XE2) 可以处理 urlencoded 字符吗?

delphi-xe2 - 是否可以制作 TCombo 编辑插入符 'wider' 或 'bold' ?

performance - 从一个数组中减去另一个数组的最高效方法

java - jar是否区分程序参数和jvm参数