CreateProcess示例 – 注意si的初始化和buffer

作者:半瓶墨水   链接:http://www.2maomao.com/blog/createprocess-sample-usage/
void Helper::StartApp(wstring cmd)
{
    
TCHAR buff[10000] = { 0 };
    
_tcscpy(buff, cmd.c_str());
 
    
PROCESS_INFORMATION pi;
    
STARTUPINFO si = {0};
    
si.cb = sizeof(STARTUPINFO);
 
    
// Create the child process.
    
BOOL bRtn = CreateProcess(
        
NULL,
        
buff,
        
NULL, // process security attributes
        
NULL, // primary thread security attributes
        
TRUE, // handles are inherited
        
0, // creation flags
        
NULL, // use parent's environment
        
NULL, // use parent's current directory
        &
si, // STARTUPINFO pointer
        &
pi); // receives PROCESS_INFORMATION
 
    
if (!bRtn)
    
{
        
MessageBox(NULL, buff,
            
L"CreateProcess failed!", MB_ICONERROR | MB_OK);
    
}
}

试了很久,加了下面两段就没问题了:
1.
TCHAR buff[10000] = { 0 };
_tcscpy(buff, cmd.c_str());

2.
STARTUPINFO si = {0};
si.cb = sizeof(STARTUPINFO);

留下回复