Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the process handle?

Status
Not open for further replies.

manumohan

Programmer
Apr 9, 2003
4
0
0
IN
Friends

I want to terminate a process. The first parametre to the API TerminateProcess is the process handle. How i can get a valid process handle.

thanx in advance
 
The following code fragment looks for any process you wish to kill. You just have to replace the "ProcessToKill.exe" with the desired process.

Code:
	OSVERSIONINFO OSVersion;
	OSVersion.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
	if(!GetVersionEx(&OSVersion)) return;
	
	HANDLE hProcess;
	
	if(OSVersion.dwPlatformId==VER_PLATFORM_WIN32_NT)// NT based OS
	{
		HINSTANCE hiPSAPI = LoadLibrary("PSAPI.DLL");
		if( hiPSAPI == NULL ) return;
		
		// PSAPI Function Pointers.
		typedef BOOL (WINAPI *LPENUMPROCESSES)(DWORD *,DWORD,DWORD *);
		typedef BOOL (WINAPI *LPENUMPROCESSMODULES)( HANDLE,HMODULE *,DWORD,LPDWORD);
		typedef DWORD (WINAPI *LPGETMODULEBASENAME)( HANDLE,HMODULE,LPTSTR,DWORD);
		
		LPENUMPROCESSES lpfEnumProcesses = (LPENUMPROCESSES)GetProcAddress(hiPSAPI,"EnumProcesses");
		LPENUMPROCESSMODULES lpfEnumProcessModules = (LPENUMPROCESSMODULES)GetProcAddress(hiPSAPI,"EnumProcessModules");
		LPGETMODULEBASENAME lpfGetModuleBaseName = (LPGETMODULEBASENAME)GetProcAddress(hiPSAPI,"GetModuleBaseNameA");
		
		if(!lpfEnumProcesses||!lpfEnumProcessModules||!lpfGetModuleBaseName)
		{
			FreeLibrary(hiPSAPI);
			return;
		}
		
		DWORD ProcessIDList[100],dwCb=100,dwCbNeeded,cbRet;
		
		if(!lpfEnumProcesses(ProcessIDList,dwCb,&dwCbNeeded))
		{
			FreeLibrary(hiPSAPI);
			return;
		}
		
		HMODULE hModule[50];
		char ModuleName[50];
		unsigned char i=0;
		while(i<dwCbNeeded/sizeof(DWORD))
		{
			hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ|PROCESS_TERMINATE,0,ProcessIDList[i++]);
			if(hProcess)
			{
				cbRet=0;
				if(lpfEnumProcessModules(hProcess,hModule,50,&cbRet))
					if(lpfGetModuleBaseName(hProcess,hModule[0],ModuleName,50))
						if(!strcmp(ModuleName,&quot;ProcessToKill.EXE&quot;))
						{
							TerminateProcess(hProcess,0);
							CloseHandle(hProcess);
							break;
						}
						cbRet=GetLastError();
						CloseHandle(hProcess);
			}
		}
		FreeLibrary(hiPSAPI);
	}
	else if(OSVersion.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)// Win 9x 
	{
		HINSTANCE hiKernel32 = LoadLibrary(&quot;Kernel32.DLL&quot;);
		if(hiKernel32 == NULL) return;
		
		// ToolHelp32 Function Pointers.
		typedef HANDLE (WINAPI *LPCREATETOOLHELP32SNAPSHOT)(DWORD,DWORD);
		typedef BOOL (WINAPI *LPPROCESS32FIRST)(HANDLE,LPPROCESSENTRY32);
		typedef BOOL (WINAPI *LPPROCESS32NEXT)(HANDLE,LPPROCESSENTRY32);
		
		LPCREATETOOLHELP32SNAPSHOT lpfCreateToolhelp32Snapshot = (LPCREATETOOLHELP32SNAPSHOT)GetProcAddress(hiKernel32,&quot;CreateToolhelp32Snapshot&quot;);
		LPPROCESS32FIRST lpfProcess32First = (LPPROCESS32FIRST)GetProcAddress(hiKernel32,&quot;Process32First&quot;);
		LPPROCESS32NEXT lpfProcess32Next = (LPPROCESS32NEXT)GetProcAddress(hiKernel32,&quot;Process32Next&quot;);
		
		if(!lpfCreateToolhelp32Snapshot||!lpfProcess32First||!lpfProcess32Next)
		{
			FreeLibrary(hiKernel32);
			return;
		}
		
		HANDLE hProcessList = lpfCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
		if(!hProcessList)
		{
			FreeLibrary(hiKernel32);
			return;	// cannot take the snapshot
		}
		
		PROCESSENTRY32 Process;
		Process.dwSize = sizeof(PROCESSENTRY32);
		if(!lpfProcess32First(hProcessList,&Process))
		{
			CloseHandle(hProcessList);
			FreeLibrary(hiKernel32);
			return;
		}
		do
		{
			if(!strcmp(Process.szExeFile,&quot;ProcessToKill.EXE&quot;))
			{
				hProcess = OpenProcess(PROCESS_TERMINATE,0,Process.th32ProcessID);
				if(!hProcess) break;// cannot open the process				
				TerminateProcess(hProcess,0);
				CloseHandle(hProcess);
				break;
			}
		}while (lpfProcess32Next(hProcessList,&Process));
		
		CloseHandle(hProcessList);
		FreeLibrary(hiKernel32);
	}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top