windows - QStringList QCoreApplication::arguments() - 在什么情况下索引 0 不是 Windows 中的应用程序路径?

标签 windows qt

QStringList QCoreApplication::arguments() (Qt) 的文档指出:

Usually arguments().at(0) is the program name, arguments().at(1) is the first argument, and arguments().last() is the last argument. See the note below about Windows.

进一步阐述:

On Windows, [...] the arguments() are constructed from the return value of GetCommandLine(). As a result of this, the string given by arguments().at(0) might not be the program name on Windows, depending on how the application was started.

关于最后一个粗体部分,我很好奇这适用于哪种情况。Qt 文档没有解释它,GetCommandLine 也没有解释。的 (WINAPI) 文档。

最佳答案

当您的程序通过 CreateProcess 生成时,可能会发生这种情况- 查看lpCommandLine 参数的说明。基本上,由调用 CreateProcess 的程序来填充第一个参数,因此可能会出现未以通常方式填充值的情况。

来自链接:

"如果lpApplicationName和lpCommandLine都非NULL,则lpApplicationName指向的以null结尾的字符串指定要执行的模块,lpCommandLine指向的以null结尾的字符串指定命令行。新进程可以使用GetCommandLine检索整个命令行。用 C 编写的控制台进程可以使用 argc 和 argv 参数来解析命令行。因为 argv[0] 是模块名称,所以 C 程序员通常将模块名称重复为命令行中的第一个标记”

同样,如果您的程序是使用 spawn 从另一个程序启动的,则可能会发生这种情况。函数族。在那里,文档指出:

“至少有一个参数,arg0 或 argv[0],必须传递给子进程。按照惯例,这个参数是路径名参数的副本。但是,不同的值不会产生错误。”

关于windows - QStringList QCoreApplication::arguments() - 在什么情况下索引 0 不是 Windows 中的应用程序路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3304845/

相关文章:

ruby - Shell 从 Ruby 执行

C# 将位图转换为索引颜色格式

c++ - 在 soap_ssl_server_context(...) 中使用继承时 gsoap 中的段错误

c++ - 精确的信号发生器

c++ - 替换 QDockWidget 的小部件而不删除以前的

c++ - 显示未显示在 qt 中其他类的文本框中

c - 在 Windows 平台上,C 代码中的临时重定向 stderr 为 null。

c# - 在 SQL Server 或 C# 中加密数据

windows - 为另一个用户运行 Windows 应用程序

c++ - 诺基亚是否误用了 static_cast?