//Reference: MSDN
//http://msdn2.microsoft.com/en-us/library/ms686714.aspx --> terminate process
//http://msdn2.microsoft.com/en-gb/library/ms682629.aspx
//http://msdn2.microsoft.com/en-gb/library/ms682631.aspx
//Some other links on MSDN website
//Please, visit MSDN website for more detailed information
#define UNICODE
#ifdef UNICODE
#define _UNICODE
#endif
#define DEV_CPP
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#ifdef VISUAL_CPP
#define NOT_USE_DEV_CPP
#include <psapi.h>//-->For using EnumProcesses,...Source:MSDN
#endif
#ifdef NOT_USE_DEV_CPP
#undef DEV_CPP
#endif
#ifdef DEV_CPP
#define EnumProcesses pfnEnumProcesses
#define EnumProcessModules pfnEnumProcessModules
#define GetModuleBaseName pfnGetModuleBaseName
#endif
#ifdef DEV_CPP
typedef int (WINAPI *PFNINT_PSAPI_EP) (DWORD*, DWORD, DWORD*);
PFNINT_PSAPI_EP pfnEnumProcesses=NULL;
typedef int (WINAPI *PFNINT_PSAPI_EPM) (HANDLE, HMODULE*, DWORD, DWORD*);
PFNINT_PSAPI_EPM pfnEnumProcessModules=NULL;
typedef int (WINAPI *PFNINT_PSAPI_GMBN) (HANDLE, HMODULE, LPTSTR, DWORD);
PFNINT_PSAPI_GMBN pfnGetModuleBaseName=NULL;
#endif
HINSTANCE hLibrary=NULL;
BOOL GetProcessIdByName(LPTSTR lpszCheckedProcessName, DWORD& dwProcessID)
{
DWORD aProcesses[1024];
DWORD cbNeeded, cProcesses;
unsigned int i;
//Find all running processes
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
{
MessageBox(NULL,TEXT("Error when executing EnumProcesses function"),TEXT("Error"),MB_ICONERROR);
return FALSE;
}
//Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
//It is a good idea to use a large array,
//because it is hard to predict how many processes
//there will be at the time you call EnumProcesses.
//To determine how many processes were enumerated,
//divide the pBytesReturned value (cbNeeded) by sizeof(DWORD).
//There is no indication given when the buffer is too small to store all process identifiers.
//Therefore, if pBytesReturned (cbNeeded) equals cb (sizeof(aProcesses)), consider retrying the call with a larger array.
//Source:MSDN
HANDLE hProcess;
HMODULE hMod;
TCHAR szProcessName[MAX_PATH];
for ( i = 0; i < cProcesses; i++ )
{
if(aProcesses[i]!=0)
{
hProcess=OpenProcess( PROCESS_QUERY_INFORMATION |PROCESS_VM_READ,FALSE, aProcesses[i]);
if(hProcess)
{
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) )
{
GetModuleBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName)/sizeof(TCHAR) );
if(_tcscmp(szProcessName,lpszCheckedProcessName)==0)
{
dwProcessID=aProcesses[i];
return TRUE;
}
}
}
}
}
//Warning:An application may have multiple instances.
//If you want to save all processids, you should use an array instead
return FALSE;
}
void PrintProcessNameAndID( DWORD processID )
{
TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
//MAX_PATH = 260
// Get a handle to the process.
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
// Get the process name.
if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded;
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName)/sizeof(TCHAR) );
}
}
// Print the process name and identifier.
_tprintf( TEXT("%s (PID: %u)\n"), szProcessName, processID );
CloseHandle( hProcess );
}
int main( )
{
#ifdef DEV_CPP
hLibrary=LoadLibrary(TEXT("psapi.dll"));
if(hLibrary==NULL)
{
MessageBox(NULL,TEXT("Error when loading DLL"),TEXT("Msg"),MB_ICONERROR);
return 0;
}
EnumProcesses = (PFNINT_PSAPI_EP) GetProcAddress (hLibrary, "EnumProcesses");
EnumProcessModules = (PFNINT_PSAPI_EPM) GetProcAddress (hLibrary, "EnumProcessModules");
#ifndef UNICODE
GetModuleBaseName = (PFNINT_PSAPI_GMBN) GetProcAddress (hLibrary, "GetModuleBaseNameA");
#else
GetModuleBaseName = (PFNINT_PSAPI_GMBN) GetProcAddress (hLibrary, "GetModuleBaseNameW");
#endif
if((EnumProcesses==NULL)||(EnumProcessModules==NULL)||(GetModuleBaseName==NULL))
{
MessageBox(NULL,TEXT("Error when loading function"),TEXT("Msg"),MB_ICONERROR);
return 0;
}
#endif
// Get the list of process identifiers.
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return 0;
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the name and process identifier for each process.
for ( i = 0; i < cProcesses; i++ )
if( aProcesses[i] != 0 )
PrintProcessNameAndID( aProcesses[i] );
DWORD dwProcessId=0;
unsigned int uiRadix=10;
TCHAR szProcessId[10];
if(GetProcessIdByName(TEXT("iexplore.exe"),dwProcessId))
{
_ltot(dwProcessId,szProcessId,uiRadix);//convert dwProcessId to string
MessageBox(NULL,szProcessId,TEXT("This is the ProcessID of iexplore.exe"),MB_OK);
HANDLE hp = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE,FALSE,dwProcessId);
if(hp)
{
if(TerminateProcess(hp,0))
MessageBox(NULL,TEXT("iexplore.exe terminated successfully"),TEXT("Message"),MB_OK);
}
CloseHandle(hp);
}
#ifdef DEV_CPP
FreeLibrary(hLibrary);
#endif
return 0;
}