Results 1 to 6 of 6

Thread: VB6|Killing Processes

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Resolved VB6|Killing Processes

    Well before i start, i already searched with no luck, no code seems to work.
    i need an api that will kill a process when form unloads...i have tried numerous kinds so dont tell me to search, thanks in advance
    Last edited by manavo11; Jan 28th, 2005 at 05:55 PM. Reason: Added checkmark

  2. #2
    Frenzied Member ice_531's Avatar
    Join Date
    Aug 2002
    Location
    Sitting w/ Bob Status: -Next -To- Null- Friend: Philip
    Posts
    1,152

    Re: VB6|Killing Processes

    What process do you need to kill? Another application not of your own, when your program ends?
    :::`DISCLAIMER`:::
    Do NOT take anything i have posted to be truthful in any way, shape or form.
    Thank You!

    --------------------------------
    "Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
    "Finaly I can look as gay as I want..." - NoteMe
    Languages: VB6, BASIC, Java, C#. C++

  3. #3
    Frenzied Member ice_531's Avatar
    Join Date
    Aug 2002
    Location
    Sitting w/ Bob Status: -Next -To- Null- Friend: Philip
    Posts
    1,152

    Re: VB6|Killing Processes

    Good example on Killing a Program Process:
    http://www.andreavb.com/tip020021.html
    :::`DISCLAIMER`:::
    Do NOT take anything i have posted to be truthful in any way, shape or form.
    Thank You!

    --------------------------------
    "Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
    "Finaly I can look as gay as I want..." - NoteMe
    Languages: VB6, BASIC, Java, C#. C++

  4. #4

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: VB6|Killing Processes

    yeah i cant get that one to work, i tried it before.
    im not killing my project, but another..
    heres my module:
    VB Code:
    1. Type PROCESSENTRY32
    2.     dwSize As Long
    3.     cntUsage As Long
    4.     th32ProcessID As Long
    5.     th32DefaultHeapID As Long
    6.     th32ModuleID As Long
    7.     cntThreads As Long
    8.     th32ParentProcessID As Long
    9.     pcPriClassBase As Long
    10.     dwFlags As Long
    11.     szexeFile As String * 260
    12. End Type
    13. '-------------------------------------------------------
    14. Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, _
    15. ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
    16.  
    17. Declare Function ProcessFirst Lib "kernel32.dll" Alias "Process32First" (ByVal hSnapshot As Long, _
    18. uProcess As PROCESSENTRY32) As Long
    19.  
    20. Declare Function ProcessNext Lib "kernel32.dll" Alias "Process32Next" (ByVal hSnapshot As Long, _
    21. uProcess As PROCESSENTRY32) As Long
    22.  
    23. Declare Function CreateToolhelpSnapshot Lib "kernel32.dll" Alias "CreateToolhelp32Snapshot" ( _
    24. ByVal lFlags As Long, lProcessID As Long) As Long
    25.  
    26. Declare Function TerminateProcess Lib "kernel32.dll" (ByVal ApphProcess As Long, _
    27. ByVal uExitCode As Long) As Long
    28.  
    29. Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
    30.  
    31.  Public Sub KillProcess(NameProcess As String)
    32. Const PROCESS_ALL_ACCESS = 0
    33. Const TH32CS_SNAPPROCESS As Long = 2&
    34. Dim uProcess  As PROCESSENTRY32
    35. Dim RProcessFound As Long
    36. Dim hSnapshot As Long
    37. Dim SzExename As String
    38. Dim ExitCode As Long
    39. Dim MyProcess As Long
    40. Dim AppKill As Boolean
    41. Dim AppCount As Integer
    42. Dim i As Integer
    43. Dim WinDirEnv As String
    44.        
    45.        If NameProcess <> "" Then
    46.           AppCount = 0
    47.  
    48.           uProcess.dwSize = Len(uProcess)
    49.           hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    50.           RProcessFound = ProcessFirst(hSnapshot, uProcess)
    51.  
    52.           Do
    53.             i = InStr(1, uProcess.szexeFile, Chr(0))
    54.             SzExename = LCase$(Left$(uProcess.szexeFile, i - 1))
    55.             WinDirEnv = Environ("Windir") + "\"
    56.             WinDirEnv = LCase$(WinDirEnv)
    57.        
    58.             If Right$(SzExename, Len(NameProcess)) = LCase$(NameProcess) Then
    59.                AppCount = AppCount + 1
    60.                MyProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
    61.                AppKill = TerminateProcess(MyProcess, ExitCode)
    62.                Call CloseHandle(MyProcess)
    63.             End If
    64.             RProcessFound = ProcessNext(hSnapshot, uProcess)
    65.           Loop While RProcessFound
    66.           Call CloseHandle(hSnapshot)
    67.        End If
    68.  
    69. End Sub

    and in my form:

    VB Code:
    1. Private Sub Form_Load()
    2. KillProcess (App.Path & "\text1.text")
    3. End Sub
    nothing happends at all : /

  5. #5

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: VB6|Killing Processes

    got 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")

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

    Resolved Re: VB6|Killing Processes

    solved

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