有時想直接從自己的程式叫入其他執行檔, 這樣就會有parent/ child 的關係可使用.

 

sample code from here, 就不做太多細節說明.

https://www.codeproject.com/Articles/6874/Enhanced-version-of-the-CreateProcess-function

 

// sample MFC: this-->m_hWnd;  

CString m_szAppPath="d:\\test.exe";

CString m_szCmdLine="10 20 30";

// 使用方式

 if (CreateProcessEx(m_szAppPath, m_szCmdLine, true, false, true, m_bMinimizeOnWait, this->m_hWnd) == -1)
{

// error

}

 

DWORD CreateProcessEx ( LPCSTR lpAppPath, LPCSTR lpCmdLine, BOOL bAppInCmdLine, BOOL bCompletePath,
                        BOOL bWaitForProcess, BOOL bMinimizeOnWait, HWND hMainWnd ) {

    .....

    STARTUPINFO startupInfo;
    PROCESS_INFORMATION    processInformation;    

    ZeroMemory( &startupInfo, sizeof( STARTUPINFO ));

    startupInfo.cb = sizeof( STARTUPINFO );

    ZeroMemory( &processInformation, sizeof( PROCESS_INFORMATION ));

    if ( CreateProcess(    bAppInCmdLine ? NULL: szAppPath,    // lpszImageName
                        szCmdLine,                            // lpszCommandLine
                        0,                                    // lpsaProcess
                        0,                                    // lpsaThread
        TRUE,                                // fInheritHandles
//        FALSE,                                // fInheritHandles
                        DETACHED_PROCESS,                    // fdwCreate
                        0,                                    // lpvEnvironment
                        0,                                    // lpszCurDir
                        &startupInfo,                        // lpsiStartupInfo
                        &processInformation                    // lppiProcInfo
                    )) {
 

// 可以等待 外call 的 program 結束, 或是平行. 

        if ( bWaitForProcess ) {

            if ( bMinimizeOnWait )
                    
                if ( IsWindow( hMainWnd )) ShowWindow( hMainWnd, SW_MINIMIZE );
                #ifdef __AFX_H__
                else AfxGetMainWnd()->ShowWindow( SW_MINIMIZE );
                #endif

            WaitForSingleObject( processInformation.hProcess, INFINITE );

            if ( bMinimizeOnWait )

                if ( IsWindow( hMainWnd )) ShowWindow( hMainWnd, SW_RESTORE );
                #ifdef __AFX_H__
                else AfxGetMainWnd()->ShowWindow( SW_RESTORE );
                #endif

            GetExitCodeProcess( processInformation.hProcess, &dwExitCode );
        }
        else {

            CloseHandle( processInformation.hThread );
            CloseHandle( processInformation.hProcess );

            dwExitCode = 0;
        }
 

...

 

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 cianfen 的頭像
    cianfen

    cianfen的部落格

    cianfen 發表在 痞客邦 留言(0) 人氣()