2014-02-26 117 views
0

我只是想創建一個進程在Windows上運行我的代碼,如下一個應用程序:的CreateProcess無法運行應用程序

//init the structure 
STARTUPINFOW StartupInfo; 
ZeroMemory(&StartupInfo,sizeof(StartupInfo)); 
StartupInfo.cb = sizeof(StartupInfo); 
StartupInfo.dwFlags = STARTF_USESHOWWINDOW; 
StartupInfo.wShowWindow = true ; 
PROCESS_INFORMATION ProcessInfo; 
ZeroMemory(&ProcessInfo,sizeof(ProcessInfo)); 
DWORD dwExitCode = 0; 

LPCWSTR cmdFormat = "xxxxxx"; // this is the applocation's path 
LPWSTR cmd = new wchar_t[256*sizeof(wchar_t)]; 
wcscpy_s(cmd, wcslen(cmdFormat)+1,cmdFormat); 
int ret = CreateProcessW(cmd, 
         NULL, 
         NULL, 
         NULL, 
         false, 
         NORMAL_PRIORITY_CLASS, 
         NULL, 
         NULL, 
         &StartupInfo, 
         &ProcessInfo); 
if(ret) 
{ 
    CloseHandle(ProcessInfo.hThread); 
    WaitForSingleObject(ProcessInfo.hProcess, INFINITE); 
    GetExitCodeProcess(ProcessInfo.hProcess, &dwExitCode); 
    CloseHandle(ProcessInfo.hProcess); 
} 
if(dwExitCode==0) 
{ 
    DWORD errorcode = GetLastError(); 
    std::cout<<"ERROR: "<<errorcode<<std::endl; 
} 

我使用這個功能,我可以創建新的進程來運行的notepad.exe和一些其他的應用

Q1:但是當我關閉應用程序的dwExitCode = 0,錯誤代碼1803

Q2:一些應用程序無法運行只是立即退出

+1

題外話,但很重要:那就是*不是*正確的方式來使用'strcpy_s'。第二個參數不是源字符串的長度,而是您要複製到的緩衝區的長度。 –

回答

0

以下函數總是對我的作品:

static int createProcess(string cmdLine, bool isWait, LPDWORD pExitCode) 
    { 
     STARTUPINFOA si; 
     PROCESS_INFORMATION pi; 
     ::ZeroMemory(&si, sizeof(si)); 

     si.cb = sizeof(si); 
     ::ZeroMemory(&pi, sizeof(pi)); 

     // reset last error 
     ::SetLastError(0); 
     // Start the child process. 
     BOOL bCreateProcess = ::CreateProcessA(NULL, // No module name (use command line) 
     (LPSTR) cmdLine.c_str(), // Command line 
     NULL,      // Process handle not inheritable 
     NULL,      // Thread handle not inheritable 
     FALSE,     // Set handle inheritance to FALSE 
     CREATE_NO_WINDOW,   // 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 

     if(!bCreateProcess) 
     { 
     // create process failed, 
     //Logger::trace(error, getClassName(), "createProcess", getFormattedStringA("create process failed with error:%d, Commad line:'%s',isWait:%d",GetLastError(), cmdLine.c_str(), isWait),"CreateProcess Failed"); 
     return 0; 
     } 

     //Logger::trace(info, getClassName(), "createProcess", getFormattedStringA("created process,Commad line:'%s',isWait:%d,Result:%d", cmdLine.c_str(), isWait,bCreateProcess),"Launched Process"); 
     // Wait until child process exits. 
     if(isWait) 
     { 
     ::WaitForSingleObject(pi.hProcess, INFINITE); 
     if(pExitCode) 
     { 
      ::GetExitCodeProcess(pi.hProcess, pExitCode); 
     } 
     } 
     ::CloseHandle(pi.hProcess); 
     pi.hProcess = NULL; 
     ::CloseHandle(pi.hThread); 
     pi.hThread = NULL; 
     return 1; // return non zero. function succeeded 
    } 
+0

是的我知道,但我的問題是我想通過「createprocess」運行遊戲,但它不能運行其他應用程序都OK。是否有一些編譯器的參數是我之前設置的遊戲 – Ericzhang88120

0

是的,我找了根會導致一些應用程序需要一些本地的資源,所以可能需要家長的起始目錄