HANDLE GetProcessHandleByPid(DWORD dwPid)
{
return OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPid);
}
//通过进程名获取进程句柄
#include <tlhelp32.h>
HANDLE GetProcessHandleByName(LPCTSTR pName)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot)
{
return NULL;
}
PROCESSENTRY32 pe = { sizeof(pe) };
BOOL fOk = Process32First(hSnapshot, &pe);
for (; fOk; fOk = Process32Next(hSnapshot, &pe))
{
if (!_tcsicmp(pe.szExeFile, pName))
{
CloseHandle(hSnapshot);
return OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
}
}
CloseHandle(hSnapshot);
return NULL;
}
//通过进程名获取进程ID
BOOL GetPidByFileName(CString sName, DWORD &dwPid)
{
//创建进程快照
HANDLE hSnapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //0: 指本进程
if (INVALID_HANDLE_VALUE == hSnapshot)
{
return FALSE;
}
//遍历进程快照
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(pe32);
BOOL bMore = ::Process32First(hSnapshot, &pe32);
while (bMore)
{
if (!_strcmpi(pe32.szExeFile, sName))
{
dwPid = pe32.th32ProcessID;
::CloseHandle(hSnapshot);
return TRUE;
}
bMore = ::Process32Next(hSnapshot, &pe32);
}
//清除快照对象
::CloseHandle(hSnapshot);
return FALSE;
}
//通过进程ID获取窗口句柄
HWND GetWindowHandleByPID(DWORD dwProcessID)
{
HWND hWnd = GetTopWindow(0);
while (hWnd)
{
DWORD dwPid = 0;
DWORD dwTid = GetWindowThreadProcessId(hWnd, &dwPid);
if (0 != dwTid)
{
if (dwProcessID == dwPid)
{
return hWnd;
}
}
hWnd = GetNextWindow(hWnd, GWL_HWNDPARENT);
}
return NULL;
}
//===============枚举窗体===============
//通过进程ID获取窗口句柄(使用枚举窗体方法)
typedef struct tagWNDINFO
{
DWORD dwProcessId;
HWND hWnd;
}WNDINFO, *LPWNDINFO;
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
DWORD dwProcessId;
GetWindowThreadProcessId(hWnd, &dwProcessId);
LPWNDINFO pInfo = (LPWNDINFO)lParam;
if (dwProcessId == pInfo->dwProcessId)
{
pInfo->hWnd = hWnd;
return FALSE; //退出枚举
}
return TRUE; //继续枚举
}
HWND GetProcessMainWnd(DWORD dwProcessId)
{
WNDINFO wi;
wi.dwProcessId = dwProcessId;
wi.hWnd = NULL;
EnumWindows(EnumWindowsProc, (LPARAM)&wi);
return wi.hWnd;
}
//===============枚举窗体===============
//ID--HANDLE--HWND三者之间的互相转换
//id->句柄 hWnd = ::GetDlgItem(hParentWnd, id);
//id->指针 CWnd::GetDlgItem();
//句柄->id id = GetWindowLong(hWnd, GWL_ID);
//句柄->指针 CWnd *pWnd = CWnd::FromHandle(hWnd);
//指针->ID id = GetWindowLong(pWnd->GetSafeHwnd, GWL_ID);
//GetDlgCtrlID();
//指针->句柄 hWnd = cWnd.GetSafeHandle() or mywnd->m_hWnd;
//句柄->指针
CWnd* GetCwndByHwnd(HWND hWnd)
{
CWnd *pWnd = CWnd::FromHandle(hWnd);
return pWnd;
}
//指针->句柄
HWND GetCwndByHwnd(CWnd cWnd)
{
HWND hWnd = cWnd.GetSafeHwnd(); // mywnd->m_hWnd
return hWnd;
}
//=====================================================================================================
//通过窗口标题名杀进程
BOOL FindProcessHandleAndKill(const CString &sWndName)
{
HWND hWnd;
DWORD ProcessID;
HANDLE hProcess;
hWnd = ::FindWindow(NULL, sWndName);
if (hWnd == NULL)
{
return FALSE;
}
else
{
if (GetWindowThreadProcessId(hWnd, &ProcessID) == 0)
{
return FALSE;
}
else
{
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessID);
if (hProcess == NULL)
{
return FALSE;
}
else
{
if (TerminateProcess(hProcess, 0))
{
return TRUE;
}
else
{
return FALSE;
}
}
}
}
}