在windows下,通过在C++中调用shell的方式来启动以及关闭exe,代码如下:
#include <Windows.h>
#include <ShellAPI.h>
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{ //启动exe
SHELLEXECUTEINFO shell = { sizeof(shell) };
shell.fMask = SEE_MASK_FLAG_DDEWAIT;
shell.lpVerb = L"open";
shell.lpFile = L"C:\\Users\\Z\\Desktop\\yu\\transportStr_server_serach - 副本\\transportStr\\Debug\\server.exe";
shell.nShow = SW_SHOWNORMAL;
BOOL ret = ShellExecuteEx(&shell);
if(ret)
{
cout<<"open ok"<<endl;
}else{
cout<<"open falied"<<endl;
}
Sleep(3000);
//关闭exe
HWND hWnd = ::FindWindow(NULL,(L"C:\\Users\\Z\\Desktop\\yu\\transportStr_server_serach - 副本\\transportStr\\Debug\\server.exe"));
if (NULL != hWnd) {
::SendMessage(hWnd, WM_CLOSE, 0, 0);
cout<<"close ok"<<endl;
}else{
cout<<"close falied"<<endl;
}
system("pause");
return 0;
}
本文介绍了一种在Windows环境下使用C++通过调用Shell来启动和关闭EXE文件的方法。具体实现包括设置SHELLEXECUTEINFO结构体以启动指定路径的EXE文件,并通过FindWindow和SendMessage函数来查找并关闭该进程。

493

被折叠的 条评论
为什么被折叠?



