1

我不得不承认,这段代码中我不熟悉的大部分 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);

            }
4

1 回答 1

3

在这一行

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

arrString[i].c_str你是按你的意思写arrString[i].c_str()的。

于 2012-06-08T17:52:48.457 回答