在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;
}