Results 1 to 3 of 3

Thread: Must kill explorer

  1. #1

    Thread Starter
    Hyperactive Member Sneeden's Avatar
    Join Date
    Oct 2001
    Location
    Sneedville
    Posts
    258

    Must kill explorer

    I need to kill explorer. My approch is to kill the process. The problem is that I need the processID, or Handle to kill. What I need is a way to retrieve that handle or processID. Does anyone have a function they could post or if there is a better approach let me know. Need ASAP.

    Help!
    balls deep in bad code

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: Must kill explorer

    Check out the code posted in this thread:
    http://www.vbforums.com/showthread.php?t=249578

    Which will kill a process given it's file name ("explorer.exe").

    Hope this helps!
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  3. #3

    Thread Starter
    Hyperactive Member Sneeden's Avatar
    Join Date
    Oct 2001
    Location
    Sneedville
    Posts
    258

    Re: Must kill explorer

    Hey, thx a lot. That totally worked with a little tweaking for my application. Awsome! Here is the code in case anyone else is looking. The includes and such may be in the wrong order.... I pasted from my header files.

    Code:
    // the purpose of this project is to kill a process by it's name
    
    
    
    #using <mscorlib.dll>
    #include <iostream>
    #include <tchar.h>
    
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <tchar.h>
    #include <psapi.h>
    #pragma comment (lib, "psapi")
    
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <tchar.h>
    #include <psapi.h>
    #pragma comment (lib, "psapi")
    
    using namespace System;
    using namespace std;
    
    
    DWORD GetProcessIdFromName(LPCTSTR lpszName, BOOL bCaseSensitive);
    BOOL  KillProcess(DWORD dwProcessId, DWORD dwExitCode);
    BOOL  CloseProcess(DWORD dwProcessId);
    
    
    DWORD GetProcessIdFromName(LPCTSTR lpszName, BOOL bCaseSensitive);
    BOOL  KillProcess(DWORD dwProcessId, DWORD dwExitCode);
    BOOL  CloseProcess(DWORD dwProcessId);
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	if(argc == 1)
    	{
    		cout << "Usage: KillProcess FullyQualifiedProcessName\n";
    	}
    
    	// while an instance of the process is running
    	while(DWORD dwProc = GetProcessIdFromName(_T(argv[1]), FALSE))
    	{
    		// destroy!
    		KillProcess(dwProc, 0);
    		return 0;
    	}
    
    	cout << "Process could not be killed/n"
    		<< "Perhaps it is not running, or the fully qualified was not specified\n";
    
    	return 1;
    }
    
    // This kills a process given its process ID.
    BOOL KillProcess(DWORD dwProcessId, DWORD dwExitCode)
    {
    	// open process with rights to terminate
    	// opening a process means
    	// obtaining a local process handle
    	// given the ID.
    	HANDLE hProc = OpenProcess(PROCESS_TERMINATE , FALSE, dwProcessId);
    
    	// if opening succeeded
    	if (hProc)
    	{
    		// try to kill it
    		if (TerminateProcess(hProc, dwExitCode))
    		{
    			CloseHandle(hProc);
    			return TRUE;
    		}
    		CloseHandle(hProc);
    	}
    
    	// either OpenProcess or TerminateProcess failed.
    	return FALSE;
    }
    
    #define IDCOUNT 512
    
    // this gets the process ID for a given filename
    DWORD GetProcessIdFromName(LPCTSTR lpszName, BOOL bCaseSensitive)
    {
    	DWORD dwBytes;		// number of bytes returned
    	DWORD dwIDs[IDCOUNT];	// Process IDs.
    	DWORD dwCount;		// number of IDs returned
    
    	if (EnumProcesses(dwIDs, sizeof(dwIDs), &dwBytes))
    	{
    		dwCount = dwBytes / sizeof(DWORD);
    		for(DWORD i = 0; i < dwCount; i++)
    		{
    			// open process
    			// this will fail on some system processes
    			//like the Idle Process
    
    			// again: convert ID to handle
    			HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
    					PROCESS_VM_READ, FALSE,
    					dwIDs[i]);
    
    			if (hProcess)
    			{
    				// A buffer for the filename.
    				TCHAR szProcessName[MAX_PATH];
    				HMODULE hModule;
    
    				// Get the various files loaded by the process.
    				if (EnumProcessModules(hProcess, &hModule,
    						sizeof(hModule), &dwBytes))
    				{
    					int cmp;
    					// get the fully qualified name of the file
    					GetModuleFileNameEx(hProcess, hModule,
    							szProcessName,
    							MAX_PATH);
    
    					// compare it to the wanted name
    					if (bCaseSensitive)
    						cmp = _tcscmp(szProcessName, lpszName);
    					else
    						cmp = _tcsicmp(szProcessName, lpszName);
    					
    					// if they are equal, return the ID of this process
    					if (cmp == 0)
    					{
    						CloseHandle(hProcess);
    						return dwIDs[i];
    					}
    				}
    
    				CloseHandle(hProcess);
    			}
    
    		}
    	}
    
    	// didn't find the process, or something went wrong.
    	return 0;	// IDs are not pointers, so no sense in returning the pointer
    				
    }
    balls deep in bad code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width