Results 1 to 7 of 7

Thread: Unable to kill process using ExitProcess

  1. #1
    mhuibers
    Guest
    We want to kill a process but it won't work with the ExitProcess API call.

    It 'dowsn't work because when we run that code VB is killed instead of the process we wan to be killed.

  2. #2
    Matthew Gates
    Guest
    Try this:


    Code:
    Private Declare Function ProcessFirst _
    Lib "kernel32" Alias "Process32First" (ByVal hSnapshot _
    As Long, uProcess As PROCESSENTRY32) As Long
    
    Private Declare Function ProcessNext Lib "kernel32" _
    Alias "Process32Next" (ByVal hSnapshot As Long, _
    uProcess As PROCESSENTRY32) As Long
    
    Private Declare Function CreateToolhelpSnapshot _
    Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal _
    lFlags As Long, lProcessID As Long) As Long
    
    Private Declare Function CloseHandle Lib "kernel32" _
    (ByVal hObject As Long) As Long
    
    Private Declare Function OpenProcess Lib "kernel32" _
    (ByVal dwDesiredAccess As Long, ByVal bInheritHandle _
    As Long, ByVal dwProcessId As Long) As Long
    
    Private Declare Function TerminateProcess _
    Lib "kernel32" (ByVal hProcess As Long, ByVal _
    uExitCode As Long) As Long
    
    
    Private Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long
        th32DefaultHeapID As Long
        th32ModuleID As Long
        cntThreads As Long
        th32ParentProcessID As Long
        pcPriClassBase As Long
        dwFlags As Long
        szexeFile As String * MAX_PATH
        End Type
    
    
    Private Const MAX_PATH = 260
    
    
    Private Function KillApp(myName As String) As Boolean
        Const PROCESS_ALL_ACCESS = 0
        Dim uProcess As PROCESSENTRY32
        Dim rProcessFound As Long
        Dim hSnapshot As Long
        Dim szExename As String
        Dim exitCode As Long
        Dim myProcess As Long
        Dim AppKill As Boolean
        Dim appCount As Integer
        Dim i As Integer
        On Local Error GoTo Finish
        appCount = 0
        
        Const TH32CS_SNAPPROCESS As Long = 2&
        
        uProcess.dwSize = Len(uProcess)
        hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
        rProcessFound = ProcessFirst(hSnapshot, uProcess)
        
        Do While rProcessFound
            i = InStr(1, uProcess.szexeFile, Chr(0))
            szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
            If Right$(szExename, Len(myName)) = LCase$(myName) Then
                KillApp = True
                appCount = appCount + 1
                myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
                AppKill = TerminateProcess(myProcess, exitCode)
                Call CloseHandle(myProcess)
            End If
            rProcessFound = ProcessNext(hSnapshot, uProcess)
        Loop
    
        Call CloseHandle(hSnapshot)
    Finish:
    End Function

  3. #3
    mhuibers
    Guest

    Question CreateToolhelpSnapshot

    I already tried your code but it does not work.
    Because we get an error like DDL ENTRY POINT......CreateToolhelpSnapshot . etc etc,

    What we try to do is this: ...out application uses WINWORD 2000 but sometimes Word is still in memory and makes the system hang. In that particular we want to kill the process by using API calls, so we thought that the ExitProcess API should do the trick. The only thing is that we cannot can get it to work

    Michiel

    PS we use a winNT 4.0 SP 6 system

  4. #4
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    The CreateToolhelp32Snapshot entry point is defined in IMAGEHLP.DLL, not KERNEL32.DLL.

    Replace Lib "kernel32" with Lib "imagehlp" in the first Declare statement and try again.

  5. #5
    Matthew Gates
    Guest
    Originally posted by Yonatan
    The CreateToolhelp32Snapshot entry point is defined in IMAGEHLP.DLL, not KERNEL32.DLL.

    Replace Lib "kernel32" with Lib "imagehlp" in the first Declare statement and try again.
    It's not? Why does the code work on Win98 then?

  6. #6
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Hmm, oh wait, it's defined in both
    But I guess not in NT

  7. #7
    Tygur
    Guest
    CreateToolhelp32Snapshot doesn't work in Windows NT. It works fine in Windows 95, 98 and 2000, but not Windows NT. I was gonna used it in one of my own programs one time, but I didn't because of that.

    Also, the reason why ExitProcess kept closing your program is because that's what it is supposed to do.

    If you can find another way of getting the handle to the process, you can use TerminateProcess to close it as it is used in the above code posted by Matthew Gates.

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