Results 1 to 3 of 3

Thread: Kill a process

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    5

    Angry

    HI everybody,

    I have some problem with my program...please help...

    my program needs to call up some CD. I use the following code to call the CD


    Public Function hWndShell(ByVal JobToDo As String, Optional ExecMode) As Long
    '
    ' Shells a new process and returns the hWnd
    ' of its main window.
    '
    Dim ProcessID As Long
    Dim PID As Long
    Dim hProcess As Long
    Dim hWndJob As Long

    If IsMissing(ExecMode) Then
    ExecMode = vbMinimizedNoFocus
    Else
    If ExecMode < vbHide Or ExecMode > vbMinimizedNoFocus Then
    ExecMode = vbMinimizedNoFocus
    End If
    End If

    On Error Resume Next
    ProcessID = Shell(JobToDo, CLng(ExecMode))
    If Err Then
    hWndShell = 0
    Exit Function
    End If
    On Error GoTo 0

    hWndJob = FindWindow(vbNullString, vbNullString)
    Do While hWndJob <> 0
    If GetParent(hWndJob) = 0 Then
    Call GetWindowThreadProcessId(hWndJob, PID)
    If PID = ProcessID Then
    hWndShell = hWndJob
    Exit Do
    End If
    End If
    hWndJob = GetWindow(hWndJob, GW_HWNDNEXT)
    Loop
    End Function

    The CD can be called successfully with the hwnd returned. However, my program needs to mointor the CD is running or not. When the CD is finished, I need to return the my main screen which is hide when the CD is calling. I want to know how can I find the CD is running or not just by the hwnd.

    Moreover, my program will kill the CD if it is idle too long. Now, I am using the code from Matthew Gates to kill the application by it's execuable. However, the CD cannot be called again if it is closed in such way until I reboot the PC. If I use the alt-ctrl-del and end task for the CD, then the CD can be called again... I want to know is there any different to kill an application from alt-ctrl-del and the code from Matthew Gates. How can I kill the application just like alt-ctrl-del with end task

    Thanks a lots......
    Yammy

    'Here is the code the Matthew Gates to kill a process

    Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
    Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

    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


    Public 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

    Private Sub Command1_Click()
    If Dir("C:\file.exe") <> "" Then
    Call KillApp("C:\file.exe")
    Kill ("C:\file.exe")
    Else
    Msgbox "Cannot delete file!", vbCritical
    End If
    End Sub

  2. #2
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722
    Try this API call:
    Code:
    Public Declare Sub ExitProcess Lib "kernel32" Alias "ExitProcess" (ByVal uExitCode As Long)

    r0ach™
    Don't forget to rate the post

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Posts
    5

    Unhappy

    What should I put the value for exitcode?????

    if I just call exitporcess(exitcode)
    then the VB program will be closed. In fact, I want to closing the CD/other application which called up by the program...

    Thanks

    Yammy

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