Results 1 to 8 of 8

Thread: help terminateEXE(RESOLVED)

  1. #1

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Resolved help terminateEXE(RESOLVED)

    this code is working but can terminate only one EXE but not all listed EXE
    in the command1_click event. If any of you
    can highlight this trouble code is greatly appreciated. Thanks so much
    in advance for all response
    Kamus

    in module1:
    ========
    VB Code:
    1. Public 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. Public Type OSVERSIONINFO
    15.   dwOSVersionInfoSize As Long
    16.   dwMajorVersion As Long
    17.   dwMinorVersion As Long
    18.   dwBuildNumber As Long
    19.   dwPlatformId As Long
    20.   szCSDVersion As String * 128
    21. End Type
    22.  
    23. Public Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    24. Public Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    25. Public Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
    26. Public Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
    27. Public Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
    28. Public Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
    29. Public Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
    30. Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
    31. Public Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
    32. Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    33.  
    34. Public Const PROCESS_TERMINATE = &H1
    35. Public Const VER_PLATFORM_WIN32_WINDOWS = 1
    36. Public Const PROCESS_QUERY_INFORMATION = 1024
    37. Public Const PROCESS_VM_READ = 16
    38. Public Const TH32CS_SNAPPROCESS = &H2
    39.  
    40. Public Function CheckVersion() As Long
    41.   Dim tOS As OSVERSIONINFO
    42.   tOS.dwOSVersionInfoSize = Len(tOS)
    43.   Call GetVersionEx(tOS)
    44.   CheckVersion = tOS.dwPlatformId
    45. End Function
    46.  
    47. Public Function GetEXEProcessID(ByVal sEXE As String) As Long
    48.   Dim aPID() As Long
    49.   Dim lProcesses As Long
    50.   Dim lProcess As Long
    51.   Dim lModule As Long
    52.   Dim sName As String
    53.   Dim iIndex As Integer
    54.   Dim bCopied As Long
    55.   Dim lSnapShot As Long
    56.   Dim tPE As PROCESSENTRY32
    57.   Dim bDone As Boolean
    58.  
    59.   If CheckVersion() = VER_PLATFORM_WIN32_WINDOWS Then
    60.     'Windows 9x
    61.     'Create a SnapShot of the Currently Running Processes
    62.     lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    63.     If lSnapShot < 0 Then Exit Function
    64.     tPE.dwSize = Len(tPE)
    65.     'Buffer the First Processes Info..
    66.     bCopied = Process32First(lSnapShot, tPE)
    67.     Do While bCopied
    68.       'While there are Processes List them..
    69.       sName = Left$(tPE.szExeFile, InStr(tPE.szExeFile, Chr(0)) - 1)
    70.       sName = Mid(sName, InStrRev(sName, "\") + 1)
    71.       If InStr(sName, Chr(0)) Then
    72.         sName = Left(sName, InStr(sName, Chr(0)) - 1)
    73.       End If
    74.       bCopied = Process32Next(lSnapShot, tPE)
    75.       If StrComp(sEXE, sName, vbTextCompare) = 0 Then
    76.         GetEXEProcessID = tPE.th32ProcessID
    77.         Exit Do
    78.       End If
    79.     Loop
    80.    
    81.   Else
    82.     'Windows NT
    83.     'The EnumProcesses Function doesn't indicate how many Process there are,
    84.     'so you need to pass a large array and trim off the empty elements
    85.     'as cbNeeded will return the no. of Processes copied.
    86.     ReDim aPID(255)
    87.     Call EnumProcesses(aPID(0), 1024, lProcesses)
    88.     lProcesses = lProcesses / 4
    89.     ReDim Preserve aPID(lProcesses)
    90.    
    91.     For iIndex = 0 To lProcesses - 1
    92.       'Get the Process Handle, by Opening the Process
    93.       lProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, aPID(iIndex))
    94.       If lProcess Then
    95.         'Just get the First Module, all we need is the Handle to get
    96.         'the Filename..
    97.         If EnumProcessModules(lProcess, lModule, 4, 0&) Then
    98.           sName = Space(260)
    99.           Call GetModuleFileNameExA(lProcess, lModule, sName, Len(sName))
    100.           If InStr(sName, "\") > 0 Then
    101.             sName = Mid(sName, InStrRev(sName, "\") + 1)
    102.           End If
    103.           If InStr(sName, Chr(0)) Then
    104.             sName = Left(sName, InStr(sName, Chr(0)) - 1)
    105.           End If
    106.           If StrComp(sEXE, sName, vbTextCompare) = 0 Then
    107.             GetEXEProcessID = aPID(iIndex)
    108.             bDone = True
    109.           End If
    110.         End If
    111.         'Close the Process Handle
    112.         CloseHandle lProcess
    113.         If bDone Then Exit For
    114.       End If
    115.     Next
    116.   End If
    117. End Function

    form code :
    =========
    VB Code:
    1. Public Function TerminateEXE(ByVal sEXE As String) As Boolean
    2.   Dim lPID As Long
    3.   Dim lProcess As Long
    4.  
    5.   Do
    6.     lPID = GetEXEProcessID(sEXE)
    7.     If lPID <> 0 Then
    8.         lProcess = OpenProcess(PROCESS_TERMINATE, 0, lPID)
    9.         Call TerminateProcess(lProcess, 0&)
    10.         Call CloseHandle(lProcess)
    11.     End If
    12.   Loop Until lPID = 0
    13.   TerminateEXE = True
    14. End Function
    15.  
    16. Private Sub Command1_Click()
    17.  
    18. blnRet = TerminateEXE("Ragnarok.exe") And _
    19.  blnRet = TerminateEXE("run.exe") And _
    20.  blnRet = TerminateEXE("qRO.exe") And _
    21.  blnRet = TerminateEXE("IEXPLORE.EXE") And _
    22.  blnRet = TerminateEXE("amped.exe") And _
    23. blnRet = TerminateEXE("YPager.exe") And _
    24.  blnRet = TerminateEXE("Login.exe") And _
    25.  blnRet = TerminateEXE("main.exe") And _
    26. blnRet = TerminateEXE("GunBound.exe")
    27.                    
    28. End Sub
    Last edited by ksuwanto8ksd; Jun 3rd, 2005 at 12:39 PM.

  2. #2

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: help terminateEXE

    and I also try with this one result is same
    VB Code:
    1. Private Sub Command1_Click()
    2. blnRet = TerminateEXE("Ragnarok.exe")
    3. blnRet = TerminateEXE("run.exe")
    4. blnRet = TerminateEXE("qRO.exe")
    5.  blnRet = TerminateEXE("IEXPLORE.EXE")
    6. blnRet = TerminateEXE("amped.exe")
    7. blnRet = TerminateEXE("YPager.exe")
    8. blnRet = TerminateEXE("Login.exe")
    9. blnRet = TerminateEXE("main.exe")
    10. blnRet = TerminateEXE("GunBound.exe")
    11. End Sub

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: help terminateEXE

    Try putting a DoEvents between each call.

  4. #4

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: help terminateEXE

    Hi Hack

    I'm not familiar with doevent , thats ok I will try to serch the forum first

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: help terminateEXE

    Basically, DoEvents "pauses" your program.
    Yields execution so that the operating system can process other events.

    Syntax

    DoEvents( )

    Remarks

    The DoEvents function returns anInteger representing the number of open forms in stand-alone versions of Visual Basic, such as Visual Basic, Professional Edition. DoEvents returns zero in all other applications.

    DoEvents passes control to the operating system. Control is returned after the operating system has finished processing the events in its queue and all keys in the SendKeys queue have been sent.

    DoEvents is most useful for simple things like allowing a user to cancel a process after it has started, for example a search for a file. For long-running processes, yielding the processor is better accomplished by using a Timer or delegating the task to an ActiveX EXE component.. In the latter case, the task can continue completely independent of your application, and the operating system takes case of multitasking and time slicing.

  6. #6

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: help terminateEXE

    hi Hack

    I put DoEvent like this and result is same (only qRO.exe is terminated)

    DoEvents
    blnRet = TerminateEXE("Ragnarok.exe")
    DoEvents
    blnRet = TerminateEXE("run.exe")
    DoEvents
    blnRet = TerminateEXE("qRO.exe")
    DoEvents
    blnRet = TerminateEXE("IEXPLORE.EXE")
    DoEvents
    blnRet = TerminateEXE("amped.exe")
    DoEvents
    blnRet = TerminateEXE("YPager.exe")
    DoEvents
    blnRet = TerminateEXE("Login.exe")
    DoEvents
    blnRet = TerminateEXE("main.exe")
    DoEvents
    blnRet = TerminateEXE("GunBound.exe")

  7. #7

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: help terminateEXE

    this code also same
    [vbode]

    DoEvents
    blnRet = TerminateEXE("Ragnarok.exe")
    blnRet = TerminateEXE("run.exe")
    blnRet = TerminateEXE("qRO.exe")
    blnRet = TerminateEXE("IEXPLORE.EXE")
    blnRet = TerminateEXE("amped.exe")
    blnRet = TerminateEXE("YPager.exe")
    blnRet = TerminateEXE("Login.exe")
    blnRet = TerminateEXE("main.exe")
    blnRet = TerminateEXE("GunBound.exe")

    anyway, your response is greatly appreciated

  8. #8

    Thread Starter
    Fanatic Member ksuwanto8ksd's Avatar
    Join Date
    Apr 2005
    Posts
    636

    Re: help terminateEXE(RESOLVED)

    Thanks Hack
    It working. I have put EXE name wrong

    Appreciate

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