gpt4 book ai didi

c++ - 如何将 argv 转换为 CreateProcess 的 lpCommandLine 参数?

转载 作者:行者123 更新时间:2023-11-30 20:44:01 27 4
gpt4 key购买 nike

让我想编写一个应用程序来启动另一个应用程序。像这样:

# This will launch another_app.exe
my_app.exe another_app.exe
# This will launch another_app.exe with arg1, arg and arg3 arguments
my_app.exe another_app.exe arg1 arg2 arg3

这里的问题是我在 main 函数中获取了 char* argv[],但我需要将其合并到 LPTSTR 以便将其传递给 CreateProcess

有一个 GetCommandLine 函数,但我无法使用它,因为我正在从 Linux 移植代码并绑定(bind)到 argc/argv (否则,这是一个非常难看的 hack对我来说)。

我无法轻松地手动合并参数,因为 argv[i] 可能包含空格。

基本上,我想要的是 CommandLineToArgvW 的相反内容。有没有标准方法来做到这一点?

最佳答案

有关如何引用论点的明确答案位于 Daniel Colascione 的博客上:

https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/

我不愿意在这里引用代码,因为我不知道许可证。基本思想是:

for each single argument:
if it does not contain \t\n\v\",
just use as is
else
output "
for each character
backslashes = 0
if character is backslash
count how many successive backslashes there are
fi
if eow
output the backslashs doubled
break
else if char is "
output the backslashs doubled
output \"
else
output the backslashes (*not* doubled)
output character
fi
rof
output "
fi // needs quoting
rof // each argument

如果您需要将命令行传递给cmd.exe,请参阅文章(有所不同)。

我认为 Microsoft C 运行时库没有执行此操作的函数,这太疯狂了。

关于c++ - 如何将 argv 转换为 CreateProcess 的 lpCommandLine 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31838469/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com