Results 1 to 35 of 35

Thread: Closing Applications

  1. #1

    Thread Starter
    Addicted Member VB6Coder's Avatar
    Join Date
    Apr 2001
    Location
    Northampton, UK
    Posts
    185

    Smile Closing Applications

    I need to write a VB application which will close down another application. The application it needs to close doesn't have a constant window title, but I do know the EXE name.

    Is there any way I can close an application down if I only know the EXE name.


    Thanks in advance.

  2. #2
    Member VbAndersonic's Avatar
    Join Date
    Aug 2001
    Location
    Derby, UK
    Posts
    58
    How is the application you want to close executed? Does your program use the Shell command to open it?

  3. #3
    Registered User
    Join Date
    May 2001
    Location
    taizhou zheziang china
    Posts
    17
    This is third time i reply such Q.
    :-)

    you can use
    CreateToolhelp32Snapshot to create a Snapshot,pass it to Process32First to get the first process ID in OS,use Process32Next functions to get the remaining .
    and use OpenProcess to get process handle,
    use GetExitCodeProcess,ExitProcess to exit a process.

  4. #4
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    if you have Spy++ (it comes with VB me thinks or VC++), open that and get the windows class name.

    VB Code:
    1. 'in module
    2. Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal _
    3.   lpClassName As String, ByVal lpWindowName As String) As Long
    4. Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _
    5.   hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    6.  
    7. Public Const WM_CLOSE = &H10
    8.  
    9.  
    10. 'in command button or whereever
    11.  
    12. w_handle = FindWindow("classname", vbNullString)
    13. SendMessage(w_handle, WM_CLOSE, CLng(0), CLng(0)

    thats the best way to do it. if you dont have Spy++ but if you know what the possible captions are then do:

    VB Code:
    1. captions[1]="cap1"
    2. captions[2]="cap2"
    3. for i = 1 to ubound(captions)
    4. w_handle = FindWindow(vbNullString,captions[i])
    5. SendMessage(w_handle, WM_CLOSE, CLng(0), CLng(0)
    6. next i

    captions have to be *exactly* the same as they show on the window
    if it doesnt find the window it wont give an error btw

    hope that helps,
    nabeel
    Last edited by nabeels786; Aug 14th, 2001 at 01:47 PM.
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  5. #5
    Lively Member
    Join Date
    Jul 2000
    Posts
    94

    Angry Still dosn't work!

    I tried to use WM_CLOSE with PostMessage but I'm afraid the application I'm trying to close still stays resident in memory. I know this because I can still see it when I press Ctrl-Alt-Del.

    How can I get rid of the App altogether?

  6. #6
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    Try this.

    Code:
    Option Explicit
    
    Public Declare Function ProcessFirst Lib "kernel32" _
    Alias "Process32First" (ByVal hSnapshot As Long, uProcess As _
    PROCESSENTRY32) As Long
    
    Public Declare Function ProcessNext Lib "kernel32" _
    Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As _
    PROCESSENTRY32) As Long
    
    Public Declare Function CreateToolhelpSnapshot Lib "kernel32" _
    Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, _
    lProcessID As Long) As Long
    
    Public Declare Function CloseHandle Lib "kernel32" (ByVal _
    hObject As Long) As Long
    
    Public Declare Function OpenProcess Lib "kernel32" (ByVal _
    dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) As Long
    
    Public Declare Function TerminateProcess Lib "kernel32" (ByVal _
    hProcess As Long, ByVal uExitCode As Long) As Long
    
    Public Const MAX_PATH = 260
    
    Public Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long
        th32DefaultHeapID As Long
        th32ModuleID As Long
        cntThreads As Long
        th32ParentProcessID As Long
        pcPriClassBase As Long
        dwFlags As Long
        szexeFile As String * MAX_PATH
    End Type
    
    Public Function KillApp(myName As String) As Boolean
        On Local Error Resume Next
        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
    Usage:
    Code:
    KillApp("C:\myApp.exe")
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  7. #7
    Lively Member
    Join Date
    Jul 2000
    Posts
    94

    Wink Thans but no thanx!

    Thanks for that but it was uneccessary as I worked out my own way to do it:

    'Closing calculator
    lHandle = FindWindow(vbNullString, "Calculator")
    CloseWindow (lHandle)
    PostMessage lHandle, WM_CLOSE, 0&, 0&
    PostMessage lHandle, WM_QUIT, 0&, 0&

    This removes the app from the task list after closing it.

  8. #8
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    Fair enough...

    The difference is...

    my code will close an entire process
    but your will only close a windowed application.

    What if the window isn't visible??
    Or if the Application Doesn't use Windows?
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  9. #9
    Junior Member
    Join Date
    Aug 2000
    Location
    USA, Virginia
    Posts
    25
    hey, i'm looking for a similar thing. will gulliver's method work if i know the hwnd of the program opened? i don't want any non-visable windows not to be closed.

  10. #10
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    I'll try and explain it

    When an application starts, it registers the process with Windows. While my code is actually closing the whole process, similar to the way that windows does it, when you press Alt + Ctrl + Delete and then click End Task.

    The other guys code is just the same as pressing the x button in the corner of the window. It may seem the same at first, but be on the safe side, and close the whole process along with anything else that goes with it in the way of windows or whatever.

    Make Sense???
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  11. #11
    Junior Member
    Join Date
    Aug 2000
    Location
    USA, Virginia
    Posts
    25
    i understand what you mean. the problem for me is i dont' know the name of the exe file to terminate. i used shell command to open a file associated to the exe. so i have the hwnd because it's one of it's parameters.

  12. #12
    Junior Member
    Join Date
    Aug 2000
    Location
    USA, Virginia
    Posts
    25
    actually, the hwnd isn't for the new exe... is it? =\

  13. #13
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    You said that you used the shell command

    What are you shelling if you are using the shell command??? Is it not like ShellExecute() where you have to specify the executables path also?? Just pass that. Also... take a look at the looping sequence, it will tell you an abbreviated class name for each of the running files there.
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  14. #14
    Junior Member
    Join Date
    Aug 2000
    Location
    USA, Virginia
    Posts
    25
    whoops, i'm sorry, yes, i meant ShellExecute(). i specified the path of the file to be opened. in my case, it was an mp3 file so the program associated with it opened up. i am looking to close the program associated with it after a certain amount of time. will passing the file i opened work? no right? i would have to specify the exe that was associated with the file. u were sayin check the loop for abbreviated class names. where do i look for that?
    thanks for the help

  15. #15
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    ok...

    I see two options...

    Find in the registry what programs will open for that file extension (.mp3) or You can use the class that is shown when u press alt + ctrl + del to do it, but to do this, you need to know the program that will be used to open the mp3 file anyway...

    If u know which one u want, I might be able to get you some code.
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  16. #16
    Junior Member
    Join Date
    Aug 2000
    Location
    USA, Virginia
    Posts
    25
    hmm. the thing is, i would like for it to work for any program that is associated with the mp3 so that it would work on other computers. Is there a way to follow/analyze the processes so that when ShellExecute() command is issued, we can capture the filename of the exe when it begins to run?

    i think it's better to look into that method, if possible, because i would like for my program to be able to open a .wav .mpg .avi file or such and terminate it's associated program after a certain time.

  17. #17
    Junior Member
    Join Date
    Aug 2000
    Location
    USA, Virginia
    Posts
    25
    oh well, i suppose i will just have the user browse and specify the app to close at the first time using my prog.

    thanks for the help

  18. #18

    Thread Starter
    Addicted Member VB6Coder's Avatar
    Join Date
    Apr 2001
    Location
    Northampton, UK
    Posts
    185
    Hi Wak

    I've just tried to run your KillApp procedure on Windows 95 and it works fine, but it doesn't work when I run it on Windows 2000.

    It looks like it is because the OpenProcess calls returns 0.



    How can I get this to run on Windows 2000?

  19. #19
    Member
    Join Date
    Jun 2000
    Location
    France
    Posts
    51

    Talking I wait this for many many time !

    The KillApp function doesn't work with Windows NT family (NT 4, 2000, XP)

    Matthew GATES wrote it :
    [email protected]

    How can we get this to run on Windows NT 4, 2000, XP ?


    I have VB 5 Pro


    Thank you in advance for your help !
    Last edited by sebmaurice; Oct 4th, 2001 at 07:41 AM.

  20. #20
    Junior Member
    Join Date
    Aug 2000
    Location
    USA, Virginia
    Posts
    25
    i am on 2000 too.

  21. #21
    Junior Member
    Join Date
    Aug 2000
    Location
    USA, Virginia
    Posts
    25
    also, wak, i think i like the option of looking into the registry to find exes associated to the mp3(or whatever file type). where can i get code to do that?

  22. #22
    Member
    Join Date
    Jun 2000
    Location
    France
    Posts
    51

    Arrow Answer from Matthew GATES

    Matthew GATES mailed me :

    I've noticed it doesn't work with NT or 2000. Coding is different, I, nor I
    don't think anyone else has found a way to kill an app on WinNT or Win2k
    without the regular PostMessage/SendMessage API functions.

  23. #23
    Fanatic Member pradeepkrao's Avatar
    Join Date
    Sep 2001
    Location
    New Jersey
    Posts
    534
    You can use CreateProcess() to create a new window application or a simple application ..

    To terminate it use PostThreadMessage() if it is a window application (the message to be posted is WM_QUIT )

    and if it is a simple application without a window.. use TerminateProcess()
    Learn by others experience as you cannot live long to experience them all.
    www.freewebs.com/pradeepkrao

    LOOK AT MY GAMES AT MY WEB SITE.

  24. #24
    Member
    Join Date
    Jun 2000
    Location
    France
    Posts
    51

    Smile Source code ?

    I think it is interesting !

    Can you give me a sample source code, please ?

    Thank you.

  25. #25
    Fanatic Member pradeepkrao's Avatar
    Join Date
    Sep 2001
    Location
    New Jersey
    Posts
    534

    here goes the code

    'copy the following code in your bas file
    Public Type SECURITY_ATTRIBUTES_TYPE
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
    End Type

    Public Type STARTUPINFO_TYPE
    cb As Long
    lpReserved As Long
    lpDesktop As Long
    lpTitle As Long
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
    End Type

    Public Type PROCESS_INFORMATION_TYPE
    hProcess As Long
    hThread As Long
    dwProcessID As Long
    dwThreadID As Long
    End Type

    Public Declare Function CreateProcessA Lib "kernel32" (ByVal _
    lpApplicationName As Long, ByVal lpCommandLine As String, _
    lpProcessAttributes As Any, lpThreadAttributes As Any, _
    ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
    ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
    lpStartupInfo As Any, lpProcessInformation As Any) As Long

    Public Declare Function PostThreadMessage Lib "user32" Alias "PostThreadMessageA" (ByVal _
    idThread As Long, _
    ByVal msg As Long, _
    ByVal wParam As Long, _
    ByVal lParam As Long) As Long


    Global Const NORMAL_PRIORITY_CLASS = &H20&
    Global Const STARTF_USESTDHANDLES = &H100&
    Global Const WM_QUIT = &H12

    Public udtProc As PROCESS_INFORMATION_TYPE

    Public Function Terminate()
    Dim ret As Long
    ret = PostThreadMessage(udtProc.dwThreadID, WM_QUIT, 0, 0)
    End Function


    ' call this function to start your application

    Public Function CreateProcess(prcs)
    Dim lngret As Long
    Dim udtStart As STARTUPINFO_TYPE
    Dim udtSa As SECURITY_ATTRIBUTES_TYPE

    udtSa.nLength = Len(udtSa)
    udtSa.bInheritHandle = 1&
    udtSa.lpSecurityDescriptor = 0&

    udtStart.cb = Len(udtStart)
    udtStart.dwFlags = STARTF_USESTDHANDLES

    ' prcs = "c:\temp\yourapp.exe" your application path

    lngret& = CreateProcessA(0&, prcs, udtSa, udtSa, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, udtStart, udtProc)

    End Function

  26. #26

    Thread Starter
    Addicted Member VB6Coder's Avatar
    Join Date
    Apr 2001
    Location
    Northampton, UK
    Posts
    185
    How do I terminate an application in Windows 2000 that I didn't start, and is already running?

    Cheers

  27. #27
    Fanatic Member pradeepkrao's Avatar
    Join Date
    Sep 2001
    Location
    New Jersey
    Posts
    534

    her it is....

    Hwnd = FindWindow(0&, "Application caption")

    If (Not (0 = Hwnd)) Then
    ret = PostMessage(Hwnd, 16, 0, 0)
    End If


    this would do if it is a window .....

  28. #28
    Member
    Join Date
    Jun 2000
    Location
    France
    Posts
    51

    Cool How about TerminateProcess()

    Thank you pradeepkrao

    I will test your code as soon as possible but can I have a sample code with TerminateProcess() function ?

    (you wrote : "(...)and if it is a simple application without a window... use TerminateProcess()"

    Thank you for your work.

  29. #29
    Fanatic Member pradeepkrao's Avatar
    Join Date
    Sep 2001
    Location
    New Jersey
    Posts
    534
    wait ... i will prepare the code and send it to you after testing it...

    Pradeep

    any help ... mail me at
    [email protected]

  30. #30
    Fanatic Member pradeepkrao's Avatar
    Join Date
    Sep 2001
    Location
    New Jersey
    Posts
    534
    Hi,

    here is the code... To terminate any process one must provide the pid of the process.

    I will create a process and then terminate it...


    copy the code below to your form....


    Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal _
    dwAccess As Long, ByVal fInherit As Integer, ByVal hObject _
    As Long) As Long


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



    Const PROCESS_ALL_ACCESS = &H1F0FFF


    Private Sub Form_Load()

    ProcessID& = Shell("notepad", vbNormalFocus)

    'processID of the application which you want to kill should be supplied

    processhandle& = OpenProcess(PROCESS_ALL_ACCESS, True, ProcessID&)

    MsgBox "notepad has been launched!"

    ret& = TerminateProcess(processhandle, 0&)

    MsgBox "notepad has been terminated!"

    End Sub



    oopppp,

    try it.. and let me know...


    Pradeep

  31. #31

    Thread Starter
    Addicted Member VB6Coder's Avatar
    Join Date
    Apr 2001
    Location
    Northampton, UK
    Posts
    185
    Here is the code to kill a process in Windows 95/98/2000, when you don't know the Process ID.
    You use the PROCESS_TERMINATE constant in the OpenProcess call instead of PROCESS_ALL_ACCESS.




    Code:
    Option Explicit
    
    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 CloseHandle Lib "kernel32" (ByVal _
    hObject As Long) As Long
    
    Private Declare Function OpenProcess Lib "kernel32" (ByVal _
    dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) As Long
    
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal _
    hProcess As Long, ByVal uExitCode As Long) As Long
    
    Private Const MAX_PATH = 260
    
    Private Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long
        th32DefaultHeapID As Long
        th32ModuleID As Long
        cntThreads As Long
        th32ParentProcessID As Long
        pcPriClassBase As Long
        dwFlags As Long
        szexeFile As String * MAX_PATH
    End Type
    
    Private Function KillApp(myName As String) As Boolean
        On Local Error Resume Next
        Const PROCESS_ALL_ACCESS = 0
        Const PROCESS_TERMINATE = 1
        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_TERMINATE, False, uProcess.th32ProcessID)
                AppKill = TerminateProcess(myProcess, exitCode)
                Call CloseHandle(myProcess)
            End If
            rProcessFound = ProcessNext(hSnapshot, uProcess)
        Loop
    
        Call CloseHandle(hSnapshot)
    Finish:
    End Function
    
    Public Sub Main()
        
        Call KillApp("Notepad.exe")
    
    End Sub

  32. #32
    Member
    Join Date
    Jun 2000
    Location
    France
    Posts
    51

    Cool It is very good !

    Ok, I will test pradeepkrao and VB6Coder's code as soon as posssible and I tell you the results.

  33. #33
    Member
    Join Date
    Jun 2000
    Location
    France
    Posts
    51

    Unhappy I tried the two functions...

    I tried to combine

    - the Serge DYMKOV's IsApplicationRunning function and the pradeepkrao's CreateProcess(prcs) function
    - the Serge DYMKOV's IsApplicationRunning function and the VB6Coder's KillApp modified function (with the PROCESS_TERMINATE constant in the OpenProcess call)


    Results : the same thing ! These combined functions don't work with Windows NT !


    This is the Serge DYMKOV's function.

    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 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 ' MAX_PATH
    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

    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

    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, strName As String
    Dim hSnap As Long, 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

    'Usage
    App="Drive\Path\File.exe"
    If IsApplicationRunning(App) Then
    Call KillApp(App) '(or Call CreateProcess(App) )
    End if

  34. #34

    Thread Starter
    Addicted Member VB6Coder's Avatar
    Join Date
    Apr 2001
    Location
    Northampton, UK
    Posts
    185
    The KillApp function works fine on Windows 2000, so I'm surprised it's not working on Windows NT.

    Try just passing the name of the Exe in to the KillApp function. e.g.
    Code:
    Call KillApp("Notepad.exe")
    This should kill all instances of the app.

  35. #35
    Member
    Join Date
    Jun 2000
    Location
    France
    Posts
    51

    Angry Tested

    Try just passing the name of the Exe in to the KillApp function.

    e.g.

    code :

    Call KillApp("Notepad.exe")

    This should kill all instances of the app.
    I tested your function (with the PROCESS_TERMINATE constant in the OpenProcess call) and it doesn't work with Windows NT 4 SP6 !


    The KillApp function works fine on Windows 2000, so I'm surprised it's not working on Windows NT.
    So am I !!!


    However, the Serge DYMKOV's IsApplicationRunning works fine on Windows NT 4.

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