Windows进程管理类封装

  • 头文件
#include <Windows.h>
#include <iostream>
using namespace std;
#include "stdafx.h"
using namespace std;


class ProcessMgr{
public:
	ProcessMgr();
	~ProcessMgr();

	static ProcessMgr* GetInstance();

	// 判断是否进程存在
	// @params:dw_pid:进程id
	// @return : true:进程存在,false:进程不存在
	bool IsProcessExist(DWORD dw_pid)const;

	// 获取进程句柄
	// @params:dw_pid:进程id
	// @return : nullptr:进程句柄为空,else:实际句柄值
	HANDLE GetProcessHandle(DWORD dw_pid)const;

	// 退出进程
	// @param: dw_pid: 进程id
	// @return: true : 退出成功,false: 退出失败
	bool KillProcess(DWORD dw_pid) const;

	// 判断是否进程运行()
	// @params: str_mutex :根据进程互斥量的方式来进行判断,获取的是自己设定规则打开的进程
	// @return: true:正在运行 false:没有运行
	bool IsProcessRunning(std::wstring str_mutex);

	// 获取当前进程句柄
	HANDLE GetCurrentProcess();


private:
	static ProcessMgr* m_pProcessMgr;
};

  • 实现文件
// ProcessMgr.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "ProcessMgr.h"
#include <process.h>
#include <TlHelp32.h>
using namespace std;

#define g_pProcessMgr ProcessMgr::GetInstance()

#define SAFE_DELETE(xxx) \
	CloseHandle(xxx);\
	xxx = NULL;

ProcessMgr::ProcessMgr()
{

}

ProcessMgr::~ProcessMgr()
{

}

ProcessMgr* ProcessMgr::GetInstance()
{
	if (m_pProcessMgr!=nullptr)
	{
		m_pProcessMgr = new ProcessMgr();
		return m_pProcessMgr;
	}
	return m_pProcessMgr;
}

bool ProcessMgr::IsProcessExist(DWORD dw_pid)const
{
	PROCESSENTRY32 pe;
	pe.dwSize = sizeof(PROCESSENTRY32);
	HANDLE hSnapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
	if(hSnapshot == INVALID_HANDLE_VALUE)
	{
		cout << "进程不存在" << endl;
		CloseHandle(hSnapshot);
		return false;
	}
	BOOL bFindProcess = ::Process32First(hSnapshot,&pe);
	if (bFindProcess)
	{
		do 
		{
			if (pe.th32ProcessID == dw_pid)
			{
				cout << "进程存在" << endl;
				CloseHandle(hSnapshot);
				return true;
			}
		} while (::Process32Next(hSnapshot,&pe));
	}
	cout << "进程不存在" << endl;
	CloseHandle(hSnapshot);
	return false;
}

HANDLE ProcessMgr::GetProcessHandle(DWORD dw_pid)const
{
	HANDLE hHandle = OpenProcess(PROCESS_ALL_ACCESS ,NULL,dw_pid);
	if (hHandle != nullptr)
	{
		return hHandle;
	}
	else
	{
		cout << "获取句柄为空" << endl;
		CloseHandle(hHandle);
		return nullptr;
	}
}

bool ProcessMgr::KillProcess(DWORD dw_pid)const
{
	HANDLE hProcess = GetProcessHandle(dw_pid);
	if (hProcess!=nullptr)
	{
		return TerminateProcess(hProcess,IDOK);
	}
	// 关闭句柄
	SAFE_DELETE(hProcess);
	return false;

}

bool ProcessMgr::IsProcessRunning(std::wstring str_mutex)
{
	HANDLE hMutex = CreateMutex(NULL,FALSE,str_mutex.c_str());
	DWORD err_code = GetLastError();
	if (hMutex)
	{
		if (ERROR_ALREADY_EXISTS == err_code)
		{
			return true; //正在运行
		}
		::CloseHandle(hMutex);
	}
	return false;
}

HANDLE ProcessMgr::GetCurrentProcess()
{
	return ::GetCurrentProcess();
}


ProcessMgr* ProcessMgr::m_pProcessMgr = nullptr;

int _tmain(int argc, _TCHAR* argv[])
{
	g_pProcessMgr->GetProcessHandle(71992);
	system("pause");
	return 0;
}


发布了382 篇原创文章 · 获赞 122 · 访问量 40万+

猜你喜欢

转载自blog.csdn.net/Giser_D/article/details/104632891