////////////头文件////////////////////////////////////////////////////////////
#include <stdio.h>
#include <windows.h>
#include <tlhelp32.h>
////////////宏定义////////////////////////////////////////////////////////////
////////////全局变量//////////////////////////////////////////////////////////
////////////函数定义//////////////////////////////////////////////////////////
DWORD WINAPI GetPIDbyName(LPTSTR lpName)
{
HANDLE m_Snap = INVALID_HANDLE_VALUE;
PROCESSENTRY32 pe = {sizeof(pe)};
DWORD ret = NULL;
{
m_Snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,NULL);
if (m_Snap == INVALID_HANDLE_VALUE) goto finally;
if (!Process32First(m_Snap, &pe)) goto finally;
do if(!lstrcmpi(pe.szExeFile,lpName))
{
ret = pe.th32ProcessID;
goto finally;
}
while (Process32Next(m_Snap, &pe));
}
finally: {
if (m_Snap != INVALID_HANDLE_VALUE)
CloseHandle(m_Snap);
}
return ret;
}
bool TerminateAProcess(DWORD dwPid)
{
HANDLE hThreadSnap = NULL;
THREADENTRY32 te32;
BOOL bThreadFind = FALSE;
hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,NULL);
if(hThreadSnap)
{
te32.dwSize = sizeof(THREADENTRY32);
bThreadFind = Thread32First(hThreadSnap,&te32);
while(bThreadFind)
{
if(te32.th32OwnerProcessID == dwPid)
{
HANDLE hThread = NULL;
hThread = OpenThread(THREAD_ALL_ACCESS,FALSE,te32.th32ThreadID);
if(hThread)
{
TerminateThread(hThread,0);
CloseHandle(hThread);
}
}
te32.dwSize = sizeof(THREADENTRY32);
bThreadFind = Thread32Next(hThreadSnap,&te32);
}
CloseHandle(hThreadSnap);
}
return TRUE;
}
int main(int argc, char *argv[])
{
TerminateAProcess(GetPIDbyName("crackmeapp.exe"));
return 0;
}