c++ - CreateProcess,第一个参数问题

标签 c++ windows

我正在尝试创建一个名为 s2.exe 的进程。但是,我无法确定需要作为第一个参数传递的内容。我尝试输入进程所在位置的名称和位置,但我得到“错误 2 开始 CC”。第一个参数到底应该放什么? (根据 MSDN,它是模块的路径。)

int main()
{
    PROCESS_INFORMATION po;
    STARTUPINFO s;
    GetStartupInfo (&s);

    if(CreateProcess(L"c:/s2", NULL, NULL, NULL,
        false, 0, NULL, NULL, &s, &po) == FALSE)
    {
         printf("Error %d starting CC\n", GetLastError());
         return -1;
    }
}

最佳答案

GetLastError() 返回的值 2 表示 ERROR_FILE_NOT_FOUND。需要传入c:/s2.exe,假设s2.exe实际在c:\盘中。

请注意 CreateProcess()lpApplicationName 参数的 MSDN 文档说:

The string can specify the full path and file name of the module to execute or it can specify a partial name. In the case of a partial name, the function uses the current drive and current directory to complete the specification. The function will not use the search path. This parameter must include the file name extension; no default extension is assumed.


另请注意,void main() 不是标准的 C++。但是,int main() 是标准的 C++,允许您返回某种退出代码。 (我编辑了您的代码片段以反射(reflect)这一点。)

关于c++ - CreateProcess,第一个参数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4777259/

相关文章:

c++ - C++ 中的无分支 if 是什么样子的?

c++ - 在 C++ 中,将结构从被调用函数返回到调用函数的适当方式是什么?

容器化 Linux 环境中的 C++ : why does attempting to allocate large vector causes SIGABRT or neverending loop instead of bad_alloc?

c++ - 如何在 Visual C++ 2010 Express 中更改编译器

c++ - 运算符重载 - 创建一个新的 String 类

c++ - 是否可以从 win32 或 MFC 应用程序中的窗口获取 "Low Disk Space"通知?

windows - 获取 Dos (8.3) 文件名

windows - Kafka 无法在 Windows 上启动 - 未找到 key :\tmp\kafka-logs

windows - ubuntu 上的 windows virtualbox 或 windows 上的 ubuntu virtualbox 具有更好的性能

c++ - 用新的 QWidget 替换 QWidget