进程间通信之打开关闭一个exe文件

时间:2024-09-10 14:35:26

一、打开

1、WinExec("F:\\QQ\\Bin\\QQ.exe",SW_SHOW);

注意:'\'需要改成'\\',阻塞

2、

STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
// Start the child process
if (CreateProcess(_T("C:\\测试部串口助手20161125.exe"), _T(""), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else
{
AfxMessageBox(_T("创建失败!"));
HANDLE hProcess = GetCurrentProcess();//get current process
TerminateProcess(hProcess, 0); //close process
}//非阻塞

二、关闭

HWND hWnd = ::FindWindow(NULL, _T("content"));//conten是已开启的窗口标题,获得窗口句柄
::SendMessage(hWnd, WM_CLOSE, 0, 0);//发送关闭消息