CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 1999
    Posts
    4

    How do I kill a proccess just knowing its name?

    How can I kill all instances of say test.exe programatically? Anyone know of how to do this or have a sample of how it is done?

    Thanks

    George


  2. #2
    Join Date
    Jun 1999
    Location
    Miami, FL
    Posts
    972

    Re: How do I kill a proccess just knowing its name?

    You basically need to get the process ID, pass it to OpenProcess, and then call TerminateProcess to kill it.

    The tricky part is getting the process ID and for that you can use the classes found here in CodeGuru. I found a nice set at http://www.codeguru.com/misc/process.shtml and here's how you can use them:


    CProcessList* pProcessList = CProcessList::Create();
    pProcessList->SnapShot();

    CString strFile = "c:\\the file you're interested in";

    for (CProcess* pProcess; (pProcess = pProcessList->GetNextProcess()); )
    {
    if (pProcess->GetFilename() == strFile)
    {
    HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pProcess->GetPID());
    TerminateProcess(hProcess, 0);
    CloseHandle(hProcess);
    }
    }




    Hope this helps!
    Alvaro


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

Featured