createProcess()中的argv与普通c++程序不同的原因
why argv in createProcess() is different from normal c++ program
这是我的代码,我发现第一个输出是"thisProgram.exe"第二个输出是"a"。
为什么?
我读了msdn中的文档,但我不太清楚为什么argv[0]可以是"a",在使用createProcess时,windows中是否有不同之处。有人能告诉我lpApplicationName和lpCommandline的区别吗?感谢
int main( int argc, char *argv[] ) {
cout << argv[0] << endl;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// Start the child process.
if (!CreateProcess("thisProgram.exe", // No module name (use command line)
"a b c", // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
) {
printf("CreateProcess failed (%d).n", GetLastError());
return 1;
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
CreateProcess
将第二个参数(命令行)作为命令行传递给新进程。CreateProcess
不会在模块名称前加上前缀。如果希望应用程序名称显示为argv[0]
,则必须在命令行参数中重复应用程序名称。
文件上是这样写的:
如果lpApplicationName和lpCommandLine都不是NULL,则lpApplicationName指向的以NULL结尾的字符串指定要执行的模块,而lpCommandLine指向的以NULL结尾的字符串则指定命令行。新进程可以使用GetCommandLine来检索整个命令行。用C编写的控制台进程可以使用argc和argv参数来解析命令行。由于argv[0]是模块名称,C程序员通常会将模块名称作为命令行中的第一个标记重复使用。
通常最简单的方法是为应用程序名称传递NULL
,为命令行传递连接在一起的应用程序名称和参数。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- 使用mongocxx驱动程序时包含头文件问题
- 如何在c++程序中找到函数的地址