Page 1 of 2 12 LastLast
Results 1 to 40 of 51

Thread: Detect if Process is 'Not Responding' [Almost RESOLVED]

  1. #1

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575

    Detect if Process is 'Not Responding' [Almost RESOLVED]

    Hey everyone,

    I searched for how you would detect whether a certain process is Not Responding or not, but I didnt have any luck - sorry if it has been answered elsewhere though.

    What the problem is, I have this application which needs to detect when a certain process is 'Not Responding'.

    This process is called "Game.exe" so it needs to be exactly like that.

    If it finds that it is 'Not Responding' how would I end it? A simple

    SendMessage lWnd, WM_CLOSE, 0&, 0&

    should work really, because I can still use the Window name as that doesnt change.

    Thanks for any help! This program is really going to help me and my friends when its finished.

    P.S. If this will end up being done with a loop, how much resources are we expecting to use up? It doesnt matter much at all, but I am just asking. (If there are problems at the end with lagging "Game.exe" at all, I can try and find a different solution) - thanks again
    Last edited by LITHIA; Aug 28th, 2003 at 01:12 PM.

  2. #2

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    I tested the

    SendMessage lWnd, WM_CLOSE, 0&, 0&

    against a Not Responding app...

    as I thought after posting the main thread, it didnt work.

    The app is not responding so it cant respond to the Close request (which is pretty obvious now you think about it)

    So ill need another method of ending the process.

    Thanks

    LITHIA

  3. #3
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871
    Hmm, I came around this code once that enumerated all active processes and could also 'terminate' one; don't know how it worked, but it sounds like it exactly what you're looking for. I found it somewhere on www.vbcode.com; try searching for 'processes' or 'active processes'.
    Author for Visual Basic Web Magazine

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Use SendMessageTimeOut.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SMTO Lib "user32" Alias "SendMessageTimeoutA" (ByVal hwnd As Long, _
    4.                                                                         ByVal msg As Long, _
    5.                                                                         ByVal wParam As Long, _
    6.                                                                         ByVal lParam As Long, _
    7.                                                                         ByVal fuFlags As Long, _
    8.                                                                         ByVal uTimeout As Long, _
    9.                                                                         lpdwResult As Long) _
    10.                                                                         As Long
    11. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
    12.                                                                       ByVal lpWindowName As String) _
    13.                                                                       As Long
    14.  
    15. Private Const SMTO_ABORTIFHUNG = &H2
    16. Private Const SMTO_BLOCK = &H1
    17. Private Const SMTO_NORMAL = &H0
    18.  
    19. Private Const WM_CLOSE = &H10
    20.  
    21. Private Sub Command2_Click()
    22. Dim h As Long
    23.  
    24.     h = FindWindow(vbNullString, "Hung Process")
    25.  
    26.     If SMTO(h, WM_CLOSE, 0&, 0&, SMTO_ABORTIFHUNG, 3000, 0&) = 0& Then
    27.         MsgBox "Process is hung!"
    28.     Else
    29.         MsgBox "Process is not hung!"
    30.     End If
    31.  
    32. End Sub
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    Originally posted by crptcblade
    Use SendMessageTimeOut.
    thanks very much for the code. It looks great, but what code could I use to Terminate the Process where it says

    MsgBox "Process is hung!"

    Thats where I will want to add the end process code.

    And

    MsgBox "Process is not hung!"

    Will be just Exit Sub
    VB Code:
    1. Dim h As Long
    2.     h = FindWindow(vbNullString, "Hung Process")
    3.     If SMTO(h, WM_CLOSE, 0&, 0&, SMTO_ABORTIFHUNG, 3000, 0&) = 0& Then

    I have put this on a 2 second timer. That way it will detect if the process is hung every 2 seconds. Good enough?

    Thanks again,

    TheVader, thanks for the tip ill try it out and tell you what i get. Cya

    (Please can somone still try and help with the end process code though, I may not be able to find it but I should from TheVaders help )

  6. #6

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    Hmm i just realised somthing.

    I dont understand what your code is doing.

    If SMTO(h, WM_CLOSE, 0&, 0&, SMTO_ABORTIFHUNG, 3000, 0&) = 0& Then

    seems to close the window even if its not hung!

    (and if it was hung, i dont think it would work because it sends the close request to the program doesnt it?)

    Like i said before, it cant accept a close request if its hung. You need windows to end its process.

    Thanks for help, can you stop it from closing the window?

    Thanks

  7. #7
    Member
    Join Date
    Nov 1999
    Posts
    61
    By the way, Anybody knows Where I can find API docs ?

    Thanks alot.

  8. #8

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    www.allapi.net i believe gives u lots of stuff, and a nice viewer u can download

  9. #9
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871
    I just came across this code, it might be helpful. Didn't test it, but it should terminate a process given its ID.
    VB Code:
    1. Private Type LUID
    2.   lowpart As Long
    3.   highpart As Long
    4. End Type
    5.  
    6. Private Type TOKEN_PRIVILEGES
    7.    PrivilegeCount As Long
    8.    LuidUDT As LUID
    9.    Attributes As Long
    10. End Type
    11.  
    12. Const TOKEN_ADJUST_PRIVILEGES = &H20
    13. Const TOKEN_QUERY = &H8
    14. Const SE_PRIVILEGE_ENABLED = &H2
    15. Const PROCESS_ALL_ACCESS = &H1F0FFF
    16.  
    17. Private Declare Function GetVersion Lib "kernel32" () As Long
    18. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    19. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As _
    20.    Long
    21. Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle _
    22.    As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
    23. Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias _
    24.    "LookupPrivilegeValueA" (ByVal lpSystemName As String, _
    25.    ByVal lpName As String, lpLuid As LUID) As Long
    26. Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal _
    27.    TokenHandle As Long, ByVal DisableAllPrivileges As Long, _
    28.    NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, _
    29.    PreviousState As Any, ReturnLength As Any) As Long
    30. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As _
    31.    Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    32. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As _
    33.    Long, ByVal uExitCode As Long) As Long
    34.  
    35. ' Terminate any application and return an exit code to Windows
    36. ' This works under NT/2000, even when the calling process
    37. ' doesn't have the privilege to terminate the application
    38. ' (for example, this may happen when the process was launched
    39. '  by yet another program)
    40. '
    41. ' Usage:  Dim pID As Long
    42. '         pID = Shell("Notepad.Exe", vbNormalFocus)
    43. '         '...
    44. '         If KillProcess(pID, 0) Then
    45. '             MsgBox "Notepad was terminated"
    46. '         End If
    47.  
    48. Function KillProcess(ByVal hProcessID As Long, Optional ByVal ExitCode As Long) _
    49.    As Boolean
    50.    Dim hToken As Long
    51.    Dim hProcess As Long
    52.    Dim tp As TOKEN_PRIVILEGES
    53.    
    54.    ' Windows NT/2000 require a special treatment
    55.    ' to ensure that the calling process has the
    56.    ' privileges to shut down the system
    57.    
    58.    ' under NT the high-order bit (that is, the sign bit)
    59.    ' of the value retured by GetVersion is cleared
    60.    If GetVersion() >= 0 Then
    61.        ' open the tokens for the current process
    62.        ' exit if any error
    63.        If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or _
    64.            TOKEN_QUERY, hToken) = 0 Then
    65.            GoTo CleanUp
    66.        End If
    67.        
    68.        ' retrieves the locally unique identifier (LUID) used
    69.        ' to locally represent the specified privilege name
    70.        ' (first argument = "" means the local system)
    71.        ' Exit if any error
    72.        If LookupPrivilegeValue("", "SeDebugPrivilege", tp.LuidUDT) = 0 Then
    73.            GoTo CleanUp
    74.        End If
    75.    
    76.        ' complete the TOKEN_PRIVILEGES structure with the # of
    77.        ' privileges and the desired attribute
    78.        tp.PrivilegeCount = 1
    79.        tp.Attributes = SE_PRIVILEGE_ENABLED
    80.    
    81.        ' try to acquire debug privilege for this process
    82.        ' exit if error
    83.        If AdjustTokenPrivileges(hToken, False, tp, 0, ByVal 0&, _
    84.            ByVal 0&) = 0 Then
    85.            GoTo CleanUp
    86.        End If
    87.    End If
    88.    
    89.    ' now we can finally open the other process
    90.    ' while having complete access on its attributes
    91.    ' exit if any error
    92.    hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)
    93.    If hProcess Then
    94.        ' call was successful, so we can kill the application
    95.        ' set return value for this function
    96.        KillProcess = (TerminateProcess(hProcess, ExitCode) <> 0)
    97.        ' close the process handle
    98.        CloseHandle hProcess
    99.    End If
    100.    
    101.    If GetVersion() >= 0 Then
    102.        ' under NT restore original privileges
    103.        tp.Attributes = 0
    104.        AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0&
    105.        
    106. CleanUp:
    107.        If hToken Then CloseHandle hToken
    108.    End If
    109. End Function
    Author for Visual Basic Web Magazine

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

  10. #10

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    thanks for the code Vader,

    only prob is i get this error when It trys terminating the process

    Run Time Error: '53'

    File Not Found
    I have made sure that making pID equal the correct process as "Game.exe" as it shows in Task Manager.

    Any ideas? Thanks

    Btw, i cleaned alot of crap i didnt need. Like that NT/2000 verification rubbish

    When I get the error, on Debug it highlights

    pID = Shell("Game.exe", vbNormalFocus)

  11. #11

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    *bump*

  12. #12
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871
    I just tried the code myself and it's working fine. The process is terminated (it didn't hang though). I used exactly this code:
    VB Code:
    1. Option Explicit
    2.  
    3. Dim pID As Variant
    4.  
    5. Private Type LUID
    6.   lowpart As Long
    7.   highpart As Long
    8. End Type
    9.  
    10. Private Type TOKEN_PRIVILEGES
    11.    PrivilegeCount As Long
    12.    LuidUDT As LUID
    13.    Attributes As Long
    14. End Type
    15.  
    16. Const TOKEN_ADJUST_PRIVILEGES = &H20
    17. Const TOKEN_QUERY = &H8
    18. Const SE_PRIVILEGE_ENABLED = &H2
    19. Const PROCESS_ALL_ACCESS = &H1F0FFF
    20.  
    21. Private Declare Function GetVersion Lib "kernel32" () As Long
    22. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    23. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As _
    24.    Long
    25. Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle _
    26.    As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
    27. Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias _
    28.    "LookupPrivilegeValueA" (ByVal lpSystemName As String, _
    29.    ByVal lpName As String, lpLuid As LUID) As Long
    30. Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal _
    31.    TokenHandle As Long, ByVal DisableAllPrivileges As Long, _
    32.    NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, _
    33.    PreviousState As Any, ReturnLength As Any) As Long
    34. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As _
    35.    Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    36. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As _
    37.    Long, ByVal uExitCode As Long) As Long
    38.  
    39. ' Terminate any application and return an exit code to Windows
    40. ' This works under NT/2000, even when the calling process
    41. ' doesn't have the privilege to terminate the application
    42. ' (for example, this may happen when the process was launched
    43. '  by yet another program)
    44. '
    45. ' Usage:  Dim pID As Long
    46. '         pID = Shell("Notepad.Exe", vbNormalFocus)
    47. '         '...
    48. '         If KillProcess(pID, 0) Then
    49. '             MsgBox "Notepad was terminated"
    50. '         End If
    51.  
    52. Function KillProcess(ByVal hProcessID As Long, Optional ByVal ExitCode As Long) _
    53.    As Boolean
    54.    Dim hToken As Long
    55.    Dim hProcess As Long
    56.    Dim tp As TOKEN_PRIVILEGES
    57.    
    58.    ' Windows NT/2000 require a special treatment
    59.    ' to ensure that the calling process has the
    60.    ' privileges to shut down the system
    61.    
    62.    ' under NT the high-order bit (that is, the sign bit)
    63.    ' of the value retured by GetVersion is cleared
    64.    If GetVersion() >= 0 Then
    65.        ' open the tokens for the current process
    66.        ' exit if any error
    67.        If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or _
    68.            TOKEN_QUERY, hToken) = 0 Then
    69.            GoTo CleanUp
    70.        End If
    71.        
    72.        ' retrieves the locally unique identifier (LUID) used
    73.        ' to locally represent the specified privilege name
    74.        ' (first argument = "" means the local system)
    75.        ' Exit if any error
    76.        If LookupPrivilegeValue("", "SeDebugPrivilege", tp.LuidUDT) = 0 Then
    77.            GoTo CleanUp
    78.        End If
    79.    
    80.        ' complete the TOKEN_PRIVILEGES structure with the # of
    81.        ' privileges and the desired attribute
    82.        tp.PrivilegeCount = 1
    83.        tp.Attributes = SE_PRIVILEGE_ENABLED
    84.    
    85.        ' try to acquire debug privilege for this process
    86.        ' exit if error
    87.        If AdjustTokenPrivileges(hToken, False, tp, 0, ByVal 0&, _
    88.            ByVal 0&) = 0 Then
    89.            GoTo CleanUp
    90.        End If
    91.    End If
    92.    
    93.    ' now we can finally open the other process
    94.    ' while having complete access on its attributes
    95.    ' exit if any error
    96.    hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)
    97.    If hProcess Then
    98.        ' call was successful, so we can kill the application
    99.        ' set return value for this function
    100.        KillProcess = (TerminateProcess(hProcess, ExitCode) <> 0)
    101.        ' close the process handle
    102.        CloseHandle hProcess
    103.    End If
    104.    
    105.    If GetVersion() >= 0 Then
    106.        ' under NT restore original privileges
    107.        tp.Attributes = 0
    108.        AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0&
    109.        
    110. CleanUp:
    111.        If hToken Then CloseHandle hToken
    112.    End If
    113. End Function
    114.  
    115. Private Sub Command1_Click()
    116. KillProcess pID
    117. End Sub
    118.  
    119. Private Sub Form_Load()
    120. pID = Shell("c:\nav.exe", vbNormalFocus)
    121. End Sub
    Author for Visual Basic Web Magazine

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

  13. #13
    Addicted Member
    Join Date
    Aug 2003
    Location
    houston
    Posts
    185
    I found this vb script using maybe u could use scription to do it
    [vbScript: ]

    'kill all non allowed processess !
    '
    'by Peter Boos, based on other kill scripts posted to the great site CWASHINGTON.NETREACH.NET !
    '
    'To prevent killing this application also taskmgr.exe is not allowed
    'If you want to allow different applications.
    'Then before running this script.
    ' Then place a rem ' for the terminate line
    ' And remove the rem ' from x=x & c &... and from the line wscript.echo(x)
    ' Then this script will show the full names off all running applications
    ' And then you can add that programs full name (case sensitive)
    '
    'log off to disable.
    'And if you don't have this in your login script or startup then you are rid of it.
    'This script keeps running, if you can't hide you can't run.

    system1 = "System Idle Process System smss.exe csrss.exe winlogon.exe services.exe "
    system2 = "svchost.exe spoolsv.exe cisvc.exe regsvc.exe hh.exe MSTask.exe "
    system3 = "Explorer.exe WinMgmt.exe WScript.exe lsass.exe cidaemon.exe "
    allsystem2000 = system1 & system2 &system3

    allowed = allsystem2000 & "Notepad.exe IExplorer.exe" 'now you're only alowed to run Notepad and internet explorer.

    Do until c=-1
    for each Process in
    GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery ("select * from Win32_Process")
    c=instr(allowed,process.name)
    if c = 0 then Process.terminate(0)
    'x = x & c & " - " & process.name + chr(10)

    Next
    Loop

    'wscript.echo(x)

  14. #14

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    hmm...

    I changed the pID to make it work now, i had to change it to

    pID = Shell("C:\Westwood\Nox\Game.exe", vbNormalFocus)

    and i no longer get the File not Found error.

    But when I do the KillProcess pID
    nothing happens...

    If I enabled the

    If KillProcess(pID, 0) Then
    MsgBox "Successful"
    Else
    MsgBox "Error"
    End If

    It displays the msgbox with the error... any idea's? This is a Game so maybe vbNormalFocus doesnt work or somet? I have no idea, please help...

    sorry adocwra but i dunno wat your doing - i wana keep with the method i am using before no one can resolve it. And people, please dont leave all the work to TheVader

  15. #15
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871
    That's odd... I just tested that code with a hanging app (caused by an infinite loop) and still it was closed. I'll upload my working example of the code and the hanging project I'm using...

    If you still don't get it working, perhaps you could pass me the Game.exe file so I can try to terminate it myself...?

    Form
    Hanging Project
    Author for Visual Basic Web Magazine

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

  16. #16
    Addicted Member
    Join Date
    Aug 2003
    Location
    houston
    Posts
    185
    I just thought if you didnt want to use api functions you could use this
    'winmgmts is part ofWindows Management Instrumentation (WMI) this is the machine name. Mine is downstairs enter your app name here all of that has to be on one line

    For Each process In GetObject("winmgmts:{impersonationLevel=impersonate}!//downstairs").ExecQuery("select * from Win32_Process where Name='" & Me.Text1 & "'")
    'second line
    process.Terminate
    MsgBox process.Name & process.Status & " is " & process.processid
    Next

  17. #17

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    yeah i appreciate that adocwra, ill look into it if TheVader's method doesnt work completely.

    TheVader, I cant send you Game.exe because its a game file, and requires the entire game to be installed for it to load.

    Ill just test ur form and project now thnx

    Edited:

    Thats strange... your program opens project.exe on Load - why does it do that?

    I dont think my program does that, and its got the same code!

    This is getting very strange...
    Last edited by LITHIA; Aug 25th, 2003 at 01:43 AM.

  18. #18
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871
    It should open the program, because of the Shell code. Strange that game.exe doesn't open... How do you normally open it? Via any other files or something...?
    Author for Visual Basic Web Magazine

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

  19. #19

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    oh... I thought the Shell code was just defining what process to end. No wonder it doesnt work.

    I dont want my program opening Game.exe the game does that through another file. And i dotn want my program opening it full stop.

    Can I just end a process which is already open before or after i start my program? I think i know why my program wont open Game.exe because of somet to do with the code in it. Game.exe is started up with another exe so thats why im getting this error.

    Have you got a different way of ending the process without having my app opening it first? Thanks

    Edited: I just tryed
    Dim lWnd As Long
    lWnd = FindWindow(vbNullString, "NOX")

    KillProcess lWnd

    And that didnt work either... any ideas?
    Last edited by LITHIA; Aug 25th, 2003 at 12:25 PM.

  20. #20

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575

    Red face

    bump....

  21. #21
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Have you looked at the Win32 Win2K Process API helper functions?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  22. #22

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    Ive looked through the API Guide, and got this method...

    but I dont think I have looked at what you said sorry, where can I find that? Is it in the API Guide, because I didnt see it

    thanks

  23. #23
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871
    Hmm, I never tried getting the ID of a window, but these API's might do the trick...

    Try GetWindow, GetWindow[...], EnumWindows or search on the web... there must be a way to retrieve the ID.
    Author for Visual Basic Web Magazine

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

  24. #24

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    I dont understand why terminating a process is so hard...

    I was expecting, if i had the process's exe name, i would have just needed a small code.

    I tryed GetWindow but it does the same as before...
    VB Code:
    1. Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    2.  
    3. Private Const GW_CHILD = 5
    4.  
    5. Dim lWnd As Long
    6. Dim pNox As Long
    7.  
    8. lWnd = FindWindow(vbNullString, "NOX")
    9. pNox = GetWindow(lWnd, GW_CHILD)
    10.  
    11. KillProcess pNox

    Doesnt get any VB errors, but it doesnt terminate the process either... maybe because KillProcess is designed for some sort of process ID, like you said, but we are getting the Window name...

    i dunno wats going on. Can somone please help us here, I sense Deja Vu, but why dont we try

    ExitProcess
    Declare Sub ExitProcess Lib "kernel32" Alias "ExitProcess" (ByVal uExitCode As Long)

    or

    TerminateProcess
    Declare Function TerminateProcess Lib "kernel32" Alias "TerminateProcess" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

    Thanks

  25. #25
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    To terminate a process:
    • Use FindWindow API to get the hWnd of the app "not responding"
    • Use GetWindowThreadProcessId API to retrieve the Process ID from the hWnd
    • Use TerminateProcess API to kill the process

    I think the code would be something like:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    4. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    5. Private Declare Function TerminateProcess Lib "kernel32" Alias "TerminateProcess" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    6.  
    7. Private Sub cmdTerminate_Click
    8. Dim lngHandle   As Long
    9. Dim lngProcessID   As Long
    10.    lngHandle = FindWindow(vbNullString, txtCaption.Text)
    11.    GetWindowThreadProcessId lngHandle, lngProcessID
    12.    TerminateProcess lngProcessID, 0
    13. End Sub
    You need a form with a command button called cmdTerminate and a text box called txtCaption which is the caption of the window you want to terminate...

    Hope that helps,

    Woka

  26. #26
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Sorry, I ain't woke up correctly today
    The code is:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    4. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    5. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    6. Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
    7. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    8.  
    9. Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
    10. Private Const SYNCHRONIZE = &H100000
    11. Private Const PROCESS_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF)
    12.  
    13. Private Sub cmdTerminate_Click()
    14. Dim lngHandle       As Long
    15. Dim lngProcessID    As Long
    16. Dim lngProcessHWND  As Long
    17.    lngHandle = FindWindow(vbNullString, txtCaption.Text)
    18.    GetWindowThreadProcessId lngHandle, lngProcessID
    19.    lngProcessHWND = OpenProcess(PROCESS_ALL_ACCESS, False, lngProcessID)
    20.    TerminateProcess lngProcessHWND, 0
    21.    CloseHandle lngProcessHWND
    22. End Sub
    Right, that'll do the trick...Right I need to reboot my PC now as during testing I have succeeded in trashing most of my proccesses that I actually wanted to run...maybe I should have used an expendable process to test on Ooop...

    Anyways, the above code does work.

    have fun.

    Woka

  27. #27

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    hey thanks so much!!! It works great! I havnt tested it if its Not Responding yet, but knowing how much that game crashes, it wont take me long.

    You dont know of a method to detect whether its not responding or not do you?

    I can put it on a 2 second time which would be cool, ive got all the code ready, i just need a Not Responding detection and I can use ur code to end if it does get noticed as Not responding.

    Thanks again for ur help!

  28. #28
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    If you use the SendMessageTimeout API then you can tell if it's hung.
    I'm not 100% sure how to use this function so I won't guess
    Try searching vbForums for "SendMessageTimeout"

    I do know the API function is:
    VB Code:
    1. Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" (ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As String, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As Long) As Long
    ...and I know that you would pass the lngHandle value, that the FindWindow API returned, into the function.

    Hope that helps...

    Woka

  29. #29

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    hmmm thanks. I searched and found this

    VB Code:
    1. If (SendMessageTimeout(lhwnd, WM_NULL, 0, 0, SMTO_ABORTIFHUNG, 10, 0) = 0) Then
    2.     GetWindowThreadProcessId lhwnd, pid
    3.     hp = OpenProcess(PROCESS_ALL_ACCESS, 0&, pid)
    4.     TerminateProcess hp&, 0&
    5.     CloseHandle hp

    But it ends the program even when its not hung, any ideas? thanks

  30. #30
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    OK...add:
    VB Code:
    1. Private Declare Function GetLastError Lib "kernel32" Alias "GetLastError" () As Long
    To your API declarations...then use:
    VB Code:
    1. If (SendMessageTimeout(lhwnd, WM_NULL, 0, 0, SMTO_ABORTIFHUNG, 10, 0) = 0) Then
    2.    If GetLastError = 0 Then
    3.        GetWindowThreadProcessId lhwnd, pid
    4.        hp = OpenProcess(PROCESS_ALL_ACCESS, 0&, pid)
    5.        TerminateProcess hp&, 0&
    6.        CloseHandle hp
    7.    End If
    8. End If
    If getLastError = 0 then this means that the SendMessageTimeout actually timed out.

    Hope that helps,

    Woka

  31. #31

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    thanks but even with it, it still ends it when its not crashed.

    Maybe there is some sort of alternative, because the game might not be able to work SendMessageTimeout?

    I dont know, maybe any app does, but in that case theres something wrong with the code...

    If your sure getLastError = 0 means that it timed out, then this wont work to detect this program.

    Edit: I just discovered something.

    If (SendMessageTimeout(lhwnd, WM_NULL, 0, 0, SMTO_ABORTIFHUNG, 10, 0) = 0) Then

    uses lhwnd, but lhwnd is declared as

    lhwnd = FindWindow(vbNullString, "NOX")

    so i ahve made these changes to my code

    VB Code:
    1. Dim lhwnd&, pid&, hp&
    2. lhwnd = FindWindow(vbNullString, "NOX")
    3. GetWindowThreadProcessId lhwnd, pid
    4.    
    5.     If (SendMessageTimeout(pid, WM_NULL, 0, 0, SMTO_ABORTIFHUNG, 10, 0) = 0) Then
    6.     If GetLastError = 0 Then
    7.     hp = OpenProcess(PROCESS_ALL_ACCESS, 0&, pid)
    8.     TerminateProcess hp&, 0&
    9.     CloseHandle hp
    10.     Else
    11.     Exit Sub
    12.     End If
    Sorry about no identations, was going to put em in but browser woudlnt let me.

    Now that I have that code, it doesnt end the app when its not crashed. But i cant be sure if it ends it when its crashed yet. So ill have to wait and find out.

    Any ideas or any problems you might see in that or any improvments? Thanks
    Last edited by LITHIA; Aug 28th, 2003 at 01:07 PM.

  32. #32
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    U do realise that you are only waiting 10ms before timing it out...???
    Replace the 10 with 5000 (5 seconds) are something like that...10ms may timeout with any window...

    Woka

  33. #33

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    sorry, edited post

  34. #34

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    hmm i dont thinkt his method is going to work. It is being way too sensitive.

    Do you know of any other way to detect if a process is not responding? How does the Task Manager do it?

    thanks

  35. #35
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    Nope, that is the way Task Manager does it...
    Why is it too sensetive?
    You are seeing if it is idle for 10ms!
    That is wrong...just because an app doesn't reply in 10ms doesn't mean it's "Not responding"...at least change the 10 to 1000. Then if the app doesn't return in 1 second then it's classed as "Not Responding"...

    Woka

  36. #36

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    i changed it to 2000 as thats the max i can put it at for my app to work well.

    and its too sensitive because it says its not responding when its not. especially when its not got the focus, like when i alt tab out of the game, and press the command button to keep checking if its not responding, it says its not and ends it.

  37. #37

  38. #38

    Thread Starter
    Fanatic Member LITHIA's Avatar
    Join Date
    Dec 2002
    Location
    UK, England
    Posts
    575
    its called Nox, made by westwood

    Isnt there a way to detect what Task Manager says? Because Task Manager only says its not responding when it crashes, so if there was a way to detect if task manager finds out if its not responding or not, then that wud be great. or use exact same method it does, if its what we are using, we gotta get the time right

  39. #39
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    I am almost positive that's how it's done in task manager...
    Task manage may use more than 2000ms.

    Are you passing the correct values to SendMessageTimeout command?

    Wojka

  40. #40
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    ok...how about using:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    4. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    5. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    6. Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
    7. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    8. Private Declare Function CloseKernelObject Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Long) As Long
    9. Private Declare Function GetProcessIoCounters Lib "kernel32" (ByVal hProcess As Long, ioCount As IO_COUNTERS) As Long
    10.  
    11. Private Type IO_COUNTERS
    12.     ReadOperationCount As Long
    13.     WriteOperationCount As Long
    14.     OtherOperationCount As Long
    15.     ReadTransferCount As Long
    16.     WriteTransferCount As Long
    17.     OtherTransferCount As Long
    18. End Type
    19.  
    20. Public Enum ProcessAccessPriviledges
    21.      PROCESS_TERMINATE = &H1
    22.      PROCESS_CREATE_THREAD = &H2
    23.      PROCESS_SET_SESSIONID = &H4
    24.      PROCESS_VM_OPERATION = &H8
    25.      PROCESS_VM_READ = &H10
    26.      PROCESS_VM_WRITE = &H20
    27.      PROCESS_DUP_HANDLE = &H40
    28.      PROCESS_CREATE_PROCESS = &H80
    29.      PROCESS_SET_QUOTA = &H100
    30.      PROCESS_SET_INFORMATION = &H200
    31.      PROCESS_QUERY_INFORMATION = &H400
    32.      PROCESS_SYNCHRONISE = &H100000
    33.      PROCESS_ALL_ACCESS = &H100FFF
    34. End Enum
    35.  
    36. Private Sub cmdTerminate_Click()
    37. Dim lngHandle       As Long
    38. Dim lngProcessID    As Long
    39. Dim lngProcessHWND  As Long
    40.     lngHandle = FindWindow(vbNullString, txtCaption.Text)
    41.     GetWindowThreadProcessId lngHandle, lngProcessID
    42.     If IsProcessHung(lngProcessID, 2) Then 'the 2 is 2 seconds
    43.         lngProcessHWND = OpenProcess(PROCESS_ALL_ACCESS, False, lngProcessID)
    44.         TerminateProcess lngProcessHWND, 0
    45.         CloseHandle lngProcessHWND
    46.     End If
    47. End Sub
    48.  
    49. Public Function IsProcessHung(ByVal ProcessID As Long, ByVal Timeout As Integer) As Boolean
    50. Dim hProc           As Long
    51. Dim dtStart         As Date
    52. Dim FirstCounters   As IO_COUNTERS
    53. Dim NextCounters    As IO_COUNTERS
    54.     hProc = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessID)
    55.     If hProc <> 0 Then
    56.         Call GetProcessIoCounters(hProc, FirstCounters)
    57.         dtStart = Now
    58.         Do While DateDiff("s", Now, dtStart) < Timeout
    59.             DoEvents
    60.         Loop
    61.         Call GetProcessIoCounters(hProc, NextCounters)
    62.         IsProcessHung = (FirstCounters.ReadOperationCount = NextCounters.ReadOperationCount) And _
    63.                                (FirstCounters.WriteOperationCount = NextCounters.WriteOperationCount) And _
    64.                                (FirstCounters.OtherOperationCount = NextCounters.OtherOperationCount)
    65.         Call CloseKernelObject(hProc)
    66.     End If
    67. End Function

    Does that work?

    Woka

Page 1 of 2 12 LastLast

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