【问题标题】:want to exit from the main process without wait to process2 return想退出主进程而不等待process2返回
【发布时间】:2013-10-22 07:08:38
【问题描述】:

我在 MFC 中创建了应用程序(如 App1)。从 app1,我从 app1 调用了另一个应用程序 app2。我叫

CString szCmdline = "app2.exe";
BOOL ret= CreateProcess( NULL,
                szCmdline.GetBuffer(szCmdline.GetLength()),    // application name with parameter
                NULL,          // process security attributes
                NULL,          // primary thread security attributes
                TRUE,          // handles are inherited
                0,              //DETACHED_PROCESS, // creation flags
                NULL,          // use parent's environment
                NULL,           // use parent's current directory
                &siStartInfo,  // STARTUPINFO pointer
                &piProcInfo);  // receives PROCESS_INFORMATION
    if(ret)
    {
                return;

    } else 
    {
        return;
    }


}

我希望应用程序 app1 退出而不让 App2 返回;

【问题讨论】:

    标签: visual-c++ mfc createprocess


    【解决方案1】:

    首先你应该得到父进程ID然后终止父进程如下。

    DWORD crtpid= GetCurrentProcessId();//创建进程前获取当前进程id

    创建进程后,您可以使用其进程 id(crtpid) 关闭父进程

    处理 hProc = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, crtpid);

    if (hProc)
    {
        ::TerminateProcess(hProc, 1);
        ::CloseHandle(hProc);
    } 
    

    【讨论】:

      【解决方案2】:

      你试过了吗!

      ExitProcess(0)
      

      语法:

      VOID WINAPI ExitProcess(
        _In_  UINT uExitCode
      );
      

      备注:

      使用GetExitCodeProcess 函数检索进程的退出值。使用GetExitCodeThread 函数检索线程的退出值。

      【讨论】:

        【解决方案3】:

        如果您想在启动 App2 后终止 App1,您只需在 App1 中调用 PostQuitMessage。这是关闭应用程序的一种更简洁的方式,因为它为应用程序提供了清理的机会。

        【讨论】:

          猜你喜欢
          • 2021-11-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-24
          • 1970-01-01
          • 1970-01-01
          • 2023-03-13
          • 1970-01-01
          相关资源
          最近更新 更多