STARTUPINFO   stinfo;   //启动窗口的信息     
PROCESS_INFORMATION   procinfo;   //进程的信息     
if(!CreateProcess(NULL,_T("C:\\WINDOWS\\notepad.exe"),NULL,NULL,FALSE,
 NORMAL_PRIORITY_CLASS,NULL,NULL,&stinfo,&procinfo))
 cout<<jks::GetLastError(); //这是我改进后的,可以将code找到对应的字符串
返回是“内存分配访问无效”但是 system("C:\\WINDOWS\\notepad.exe");没有问题啊请问我错到那?

解决方案 »

  1.   

    msdn上的例子:#include <windows.h>
    #include <stdio.h>void main( VOID )
    {
        STARTUPINFO si;
        PROCESS_INFORMATION pi;    ZeroMemory( &si, sizeof(si) );
        si.cb = sizeof(si);
        ZeroMemory( &pi, sizeof(pi) );    // Start the child process. 
        if( !CreateProcess( NULL,   // No module name (use command line). 
            TEXT("MyChildProcess"), // Command line. 
            NULL,             // Process handle not inheritable. 
            NULL,             // Thread handle not inheritable. 
            FALSE,            // Set handle inheritance to FALSE. 
            0,                // No creation flags. 
            NULL,             // Use parent's environment block. 
            NULL,             // Use parent's starting directory. 
            &si,              // Pointer to STARTUPINFO structure.
            &pi )             // Pointer to PROCESS_INFORMATION structure.
        ) 
        {
            printf( "CreateProcess failed (%d).\n", GetLastError() );
            return;
        }    // Wait until child process exits.
        WaitForSingleObject( pi.hProcess, INFINITE );    // Close process and thread handles. 
        CloseHandle( pi.hProcess );
        CloseHandle( pi.hThread );
    }
      

  2.   

    STARTUPINFO   startupInfo;
    memset(&startupInfo,0,sizeof(STARTUPINFO));
    startupInfo.cb = sizeof(STARTUPINFO); startupInfo.dwFlags |= STARTF_USESHOWWINDOW;
        startupInfo.wShowWindow = SW_SHOWMAXIMIZED;
     PROCESS_INFORMATION ProcessInfo;
      BOOL bCreate = ::CreateProcess
            (
    "C:\\winnt\\notepad.exe",
      NULL,
      NULL,
            NULL,
            FALSE,
            0,
            NULL,
            NULL,
            &startupInfo,
            &ProcessInfo); WaitForSingleObject(ProcessInfo.hProcess,1000000);
    MessageBox("调用程序结束!");
      

  3.   

    感谢各位的帮助现在还有一个问题,就是启动外部进程后,要等它终止主程序才应该继续进行
    本来应该用CREATE_SUSPENDED的,但是一用运行就出错,请问是怎么回事???
    CString cmd;
    cmd.Format(" -g -p ANE3FL -dir \"D:\\ansys_file\" -j \"file\" -s read -l en-us -t -d win32 -d1 %f -d3 %f",d1,d3);
    char* pcmd = const_cast<char*>((const char*)cmd); STARTUPINFO si;
    memset(&si,0,sizeof(STARTUPINFO));
    PROCESS_INFORMATION pi;
    memset(&pi,0,sizeof(PROCESS_INFORMATION)); CString path = "d:\\Program Files\\Ansys Inc\\v100\\ANSYS\\bin\\Intel\\";
    _chdir(path);
    CString workpath = "D:\\ansys_file\\"; if(!CreateProcess("ansys100.exe",pcmd,
    NULL,NULL,FALSE,
    CREATE_SUSPENDED,NULL,
    workpath,&si,&pi))
    cout<<"error:"<<GetLastError();
      

  4.   

    CREATE_SUSPENDED??
    WaitForSingleObject
      

  5.   

    STARTUPINFO   stinfo;   //启动窗口的信息     
    PROCESS_INFORMATION   procinfo;   //进程的信息 
    memset(&stinfo,0,sizeof(stinfo));
    if(!CreateProcess("C:\\WINDOWS\\notepad.exe",
    NULL,NULL,NULL,FALSE,
     NULL,NULL,NULL,&stinfo,&procinfo))
     cout<<jks::GetLastError(); 
    else
    cout<<"ok1!"<<endl; if(WAIT_FAILED==WaitForSingleObject(procinfo.hProcess,INFINITE))
    cout<<GetLastError(); cout<<"ok2!"<<endl;
    getchar();