Search This Blog

Loading...

Wednesday, December 24, 2008

MFC Code to get Process Name & Process ID and kill by process ID

#include "Tlhelp32.h"
#include "winbase.h"


// Function to kill Process using Process ID
int Killprocess(int nProcessID)
{
HANDLE hProcess = OpenProcess( PROCESS_TERMINATE, 0, nProcessID );
if (hProcess == NULL)
return -1;
BOOL nReturnVal = TerminateProcess( hProcess, 9 );
CloseHandle (hProcess);
return nReturnVal;
}


// Function to get all Process Name and Process ID
CString GetProcessNameandID()
{
HANDLE hSnapShot;
hSnapShot=CreateToolhelp32Snapshot (TH32CS_SNAPALL,NULL);
PROCESSENTRY32 pEntry;
pEntry.dwSize =sizeof(pEntry);
CString csProcessList;
DWORD dwID;
CString csProcessID;
//Get first process
Process32First (hSnapShot,&pEntry);
//Get all processes
while(1)
{
BOOL hRes=Process32Next (hSnapShot,&pEntry);
if(hRes==FALSE)
break;
csProcessList=csProcessList+pEntry.szExeFile;
dwID=pEntry.th32ProcessID;
csProcessID.Format("%i",dwID);
csProcessList=csProcessList +" "+csProcessID +"\r\n" ;
}
return csProcessList;
}

2 comments:

windchaser said...

Thanks for posting. It works really well.

windchaser said...
This comment has been removed by the author.