Ian*_*Ian 3 c++ windows createprocess
我目前正在尝试将CreateProcess与Path,Arguments和Environment Variables一起使用.我的变量存储在字符串中.
在下面的示例中,filePath和cmdArgs工作正常,但我无法使envVars工作.
std::string filePath = "C:\\test\\DummyApp.exe";
std::string cmdArgs = "Arg1 Arg2 Arg3";
std::string envVars = "first=test\0second=jam\0"; // One
//LPTSTR testStr = "first=test\0second=jam\0"; // Two
CreateProcess(
LPTSTR(filePath.c_str()), //path and application name
LPTSTR(cmdArgs.c_str()), // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
TRUE, // Set handle inheritance
0, // Creation flags
LPTSTR(envVars.c_str()), // environment block
//testStr //this line works
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
Run Code Online (Sandbox Code Playgroud)
)
当我运行此代码时,返回的错误是"错误87:参数不正确".
我不明白的是,如果我注释掉标记为"one"的行并将其替换为标记为"two"的行(并在函数调用中进行匹配交换),那么它可以正常工作.
std::string您使用的构造函数将复制"first=test\0second=jam\0"到第一个\0(C样式字符串).
要传递所有字符串,请使用另一个构造函数:
std::string envVars("first=test\0second=jam\0", 22);
^^^^^^^^^^^^^^^^^^^^^^^^ ^
|
22 characters -------+
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2442 次 |
| 最近记录: |