将字符串数组传递给CreateProcess for for循环以进行多个进程

时间:2012-06-08 17:49:28

标签: c++ c winapi

我必须承认我在这段代码中不熟悉的大部分win32 api内容。话虽如此,我想将我所知道的内容融入到我的学习过程中。我试图创建一个for循环,每次CreateProcess多次使用不同的参数。在Visual Studio中,我收到编译错误:

source.cpp(138): error C3867: 'std::basic_string<_Elem,_Traits,_Alloc>::c_str': function call        missing argument list; use '&std::basic_string<_Elem,_Traits,_Alloc>::c_str' to create a     pointer to member
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Alloc=std::allocator<char>
1>          ]

运行以下代码时:

std::string arrString[3] = {"dir","cd ..","dir"};
            int i;
            LPWSTR cmd =L"cmd";

            for(i=0; i<3; i++)
            {
            STARTUPINFO info={sizeof(info)};
            PROCESS_INFORMATION processInfo;
            if (CreateProcess(cmd, arrString[i].c_str, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
            {
                ::WaitForSingleObject(processInfo.hProcess, INFINITE);
                CloseHandle(processInfo.hProcess);
                CloseHandle(processInfo.hThread);
            }

我是否朝着正确的方向前进?

修改

            std::string arrString[3] = {"cmd","cmd","cmd"};
            int i;
            LPWSTR cmd =L"cmd";

            for(i=0; i<3; i++)
            {
                STARTUPINFO info={sizeof(info)};
                PROCESS_INFORMATION processInfo;
                vector<wchar_t> cmdline(arrString[i].begin(), arrString[i].end()); 
                CreateProcessW(cmd, &cmdline[0], NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo);
                    ::WaitForSingleObject(processInfo.hProcess, INFINITE);
                    CloseHandle(processInfo.hProcess);
                    CloseHandle(processInfo.hThread);

            }

1 个答案:

答案 0 :(得分:3)

在这一行

if (CreateProcess(cmd, arrString[i].c_str, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))

您在写arrString[i].c_str时写了arrString[i].c_str()