January 9, 2012

Run a third party application from your software in C++

Here is the function which runs the 3th party application with the given program argument. (if exists): I gave my classes name as CApplicationStarter. But you can change it as you want.

bool CApplicationStarter::RunApplication( CString applicationExe, CString progArgument )
    bool bRet = false;

    DWORD dwTimeout = 5000;

    if( !applicationExe.IsEmpty() )
        STARTUPINFO StartupInfo;
        PROCESS_INFORMATION ProcessInfo;

        memset(&StartupInfo, 0, sizeof(StartupInfo));
        StartupInfo.cb = sizeof(STARTUPINFO);
        StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
        StartupInfo.wShowWindow = SW_HIDE;
        StartupInfo.wShowWindow = SW_SHOW;

        // 0. argument must always be the path to the EXE file!
        CString arg0 = applicationExe;
        // If spaces are found, the entire path is quoted.

        // 1. argument is the XML file
        CString arg1 = progArgument;
        // If spaces are found, the entire path is quoted.

        // The command line parameters for the application
        CString cmd = arg0 + _T(" ") + arg1;

        // Launch application
        if (CreateProcess( applicationExe, cmd.GetBuffer(MAX_PATH),
            NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &StartupInfo, &ProcessInfo))
            DWORD dwRet = WaitForInputIdle(ProcessInfo.hProcess, dwTimeout );
            bRet = (dwRet == 0);



    return bRet;

No comments: