/********************************************************
* @function : IsProcessExist
* @brief : 判断进程是否存在
* @author : Name
* @time : 2021/09/07
*********************************************************/
BOOL IsProcessExist(const UINT nProcessId)
{
DWORD dwProcessID = 0;
CString strExeNameTemp = _T("");
strExeNameTemp.LoadString(nProcessId);
dwProcessID = GetProcessIdName(strExeNameTemp);
if (dwProcessID == 0)
{
return FALSE;
}
return TRUE;
}
/********************************************************
* @function : ProcessShutDown
* @brief : 关闭进程
* @author : Name
* @time : 2021/09/07
*********************************************************/
BOOL ProcessShutDown(const UINT nProcessID)
{
DWORD dwMobileID = 0;
HANDLE hWndMobile = NULL;
BOOL bRet = FALSE;
CString strExeNameTemp = _T("");
strExeNameTemp.LoadString(nProcessID);
dwMobileID = GetProcessIdName(strExeNameTemp);
if (dwMobileID == 0)
{
return bRet;
}
hWndMobile = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, 0, dwMobileID); //获取进程ID
if (NULL != hWndMobile)
{
if (::TerminateProcess(hWndMobile, 0))
{
bRet = TRUE;
}
}
CloseHandle(hWndMobile);
return bRet;
}
/********************************************************
* @function : GetProcessIdName
* @brief : 获得进程名字
* @author : Name
* @time : 2021/09/07
*********************************************************/
DWORD GetProcessIdName(CString strExeName)
{
PROCESSENTRY32 pe;
DWORD dwPid = 0;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
pe.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnapshot, &pe))
{
return dwPid;
}
CString strProcessExeName = _T("");
CString strLocalExeName = _T("");
while (TRUE)
{
pe.dwSize = sizeof(PROCESSENTRY32);
strProcessExeName = pe.szExeFile;
strLocalExeName = strExeName;
strProcessExeName.MakeUpper();
strLocalExeName.MakeUpper();
if (strProcessExeName.CompareNoCase(strLocalExeName) == 0)
{
dwPid = pe.th32ProcessID;
break;
}
if (Process32Next(hSnapshot, &pe) == FALSE)
{
break;
}
}
CloseHandle(hSnapshot);
return dwPid;
}
DWORD Ping(LPCTSTR strIp, DWORD dwMilliseconds)
{
SHELLEXECUTEINFO ShExecInfo = { 0 };
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = _T("ping.exe");
ShExecInfo.lpParameters = strIp;
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_HIDE; //SW_SHOW
ShExecInfo.lpVerb = _T("runas"); //以管理员权限运行
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
DWORD dwExitCode = 1;
if (ShExecInfo.hProcess == NULL)
return dwExitCode;
if (WaitForSingleObject(ShExecInfo.hProcess, dwMilliseconds) == WAIT_TIMEOUT)
{
TerminateProcess(ShExecInfo.hProcess, 0);
return dwExitCode;
}
BOOL bOK = GetExitCodeProcess(ShExecInfo.hProcess, &dwExitCode);
ASSERT(bOK);
return dwExitCode;
}