Results 1 to 12 of 12

Thread: Closing an open program

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Location
    Calgary Alberta
    Posts
    17

    Closing an open program

    I'm making a program that shuts off if the computer is idle for one minute. I have an idea for this. Have a program that turns on when the computer is idle and shuts down the program I need to shut off. When the idle run program is shut off, it reloads the program.

    I need to know how to shut down a program in operation.
    I know how to start a program.

    OR

    I need to know how to detect if the computer is idle from VB

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Closing an open program

    Quote Originally Posted by Turiya
    ... I need to know how to detect if the computer is idle from VB
    You'd have to create a system wide hook.
    For some nicely done sample visit www.vbaccelerator.com and browse that site.

    Good luck.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Location
    Calgary Alberta
    Posts
    17

    Re: Closing an open program

    I'm having trouble finding the page I need. So if anyone is reading this thread please post an answer.

  4. #4
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Closing an open program

    if you dont wanna do that, just find the mouse x,y, and use a timer to see how long its been idle, then shutdown

  5. #5

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Location
    Calgary Alberta
    Posts
    17

    Re: Closing an open program

    It needs to be checking system idleness, not program idleness

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Location
    Calgary Alberta
    Posts
    17

    Re: Closing an open program

    I found the page...and I only half get it. Can someone explain it to me

    http://www.vbaccelerator.com/home/VB...ks/article.asp

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Closing an open program

    Turiya,
    here is what I think and I apologize if you take it offencively: if your VB6 level of expertise is somewhat of a beginner then I'd recommend you to stay away from subclassing and hooking for awhile until you get a good understanding how it works (and not just understanding someone else's code - there is much more involved than that). Sorry.

    My best regards.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Location
    Calgary Alberta
    Posts
    17

    Re: Closing an open program

    Then how else would I do it?
    Remember what I need to know.
    How to kill a program running if I know its path and caption and everything it does.

    OR

    How to detect idle time.

  10. #10
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Closing an open program

    I don't think you can kill a process you didn't create.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Location
    Calgary Alberta
    Posts
    17

    Re: Closing an open program

    So there is NO WAY to close an open program?

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Kill A Program

    This code will kill an .exe, but it is not that easy to understand. The bottom line is how to call it.

    VB Code:
    1. Option Explicit
    2. Const MAX_PATH& = 260
    3.  
    4. Declare Function TerminateProcess _
    5. Lib "kernel32" (ByVal ApphProcess As Long, _
    6. ByVal uExitCode As Long) As Long
    7. Declare Function OpenProcess Lib _
    8. "kernel32" (ByVal dwDesiredAccess As Long, _
    9. ByVal blnheritHandle As Long, _
    10. ByVal dwAppProcessId As Long) As Long
    11. Declare Function ProcessFirst _
    12. Lib "kernel32" Alias "Process32First" _
    13. (ByVal hSnapshot As Long, _
    14. uProcess As PROCESSENTRY32) As Long
    15. Declare Function ProcessNext _
    16. Lib "kernel32" Alias "Process32Next" _
    17. (ByVal hSnapshot As Long, _
    18. uProcess As PROCESSENTRY32) As Long
    19. Declare Function CreateToolhelpSnapshot _
    20. Lib "kernel32" Alias "CreateToolhelp32Snapshot" _
    21. (ByVal lFlags As Long, _
    22. lProcessID As Long) As Long
    23. Declare Function CloseHandle _
    24. Lib "kernel32" (ByVal hObject As Long) As Long
    25.  
    26. Private Type LUID
    27. lowpart As Long
    28. highpart As Long
    29. End Type
    30.  
    31. Private Type TOKEN_PRIVILEGES
    32. PrivilegeCount As Long
    33. LuidUDT As LUID
    34. Attributes As Long
    35. End Type
    36.  
    37. Const TOKEN_ADJUST_PRIVILEGES = &H20
    38. Const TOKEN_QUERY = &H8
    39. Const SE_PRIVILEGE_ENABLED = &H2
    40. Const PROCESS_ALL_ACCESS = &H1F0FFF
    41.  
    42. Private Declare Function GetVersion _
    43. Lib "kernel32" () As Long
    44. Private Declare Function GetCurrentProcess _
    45. Lib "kernel32" () As Long
    46. Private Declare Function OpenProcessToken _
    47. Lib "advapi32" (ByVal ProcessHandle As Long, _
    48. ByVal DesiredAccess As Long, _
    49. TokenHandle As Long) As Long
    50. Private Declare Function LookupPrivilegeValue _
    51. Lib "advapi32" Alias "LookupPrivilegeValueA" _
    52. (ByVal lpSystemName As String, _
    53. ByVal lpName As String, _
    54. lpLuid As LUID) As Long
    55. Private Declare Function AdjustTokenPrivileges _
    56. Lib "advapi32" (ByVal TokenHandle As Long, _
    57. ByVal DisableAllPrivileges As Long, _
    58. NewState As TOKEN_PRIVILEGES, _
    59. ByVal BufferLength As Long, _
    60. PreviousState As Any, _
    61. ReturnLength As Any) As Long
    62.  
    63. Type PROCESSENTRY32
    64. dwSize As Long
    65. cntUsage As Long
    66. th32ProcessID As Long
    67. th32DefaultHeapID As Long
    68. th32ModuleID As Long
    69. cntThreads As Long
    70. th32ParentProcessID As Long
    71. pcPriClassBase As Long
    72. dwFlags As Long
    73. szexeFile As String * MAX_PATH
    74. End Type
    75. '---------------------------------------
    76. Public Function KillApp(myName As String) As Boolean
    77. Const TH32CS_SNAPPROCESS As Long = 2&
    78. Const PROCESS_ALL_ACCESS = 0
    79. Dim uProcess As PROCESSENTRY32
    80. Dim rProcessFound As Long
    81. Dim hSnapshot As Long
    82. Dim szExename As String
    83. Dim exitCode As Long
    84. Dim myProcess As Long
    85. Dim AppKill As Boolean
    86. Dim appCount As Integer
    87. Dim I As Integer
    88. On Local Error GoTo Finish
    89. appCount = 0
    90.  
    91. uProcess.dwSize = Len(uProcess)
    92. hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    93. rProcessFound = ProcessFirst(hSnapshot, uProcess)
    94. Do While rProcessFound
    95. I = InStr(1, uProcess.szexeFile, Chr(0))
    96. szExename = LCase$(Left$(uProcess.szexeFile, I - 1))
    97. If Right$(szExename, Len(myName)) = LCase$(myName) Then
    98. KillApp = True
    99. appCount = appCount + 1
    100. myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
    101. If KillProcess(uProcess.th32ProcessID, 0) Then
    102. 'For debug.... Remove this
    103. End If
    104.  
    105. End If
    106. rProcessFound = ProcessNext(hSnapshot, uProcess)
    107. Loop
    108. Call CloseHandle(hSnapshot)
    109. Exit Function
    110. Finish:
    111. MsgBox "Error!"
    112. End Function
    113.  
    114. 'Terminate any application and return an exit code to Windows.
    115. Function KillProcess(ByVal hProcessID As Long, Optional ByVal exitCode As Long) As Boolean
    116. Dim hToken As Long
    117. Dim hProcess As Long
    118. Dim tp As TOKEN_PRIVILEGES
    119.  
    120.  
    121. If GetVersion() >= 0 Then
    122.  
    123. If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) = 0 Then
    124. GoTo CleanUp
    125. End If
    126.  
    127. If LookupPrivilegeValue("", "SeDebugPrivilege", tp.LuidUDT) = 0 Then
    128. GoTo CleanUp
    129. End If
    130.  
    131. tp.PrivilegeCount = 1
    132. tp.Attributes = SE_PRIVILEGE_ENABLED
    133.  
    134. If AdjustTokenPrivileges(hToken, False, tp, 0, ByVal 0&, ByVal 0&) = 0 Then
    135. GoTo CleanUp
    136. End If
    137. End If
    138.  
    139. hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)
    140. If hProcess Then
    141.  
    142. KillProcess = (TerminateProcess(hProcess, exitCode) <> 0)
    143. ' close the process handle
    144. CloseHandle hProcess
    145. End If
    146.  
    147. If GetVersion() >= 0 Then
    148. ' under NT restore original privileges
    149. tp.Attributes = 0
    150. AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0&
    151.  
    152. CleanUp:
    153. If hToken Then CloseHandle hToken
    154. End If
    155.  
    156. End Function


    VB Code:
    1. KillApp ("barwr.exe")

    You really should search for answers. If you work for them, they are worth more in the long run.
    Last edited by dglienna; Apr 27th, 2005 at 07:38 PM.

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