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

    Killing a process

    Does anybody know how to kill a running process ??


  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: Killing a process

    use the TerminateProcess API.



  3. #3
    Join Date
    Sep 1999
    Posts
    24

    Re: Killing a process

    How would I implement that ???
    Can you give me an example....


  4. #4
    Join Date
    May 1999
    Posts
    3,332

    Re: Killing a process

    the hard part is getting the process handle.
    Let's say you called CreateProcess to start a process.
    you would then call OpenProcess with the following params:
    hProcess = OpenProcess(SYNCHRONIZE OR PROCESS_TERMINATE, 0, pi.dwProcessId)
    (get all the constant declarations from the APIViewer add-in)
    (pi is the process_information structure that is filled by CreatProcess.)

    Now, you are ready to kill the process by means of:
    TerminateProcess( hProcess, 0)

    Be sure to check all return codes of all api calls.


  5. #5
    Join Date
    Sep 1999
    Posts
    24

    Re: Killing a process

    But what if the process was not started with CreateProcess ??



  6. #6
    Join Date
    May 1999
    Posts
    3,332

    Re: Killing a process

    AFAIK the Shell function returns a Process ID (although the Documentation talks about a "task id"). You can get the Process handle from the Process id via OpenProcess API


  7. #7
    Join Date
    Sep 1999
    Posts
    24

    Re: Killing a process

    The thing is. I would like to have vb-source of the following:

    a list wich shows the path's of running processes (dll's exe's etc.)
    The option to select and kill any of these processes..

    The thing is I did find source of apps revealing processes and various source with wich you could kill processes (usually the process needs to have a window (titlebar), wich is not what we are looking for here), but never a combination of the two...


  8. #8
    Join Date
    Sep 1999
    Posts
    24

    Re: Killing a process

    Could you explain it to me in an example..
    Let's say f.i. c:\windows\notepad.exe is running..
    How do I find it and then kill it....(winthout using the caption from the titlebar or anything)


  9. #9
    Join Date
    Feb 2001
    Posts
    3

    Re: Killing a process

    using psapi, this function list all process, check name and if the boolean is true check the filename too (use the longname (if>8 char) without the .exe), then for your sample just call GetProcessIDByName(L"notepad", true);

    to find it.


    #include <Psapi.h>

    DWORD GetProcessIDByName(LPWSTR szName, bool bCheckFileName) {
    DWORD aProcesses[1024], cbNeeded, cProcesses;
    unsigned int i;

    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
    return 0;

    long l = wcslen(szName);
    WCHAR szProcessToFind[MAX_PATH];
    wcscpy(szProcessToFind, szName);
    if(wcsicmp(&szName[l-4], L".exe")) {
    wcscat(szProcessToFind, L".exe");
    }

    // Calculate how many process identifiers were returned.
    cProcesses = cbNeeded / sizeof(DWORD);

    // Print the name and process identifier for each process.
    for ( i = 0; i < cProcesses; i++ ) {
    WCHAR szProcessName[MAX_PATH] = L"unknown";

    // Get a handle to the process.
    HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i]);

    // Get the process name.
    if(hProcess) {
    HMODULE hMod;
    DWORD cbNeeded;

    if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
    GetModuleBaseNameW(hProcess, hMod, szProcessName, sizeof(szProcessName));
    if(wcsicmp(szProcessToFind, szProcessName) == 0) {
    CloseHandle( hProcess );
    return aProcesses[i];
    } else if(bCheckFileName) {
    WCHAR *p, szFileName[MAX_PATH] = L"unknown", szLongFileName[MAX_PATH] = L"unknown";

    GetModuleFileNameExW(hProcess, hMod, szFileName, sizeof(szFileName));
    p = wcsrchr(szFileName, '\\');
    if(wcsicmp(szProcessToFind, p+1) == 0) {
    CloseHandle( hProcess );
    return aProcesses[i];
    }
    if(wcschr(szFileName, '~')) {
    if(GetLongPathNameW(szFileName, szLongFileName, sizeof(szLongFileName))) {
    p = wcsrchr(szLongFileName, '\\');
    if(wcsicmp(szProcessToFind, p+1) == 0) {
    CloseHandle( hProcess );
    return aProcesses[i];
    }
    }
    }
    }
    }
    }
    CloseHandle(hProcess);
    }
    return 0;
    }





  10. #10
    Join Date
    Feb 2001
    Posts
    3

    Re: Killing a process

    [ ... i ... ] make my code italic...
    when you read aProcesses, you should add the index i : aProcesses [ i ]
    ccode is not enough ...



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