• 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;
    }
  • 相关阅读:
    基于Spring aop写的一个简单的耗时监控
    Intellij Idea 15 旗舰版 破解
    设计模式之工厂模式
    IDE神器intellij idea的基本使用
    [js] js判断浏览器(转)
    java知识大全积累篇
    一些技术大牛的博客集锦(转)
    添加鼠标右击菜单
    java 方法调用绑定
    Android系列--DOM、SAX、Pull解析XML
  • 原文地址:https://www.cnblogs.com/ike_li/p/4545720.html
Copyright © 2020-2023  润新知