c++ 調用外部程序exe-ShellExecuteEx


此方法最實用的調用exe.

#include <ShellAPI.h>

string file_path = s_run_dir+"\\ConsoleApplication1.exe";
    if (!myfile.IsFileExist(file_path))
    {
        return 1;
    }

    LPCWSTR lp_file_path = mystring.StringToLPCWSTR(file_path);

    SHELLEXECUTEINFO ShExecInfo;

    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = NULL;
    ShExecInfo.lpFile = lp_file_path;
    ShExecInfo.lpParameters = __T("D:\\a.txt D:\\b.txt D:\\c.txt");//傳出去的參數
    ShExecInfo.lpDirectory = NULL;
    ShExecInfo.nShow = SW_SHOW;
    ShExecInfo.hInstApp = NULL;

    BOOL b_ret=ShellExecuteEx(&ShExecInfo);
    if (b_ret)
    {
        //等待調用啟動的exe關閉,此處要設置成ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
        WaitForSingleObject(ShExecInfo.hProcess, INFINITE);

    }
    else
    {
        return 2;
    }

 

傳給控制台程序參數

#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{

    // 參數個數,第一個參數為可執行文件路徑  
     int iParamCount = argc;
     cout << "參數個數:" << iParamCount << endl;
     for (int i = 0; i < iParamCount; i++)
     {
           cout << endl << "" << i + 1 << "個參數:";
           wprintf(argv[i]);
     }
    
     getchar();

    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM