无法使用参数启动进程

时间:2016-08-03 16:41:59

标签: c++ winapi process

我尝试使用来自C ++应用程序的参数启动一个新进程,但它不起作用 - 它只是在没有这些参数的情况下启动进程

这是我的代码

void Launch(HWND hWnd) {
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    char bff[512];
    size_t size = strlen(bff) + 1;
    wchar_t* bff2 = new wchar_t[size];
    size_t outSize;
    memset(&si, 0, sizeof(si));
    si.cb = sizeof(si);
    sprintf_s(bff, "-rc:eu -lac:eng");
    mbstowcs_s(&outSize, bff2, size, bff, size-1);

    char* pathe = NULL;
    size_t size3 = MAX_PATH;
    pathe = _getcwd(pathe, size3);
    std::string myString(pathe);
    std::size_t found = myString.find_last_of("\\");

    wchar_t* txt = L"\\Client.exe";
    std::wstring stre(txt);
    std::wstring stemp = s2ws(myString) + stre;
    return;
    if (!CreateProcess(stemp.c_str(), bff2, NULL, NULL, 0, 0, NULL, NULL, &si, &pi)) {
        MessageBox(hWnd, L"Cannot launch Client!", L"Error", 0);
        return;
    }

    MessageBox(hWnd, L"Client should start soon!", L"Success!", 0);
}

由于

1 个答案:

答案 0 :(得分:0)

CreateProcess比旧的C约定更通用,可将可执行映像的名称作为argv[0]传递给新创建的进程。它允许您传递任意命令行参数。 lpApplicationName 指定可执行映像, lpCommandLine argv开始传递给argv[0]

通常不需要填充这两个参数。到目前为止,调用CreateProcess的最常用方法是将NULL作为lpApplicationName参数传递,并将整个命令行(包括正确引用的,完全限定的可执行映像的路径名)作为 lpCommandLine 参数。

你的电话基本上应该是这样的(伪代码):

CreateProcessW(NULL, L"\"<path to exe>\\Client.exe\" -rc:eu -lac:eng", ...);

<小时/> 注意:大多数字符串处理也是错误的。您应该始终使用wchar_t,更好的是std::wstring。除此之外,调用_getcwd是一个等待发生的错误。当前工作目录由进程中的所有线程共享。它可以随时更改 。永远不要使用它。而是查询当前正在执行的模块的路径,或从注册表中检索安装路径等。