سؤال

I have created application in MFC (Says App1). And from app1, I have called another application app2 from app1. I called as

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;
    }


}

I want the application app1 exit without for App2 to return;

هل كانت مفيدة؟

المحلول

first You should get parent process id then terminate parent process as follows.

DWORD crtpid= GetCurrentProcessId();//get current process id before creating process

after creating process you may close parent process using their process id(crtpid)

HANDLE hProc = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, crtpid);

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

نصائح أخرى

Have you try !

ExitProcess(0)

Syntex:

VOID WINAPI ExitProcess(
  _In_  UINT uExitCode
);

Remarks:

Use the GetExitCodeProcess function to retrieve the process's exit value. Use the GetExitCodeThread function to retrieve a thread's exit value.

If you want to terminate App1 after starting App2, you can simply call PostQuitMessage from within App1. It's a much cleaner way of shutting down an application because it provides the app with the opportunity to clean up.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top