惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
爱范儿
爱范儿
罗磊的独立博客
V
V2EX
量子位
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
Jina AI
Jina AI
博客园 - 叶小钗
小众软件
小众软件
博客园 - 【当耐特】
Y
Y Combinator Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
博客园 - 聂微东
Microsoft Security Blog
Microsoft Security Blog
美团技术团队
P
Proofpoint News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - surfer

金山开源安全卫士全套代码编译指南 TabControl添加关闭按钮 string、wstring、cstring、 char、 tchar、int、dword转换方法 string,wstring,CString,TCHAR,char*之间常用转换 在VS 2008下成功编译Chrome QT + VS2008安装配置 用于多媒体应用的无窗口ATL ActiveX控件 临界区,互斥量,信号量,事件的区别(线程同步) GuiToolKit的编译 可在U盘或光盘上运行的LINUX(自带强大软件包和开发工具) 用VC纯资源dll制作多语言界面程序 - surfer - 博客园 没有完美的外汇经纪商和交易平台 外汇交易时段 MB Trading 订单类型 外汇炒单速成法 外汇高手的顺势理论 外汇点值计算 外汇交易商排名 explorer无法随Windows 7启动原因 光驱读盘能力差的解决
在VC程序中调用exe文件或者批处理文件方法总结
surfer · 2011-03-19 · via 博客园 - surfer

在VC程序中调用exe文件或者批处理文件的方法:
一、使用system函数。
    该函数可以直接使用,调用exe程序或者bat批处理程序
    例如:CString strCommand("d:\\test.bat");
          system(strCommand);
二、使用ShellExecute函数
    该函数可以将调用的窗口隐藏或者显示
    HINSTANCE ShellExecute(          HWND hwnd,
    LPCTSTR lpOperation,
    LPCTSTR lpFile,
    LPCTSTR lpParameters,
    LPCTSTR lpDirectory,
    INT nShowCmd
   );
三、使用CreateProcess函数
std::string strCommand = strApp + "\\PerDecodeX2ap.exe";
PROCESS_INFORMATION pi;
STARTUPINFO si;
//初始化变量
memset(&si,0,sizeof(si));
si.cb=sizeof(si);
si.wShowWindow = SW_HIDE;
si.dwFlags = STARTF_USESHOWWINDOW;
char buff[256];
sprintf(buff,"%s",strCommand.c_str());

BOOL fRet=CreateProcess(NULL,
   buff,
   NULL,
   NULL,
   FALSE,
   NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,
   NULL,
   NULL,
   &si,
   &pi);
if (!fRet)
{
       LPVOID lpMsgBuf;
       FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                                                FORMAT_MESSAGE_FROM_SYSTEM |
                                                FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    GetLastError(),
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), //
Default language
    (LPTSTR) &lpMsgBuf,
    0,
    NULL );
   AfxMessageBox( (LPCTSTR)lpMsgBuf);
   return FALSE;
}

四、使用如下封装好的函数(自己写的),尝试各种方法去启动可执行程序
CommandExecuter(const char *szAppName, const char *szCmd)
{
char szWorkPath[256] = {0};
char szCommand[256] = {0};
int j = 0;
//从配置文件中取参数
STARTUPINFO si = {0};
PROCESS_INFORMATION pi = {0};

si.cb = sizeof(si);
if ( szAppName != NULL && strlen(szAppName) > 0 )
{
   memset( szWorkPath, 0, 256 );

   GetSystemDirectory(szWorkPath, 256);
   szWorkPath[3] = '\0';
   strcat(szWorkPath, "Program Files\\");
   strcat(szWorkPath, szAppName);

   if(_access(szWorkPath, strlen(szWorkPath)) == -1)
   {
    strcpy(szWorkPath, szAppName);
   }//if(_access(szWorkPath, strlen(szWorkPath)) == -1)
   strcat(szWorkPath, " ");
   strcat(szWorkPath, szCmd);

   if ( CreateProcess( NULL,
    (char *)szWorkPath,
    NULL,
    NULL,
    FALSE,
    0,
    NULL,
    NULL,
    &si,
    &pi ) )
   {
    CloseHandle( pi.hThread );
    CloseHandle( pi.hProcess );
    return;
   }
   if ( CreateProcess( szAppName,
    (LPSTR)szCmd,
    NULL,
    NULL,
    FALSE,
    0,
    NULL,
    NULL,
    &si,
    &pi ) )
   {
    CloseHandle( pi.hThread );
    CloseHandle( pi.hProcess );
    return;
   }
   if ( (int)ShellExecute ( NULL,
    NULL,
    (LPCSTR)szWorkPath,
    NULL,
    NULL,
    SW_SHOW ) > 32 )
   {
    // 启动成功了
    return;
   }
   if ( (int)ShellExecute( NULL,
    NULL,
    szAppName,
    (LPCSTR)szCmd,
    NULL,
    SW_SHOW ) > 32 )
   {
    // 启动成功了
    return;
   }
}//if ( szAppName != NULL && strlen(szAppName) > 0 )
else
{
   if ( CreateProcess(NULL, (char *)szCmd, NULL, NULL, FALSE,

0, NULL, NULL, &si, &pi) )
   {
    CloseHandle( pi.hThread );
    CloseHandle( pi.hProcess );
    return;
   }//if ( CreateProcess(NULL, (char *)szCmd, NULL, NULL,

FALSE, 0, NULL, NULL, &si, &pi) )
   ShellExecute ( NULL, NULL, (LPCSTR)szCmd, NULL, NULL,

SW_SHOW );
}
}