2014-02-26 116 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

是的,我找了根会导致一些应用程序需要一些本地的资源,所以可能需要家长的起始目录