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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

terminating a process

Status
Not open for further replies.

anddos

Programmer
Sep 5, 2004
13
GB
hello fellow coders :D
ok what i am trying to do is understand how to get this windows api
working.i am trying to get a list of the current process's in task
manager then Terminate the process's i specify
is it possible for someone to give me example's


i took the message below from some instructions.


that You'll first create a snapshot using CreateToolHelp32Snapshot,
next you'll
walk the processes list using Process32First, Process32Next. Don't
forget to
close the handle returned by CreateToolHelp32Snapshot using
CloseHandle.


 
Does it have to be in C++. You could do it in VBScript. Just google "VBScript WMI terminate". Loads of similar examples on how to do it.
 
this is a bit cut and paste from a project ive been working on but here goes

Code:
//demo code

#include "stdafx.h"
#include "windows.h"
#include <tlhelp32.h>
#include <iostream.h>
#include <sstream>
#include <string>
#include <iomanip>
#include <conio.h>

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// get OS version	
	OSVERSIONINFO OSVersion;
    OSVersion.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
    if(!GetVersionEx(&OSVersion)) return 0;

	HANDLE hProcess;
    
    if(OSVersion.dwPlatformId==VER_PLATFORM_WIN32_NT)// NT based OS
    {
        HINSTANCE hiPSAPI = LoadLibrary("PSAPI.DLL");
        if( hiPSAPI == NULL ) return 0;
        
        // PSAPI Function Pointers.
        typedef BOOL (WINAPI *LPENUMPROCESSES)(DWORD *,DWORD,DWORD *);
        typedef BOOL (WINAPI *LPENUMPROCESSMODULES)( HANDLE,HMODULE *,DWORD,LPDWORD);
        typedef DWORD (WINAPI *LPGETMODULEFILENAMEEX)( HANDLE,HMODULE,LPTSTR,DWORD);
        
        LPENUMPROCESSES lpfEnumProcesses = (LPENUMPROCESSES)GetProcAddress(hiPSAPI,"EnumProcesses");
        LPENUMPROCESSMODULES lpfEnumProcessModules = (LPENUMPROCESSMODULES)GetProcAddress(hiPSAPI,"EnumProcessModules");
        LPGETMODULEFILENAMEEX lpfGetModuleFileNameEx = (LPGETMODULEFILENAMEEX)GetProcAddress(hiPSAPI,"GetModuleFileNameExA");
        
        if(!lpfEnumProcesses||!lpfEnumProcessModules||!lpfGetModuleFileNameEx)
        {
        		//problem loading pointers to functions
            FreeLibrary(hiPSAPI);
            return 0;
        }
        
		//TODO: add loop to increase to max dwNeeded, set at 400 for test
        DWORD ProcessIDList[400],dwCb=400,dwCbNeeded,cbRet;
        
        if(!lpfEnumProcesses(ProcessIDList,dwCb,&dwCbNeeded))
        {
        		//enumprocesses failed
            FreeLibrary(hiPSAPI);
            return 0;
        }

        HMODULE hModule[50];
        DWORD i=0;
        char ModuleFileName[256];

        while(i<dwCbNeeded/sizeof(DWORD))
        {
            hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ|PROCESS_TERMINATE,0,ProcessIDList[i++]);
            if(hProcess)
            {
            	if(lpfEnumProcessModules(hProcess,hModule,50,&cbRet))
            	{
            		if(lpfGetModuleFileNameEx(hProcess,hModule[0],ModuleFileName,256))
            		{
            			//if your modulefilename matches kill process
            			TerminateProcess(hProcess,0);
            		}
            	}
				}
				cbRet=GetLastError();
            CloseHandle(hProcess);
        }
        FreeLibrary(hiPSAPI);
    }
    else if(OSVersion.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)// Win 9x 
    {
        HINSTANCE hiKernel32 = LoadLibrary("Kernel32.DLL");
        if(hiKernel32 == NULL) return 0;
        
        // 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,"CreateToolhelp32Snapshot");
        LPPROCESS32FIRST lpfProcess32First = (LPPROCESS32FIRST)GetProcAddress(hiKernel32,"Process32First");
        LPPROCESS32NEXT lpfProcess32Next = (LPPROCESS32NEXT)GetProcAddress(hiKernel32,"Process32Next");
        
        if(!lpfCreateToolhelp32Snapshot||!lpfProcess32First||!lpfProcess32Next)
        {
            FreeLibrary(hiKernel32);
            return 0;
        }
        
        HANDLE hProcessList = lpfCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
        if(!hProcessList)
        {
            FreeLibrary(hiKernel32);
            return 0;    // cannot take the snapshot
        }
        
        PROCESSENTRY32 Process;
        Process.dwSize = sizeof(PROCESSENTRY32);
        if(!lpfProcess32First(hProcessList,&Process))
        {
            CloseHandle(hProcessList);
            FreeLibrary(hiKernel32);
            return 0;
        }
        do
        {
            if(!strcmp(Process.szExeFile,"ProcessToKill.EXE"))
            {
                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);
    }

	return 0;
}

hope it helps.

(PS. credit to the writer who i cannot link because i cannot find the appropriate web page)

If somethings hard to do, its not worth doing - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top