简体   繁体   English

无法在CreateProcess中传递命令行参数

[英]Fail to pass command line arguments in CreateProcess

I'm having trouble using CreateProcess with command line arguments. 我在使用带有命令行参数的CreateProcess时遇到麻烦。 I've read to all the posts I've found but none of the solutions did work. 我已经阅读了所有找到的帖子,但是没有一种解决方案有效。

Here's what I have: 这是我所拥有的:

std::string path = "C:\\my\\path\\myfile.exe";
std::wstring stemp = std::wstring(path.begin(), path.end());
LPCWSTR path_lpcwstr = stemp.c_str();

std::string params = " Param1 Param2 Param3";
STARTUPINFO info = { sizeof(info) };
PROCESS_INFORMATION processInfo;
CreateProcess(path_lpcwstr, LPTSTR(params.c_str()), NULL, NULL, TRUE, CREATE_NEW_PROCESS_GROUP, NULL, NULL, &info, &processInfo);

The code works and myfile.exe (a QT Application) is opened, but argc is always 1. I've also tried specifying only the first parameter as "C:\\my\\path\\myfile.exe Param1 Param2 Param3" but that didn't work either. 代码可以正常工作,并且打开了myfile.exe(一个QT应用程序),但是argc始终为1。我也尝试过仅将第一个参数指定为“ C:\\ my \\ path \\ myfile.exe Param1 Param2 Param3”,但是没有也不行。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Solution: Using CreateProcessA and change the parameters accordingly fixed the problem pointed out by one of the answers. 解决方案:使用CreateProcessA并相应地更改参数可以解决答案之一指出的问题。

STARTUPINFOA info = { sizeof(info) };
PROCESS_INFORMATION processInfo;
std::string path = "C:\\my\\path\\myfile.exe";
std::string params = " Param1 Param2 Param3";
CreateProcessA(path.c_str(), const_cast<char *>(config.c_str()) , NULL, NULL, TRUE, CREATE_NEW_PROCESS_GROUP, NULL, NULL, &info, &processInfo);

There are two versions of CreateProcess (and many other Winapi functions too): 有两种版本的CreateProcess (以及许多其他Winapi函数):

One takes "normal" strings in ASCII/ISO88591/whatever where each character has 1 byte. 无论每个字符有1个字节,无论是ASCII / ISO88591 /中的“正常”字符串,都是这样。
"abc" would have the numbers 97 98 99 . “ abc”的数字为97 98 99

The other CreateProcess takes UTF16 strings; 另一个CreateProcess采用UTF16字符串; each char has 2 or 4 byte there, 每个字符在那里有2或4个字节,
and "abc" would have the byte numbers 0 97 0 98 0 99 和“ abc”的字节号为0 97 0 98 0 99
(UTF16 is a bit more complicated, but in this case, it´s just 0s added). (UTF16有点复杂,但是在这种情况下,它仅添加了0)。
The advantage is better support for internationalization, because the 优势是可以更好地支持国际化,因为
old 1-byte charsets are problematic with languages like Russian, Greek etc. 旧的1字节字符集在诸如俄语,希腊语等语言方面存在问题。

You´re using the second version. 您正在使用第二个版本。 path_lpcwstr , ie. path_lpcwstr ,即。 the program path and name as first parameter, is correctly provided as UTF16 string by you ( std::wstring on Windows and LPCWSTR etc. ...). 程序路径和名称作为第一个参数,已由您正确地作为UTF16字符串提供(在Windows和LPCWSTR等上为std::wstring ...。)。

However, the second parameter with the arguments for the new process, is not UTF16 in your code (but a one-byte charset) and to avoid a compiler error, you are simply casting a pointer and telling the compiler to treat the not-UTF16 content as UTF16. 但是,带有新过程参数的第二个参数在代码中不是UTF16(而是一个字节的字符集),并且为了避免编译器错误,您只是在投射一个指针并告诉编译器对待not-UTF16内容为UTF16。
The bytes of " Param1 Param2 Param3" understood as UTF16 won´t give any sane string without proper conversion, and to start with, the 2 byte 0 value to terminate the string, as requried by Windows, is nowhere in there. 如果没有适当的转换,被理解为UTF16的“ Param1 Param2 Param3”字节将不会给出任何理智的字符串,并且开始时,Windows要求的2字节 0值来终止该字符串,就在那里。 The result is undefined behaviour, any strange things can happen. 结果是不确定的行为,任何奇怪的事情都可能发生。

Make you parameter string like you did with the path, and everything should be fine. 使参数字符串像使用路径一样,一切都应该没问题。

您是否尝试过ShellExecuteA()?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM