我必须承认我在这段代码中不熟悉的大部分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);
}
答案 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()
。