Results 1 to 4 of 4

Thread: Close another program...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    ...Wherever God wants me...
    Posts
    125

    Close another program...

    Anyone know how to close another program from VB? For instance, My program uses a scanner, and when the scanner is finished i want the scanner driver program (runs minimized) to shutdown because everytime the scanner is used, another driver window pops up. This will become very memory consuming when these begin accumulating.
    to the power of X

  2. #2
    haravinth
    Guest
    I have a code for closing the form. I don't think it will help you.
    If it will help you i will post it when i get home.

    Now i am in schoo.

  3. #3
    Frenzied Member oh1mie's Avatar
    Join Date
    Sep 2001
    Location
    Finland
    Posts
    1,043
    Here is some tools for you

    Code:
    Option Explicit
    
    Public Const PROCESS_QUERY_INFORMATION = 1024
    Public Const PROCESS_VM_READ = 16
    Public Const MAX_PATH = 260
    Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
    Public Const SYNCHRONIZE = &H100000
    'STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF
    Public Const PROCESS_ALL_ACCESS = &H1F0FFF
    Public Const TH32CS_SNAPPROCESS = &H2&
    Public Const hNull = 0
    
    Public Enum ePlatform
      eWin95_98 = 1
      eWinNT = 2
    End Enum
    
    'Usage
    
    'Then call the routine like this:
    'If IsApplicationRunning("MyProgram.exe") Then
    '   MsgBox "Application is running."
    'Else
    '   MsgBox "Application is not running."
    'End If
    
    
    'Matthew GATES wrote :
    'Try this (code does not work for WinNT):
    'Call KillApp("C:\Program Files\Internet Explorer\iexplore.exe")
    
    Public Type PROCESSENTRY32
      dwSize              As Long
      cntUsage            As Long
      th32ProcessID       As Long ' This process
      th32DefaultHeapID   As Long
      th32ModuleID        As Long ' Associated exe
      cntThreads          As Long
      th32ParentProcessID As Long ' This process's parent process
      pcPriClassBase      As Long ' Base priority of process threads
      dwFlags             As Long
      szexeFile           As String * 260
    End Type
    
    Public Type OSVERSIONINFO
      dwOSVersionInfoSize As Long
      dwMajorVersion      As Long
      dwMinorVersion      As Long
      dwBuildNumber       As Long
      dwPlatformId        As Long '1 = Windows 95. '2 = Windows NT
      szCSDVersion        As String * 128
    End Type
    
    Private Declare Function ProcessFirst _
    Lib "kernel32" Alias "Process32First" (ByVal hSnapshot _
    As Long, uProcess As PROCESSENTRY32) As Long
    
    Private Declare Function ProcessNext Lib "kernel32" _
    Alias "Process32Next" (ByVal hSnapshot As Long, _
    uProcess As PROCESSENTRY32) As Long
    
    Private Declare Function CreateToolhelpSnapshot _
    Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal _
    lFlags As Long, lProcessID As Long) As Long
    
    Private Declare Function TerminateProcess _
    Lib "kernel32" (ByVal hProcess As Long, ByVal _
    uExitCode As Long) As Long
    
    Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
    
    Public Declare Function Process32First Lib "kernel32" ( _
    ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    
    Public Declare Function Process32Next Lib "kernel32" ( _
    ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    
    Public Declare Function CloseHandle Lib "Kernel32.dll" _
    (ByVal Handle As Long) As Long
    
    Public Declare Function OpenProcess Lib "Kernel32.dll" _
    (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _
    ByVal dwProcId As Long) As Long
    
    Public Declare Function EnumProcesses Lib "psapi.dll" _
    (ByRef lpidProcess As Long, ByVal cb As Long, _
    ByRef cbNeeded As Long) As Long
    
    Public Declare Function GetModuleFileNameExA Lib "psapi.dll" _
    (ByVal hProcess As Long, ByVal hModule As Long, _
    ByVal strModuleName As String, ByVal nSize As Long) As Long
    
    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
    
    Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" ( _
    ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
    
    Public Declare Function GetVersionExA Lib "kernel32" _
    (lpVersionInformation As OSVERSIONINFO) As Integer
    
    
    Public gDBType As String
    Public Function IsApplicationRunning(pEXEName As String) As Boolean
    
        On Error Resume Next
        
        Select Case getVersion()
        Case eWin95_98
        
            Dim lProc As Long
            Dim strName As String
            Dim hSnap As Long
            Dim proc As PROCESSENTRY32
            
            hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
            If hSnap = hNull Then Exit Function
            proc.dwSize = Len(proc)
            ' Iterate through the processes
            lProc = Process32First(hSnap, proc)
            
            Do While lProc
               strName = StrZToStr(proc.szexeFile)
               If InStr(UCase(strName), UCase(pEXEName)) Then
                  IsApplicationRunning = True
                  Exit Function
               End If
               lProc = Process32Next(hSnap, proc)
            Loop
        
        Case eWinNT
        
            Dim cb                 As Long
            Dim cbNeeded           As Long
            Dim NumElements        As Long
            Dim lProcessIDs()      As Long
            Dim cbNeeded2          As Long
            Dim lNumElements2      As Long
            Dim lModules(1 To 200) As Long
            Dim lRet               As Long
            Dim strModuleName      As String
            Dim nSize              As Long
            Dim hProcess           As Long
            Dim i                  As Long
            
            'Get the array containing the process id's for each process object
            cb = 8
            cbNeeded = 96
            
            Do While cb <= cbNeeded
               cb = cb * 2
               ReDim lProcessIDs(cb / 4) As Long
               lRet = EnumProcesses(lProcessIDs(1), cb, cbNeeded)
            Loop
            
            NumElements = cbNeeded / 4
            
            For i = 1 To NumElements
                
                'Get a handle to the Process
                hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
                Or PROCESS_VM_READ, 0, lProcessIDs(i))
                
                'Got a Process handle
                If hProcess <> 0 Then
                   'Get an array of the module handles for the specified process
                   lRet = EnumProcessModules(hProcess, lModules(1), 200, cbNeeded2)
                   'If the Module Array is retrieved, Get the ModuleFileName
                   If lRet <> 0 Then
                      strModuleName = Space(MAX_PATH)
                      nSize = 500
                      lRet = GetModuleFileNameExA(hProcess, lModules(1), strModuleName, nSize)
                      strModuleName = Left(strModuleName, lRet)
                      'Check for the client application running
                      If InStr(UCase(strModuleName), UCase(pEXEName)) Then
                          IsApplicationRunning = True
                          Exit Function
                      End If
                      'List1.AddItem Left(strModuleName, lRet)
                    End If
               End If
               'Close the handle to the process
               lRet = CloseHandle(hProcess)
               
            Next
        End Select
        
    End Function
    Function StrZToStr(pString As String) As String
        StrZToStr = Left$(pString, Len(pString) - 1)
    End Function
    Public Function getVersion() As ePlatform
    
        Dim osinfo  As OSVERSIONINFO
        Dim lRetVal As Integer
        
        osinfo.dwOSVersionInfoSize = 148
        osinfo.szCSDVersion = Space$(128)
        lRetVal = GetVersionExA(osinfo)
        getVersion = osinfo.dwPlatformId
        
    End Function
    Private 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
    oh1mie/Vic


  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    ...Wherever God wants me...
    Posts
    125
    Wow thanks!
    to the power of X

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