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

推荐订阅源

Spread Privacy
Spread Privacy
T
Threatpost
L
LINUX DO - 热门话题
Google Online Security Blog
Google Online Security Blog
I
InfoQ
大猫的无限游戏
大猫的无限游戏
博客园_首页
爱范儿
爱范儿
有赞技术团队
有赞技术团队
V
Visual Studio Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Jina AI
Jina AI
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
CERT Recently Published Vulnerability Notes
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
K
Kaspersky official blog
L
LangChain Blog
G
GRAHAM CLULEY
B
Blog RSS Feed
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
About on SuperTechFans
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
T
Tailwind CSS Blog
Cyberwarzone
Cyberwarzone
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
Help Net Security
Help Net Security
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
雷峰网
雷峰网
C
Check Point Blog
MongoDB | Blog
MongoDB | Blog
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
Vercel News
Vercel News
T
Tor Project blog
T
The Exploit Database - CXSecurity.com
T
Troy Hunt's Blog
W
WeLiveSecurity
T
Threat Research - Cisco Blogs

博客园 - 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 );
}
}