简介本文介绍C++中进程相关操作总结,主要有开启进程、结束进程以及判断进程是否存在,感兴趣的朋友可以参考一下。

开启进程
方式1 - CreateProcess

DWORD MyCreateProcess(CString strExePath, CString strCmdline, long& lProcessID)
{
	DWORD re_vlaue = 0;
	PROCESS_INFORMATION piProcInfo;
	STARTUPINFO siStartupInfo;
	SECURITY_ATTRIBUTES saProcess, saThread;

	ZeroMemory(&siStartupInfo, sizeof(siStartupInfo));
	siStartupInfo.cb = sizeof(siStartupInfo);
	siStartupInfo.dwFlags = STARTF_USESHOWWINDOW;
	siStartupInfo.wShowWindow = SW_HIDE;

	saProcess.nLength = sizeof(saProcess);
	saProcess.lpSecurityDescriptor = NULL;
	saProcess.bInheritHandle = true;

	saThread.nLength = sizeof(saThread);
	saThread.lpSecurityDescriptor = NULL;
	saThread.bInheritHandle = true;

	BOOL ret = ::CreateProcess((LPTSTR)strExePath.GetBuffer(), (LPTSTR)strCmdline.GetBuffer(), &saProcess, &saThread, false, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &siStartupInfo, &piProcInfo);
	if (!ret)
	{
		return re_vlaue;
	}
	WaitForSingleObject(piProcInfo.hProcess, INFINITE);
	lProcessID = piProcInfo.dwProcessId;
	GetExitCodeProcess(piProcInfo.hProcess, &re_vlaue);

	CloseHandle(piProcInfo.hProcess);
	return re_vlaue;
}

// 开启进程
bool StartProcess(LPCTSTR program, LPCTSTR args)
{
	bool bRet = false;
	long lProcessID = 0;
	int nRet = MyCreateProcess(program, args, lProcessID);
	if (1 != nRet)
	{
		bRet = false;
	}
	else
	{
		bRet = true;
	}
	return bRet;
}

开启进程
方式2 - ShellExecuteEx

// 开启进程
// 使用MyCreateProcess调用会有问题 命令行参数不对 需要同一目录下调用
bool StartProcessByShell(LPCTSTR program, LPCTSTR args, bool bWait)
{
	SHELLEXECUTEINFO ShExecInfo = { 0 };
	ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
	ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI;
	ShExecInfo.hwnd = NULL;
	ShExecInfo.lpVerb = _T("open");
	ShExecInfo.lpFile = program;
	ShExecInfo.lpParameters = args;
	ShExecInfo.lpDirectory = NULL;
	ShExecInfo.nShow = SW_SHOW;
	ShExecInfo.hInstApp = NULL;
	ShellExecuteEx(&ShExecInfo);
	if (bWait)
	{
		WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
	}
	return true;
}

结束进程

// 结束进程
BOOL KillProcessFromName(CString sProcessName)
{
	if (sProcessName.IsEmpty())
	{
		return FALSE;
	}
	DWORD dwProcessID = GetProcessIDFromName(sProcessName);
	if (dwProcessID == 0)
	{
		return FALSE;
	}
	HANDLE hProcess = ::OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessID);
	::TerminateProcess(hProcess, 0);
	CloseHandle(hProcess);
	return TRUE;
}

判断进程是否存在

// 判断进程是否存在
DWORD GetProcessIDFromName(CString sProcessName)
{
	DWORD id = 0;
	// 创建进程快照(TH32CS_SNAPPROCESS表示创建所有进程的快照)  
	HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	// PROCESSENTRY32进程快照的结构体  
	PROCESSENTRY32 pe;
	pe.dwSize = sizeof(PROCESSENTRY32);
	if (!Process32First(hSnapShot, &pe))
	{
		return id;
	}
	// 将字符串转换为小写  
	sProcessName.MakeLower();

	// 如果句柄有效  则一直获取下一个句柄循环下去  
	while (Process32Next(hSnapShot, &pe))
	{
		CString scTmp = pe.szExeFile;
		scTmp.MakeLower();

		// 比较当前进程的可执行文件名称和传递进来的文件名称是否相同,相同的话Compare返回0  
		if (!scTmp.Compare(sProcessName))
		{
			id = pe.th32ProcessID;
			break;
		}
	}
	return id;
}


更多为你推荐