We help IT Professionals succeed at work.
Get Started

Another Process Killing Error Question

LAME-ER
LAME-ER asked
on
297 Views
Last Modified: 2012-08-13
Hi! I wanted to make a program that closes all other application except itself and the basic system applications (programs). This should work under Windows 98. After visiting a lot of sites that document this kind of things I came up with a code of my own…

#include <windows.h>
#include <tlhelp32.h>
#include <string>

.......

 BOOL CALLBACK TerminateAppEnum( HWND hwnd, LPARAM lParam )
   {
      DWORD dwID;

      GetWindowThreadProcessId(hwnd, &dwID);

      if(dwID == (DWORD)lParam)
      {
       PostMessage(hwnd, WM_DESTROY, 0, 0);
         PostMessage(hwnd, WM_QUIT, 0, 0);
      }

      return TRUE;
   }


GetSystemDirectory(lpBuffer, MAX_PATH);
String sysdir = lpBuffer;
GetWindowsDirectory(lpBuffer, MAX_PATH);
String windir = lpBuffer;
String ppathis;
char *ppath;

  ppathis = windir + "\\EXPLORER.EXE"; //Protected processes!
  ppath = ppathis.c_str();
  std::string strExeToFind1(ppath);
  ppathis = sysdir + "\\KERNEL32.DLL";
  ppath = ppathis.c_str();
  std::string strExeToFind2(ppath);
  ppathis = sysdir + "\\MSGSRV32.EXE";
  ppath = ppathis.c_str();
  std::string strExeToFind3(ppath);
  ppathis = sysdir + "\\MPREXE.EXE";
  ppath = ppathis.c_str();
  std::string strExeToFind4(ppath);
  ppathis = sysdir + "\\SYSTRAY.EXE";
  ppath = ppathis.c_str();
  std::string strExeToFind5(ppath);
  ppathis = sysdir + "\\WMIEXE.EXE";
  ppath = ppathis.c_str();
  std::string strExeToFind6(ppath);

  HANDLE hProcessSnap = 0;
  PROCESSENTRY32 pe32 = {0};
  // Take a snapshot of all processes in the system.
  hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  pe32.dwSize = sizeof(PROCESSENTRY32);
  // Walk the snapshot of the processes, and for each process,
  if (Process32First(hProcessSnap, &pe32)) {
      do {
            if (pe32.th32ProcessID != GetCurrentProcessId() && strExeToFind1.compare(pe32.szExeFile) && strExeToFind2.compare(pe32.szExeFile) && strExeToFind3.compare(pe32.szExeFile) && strExeToFind4.compare(pe32.szExeFile) && strExeToFind5.compare(pe32.szExeFile) && strExeToFind6.compare(pe32.szExeFile)) { //If it's not listed ...
            EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM) pe32.th32ProcessID);
            HANDLE hProc;
            hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pe32.th32ProcessID);
            if (hProc) {
                  TerminateProcess(hProc,-9);
                }
            CloseHandle(hProc);
            }
     } while (Process32Next(hProcessSnap, &pe32));
  }
  CloseHandle(hProcessSnap);


.......

Questions:

1. Can anybody optimize this code or even give me a better one ?!
2. The code works, but when I try to run a program after the code has been executed (and all processes have been terminated) I get an Explorer.exe Error (Invalid page fault in module <unknown> … ). I click close, Explorer.exe restarts by itself and then all is well (I only get this once), no matter how many times I try to run my application. Why is that?!!!!! Could this have anything to do with the fact that I am using Norton SystemWorks (because I found out that NPROTECT.EXE can’t be killed).
3. Are there any more “Protected processes” (the processes that my application should not kill) that I must add? Or can I remove some of them?

The point is that the system must not be affected in any way by this operation (the process killing). That means that I should still see the desktop and taskbar. I should still see “Explorer” and “Systray” when I press CTRL+ALT+DEL and no errors like the one I described should appear!
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE