Use VBA to kill a specific Access process

I give up. :(
As I mentioned
The following lists the PID as well

tasklist /v /fi "PID gt 0" /fo csv

So I can output to a file, read the file and kill the PID

Thanks to all of you for your assistance.
 
As I mentioned
The following lists the PID as well

tasklist /v /fi "PID gt 0" /fo csv

So I can output to a file, read the file and kill the PID

Thanks to all of you for your assistance.
And I can just kill the process using the window title or part name thereof using taskkill.
No need to output to file, read it in, find the word?


Horses for courses I guess :(
 
There IS a fly in this ointment. If they are all YOUR tasks, you can do this. However, as of Windows NT over 15 years ago, Microsoft implemented the C2 level of security as defined at that time in the U.S. Government's "Orange Book" - a security standards guideline. One of those guidelines is that if you don't have sufficient privilege, you cannot kill another person's task. Depending on which utility is involved, that might extend to being unable to even SEE details of another person's task.

You didn't mention the security level of your environment. I was in a U.S. Navy personnel department with SECRET clearance (at the time). You can guess how our IT network team set up our security levels. But basically, I found that the best way to kill a process that theoretically was still running was to persuade it to kill itself. As long as it wasn't hung up on something, if I dropped a record in a table that was visible to all FE instances, I could get one or all app instances to disengage and shut down. This required a form that was always open so that it could support an OnTimer event to CHECK for the new "go away now" record, but my system used a switchboard form that never "went away" so that wasn't a show-stopper.
 
There IS a fly in this ointment. If they are all YOUR tasks, you can do this. However, as of Windows NT over 15 years ago, Microsoft implemented the C2 level of security as defined at that time in the U.S. Government's "Orange Book" - a security standards guideline. One of those guidelines is that if you don't have sufficient privilege, you cannot kill another person's task. Depending on which utility is involved, that might extend to being unable to even SEE details of another person's task.

You didn't mention the security level of your environment. I was in a U.S. Navy personnel department with SECRET clearance (at the time). You can guess how our IT network team set up our security levels. But basically, I found that the best way to kill a process that theoretically was still running was to persuade it to kill itself. As long as it wasn't hung up on something, if I dropped a record in a table that was visible to all FE instances, I could get one or all app instances to disengage and shut down. This required a form that was always open so that it could support an OnTimer event to CHECK for the new "go away now" record, but my system used a switchboard form that never "went away" so that wasn't a show-stopper.
I have control over all Access sessions, so security issues.
I am investigating using RemoteApp to put one of my applications on Azure. So I will have several sessions working and if something goes wrong with one of these sessions, then based on the client number I can kill the process.
I have the same application developed on .net and SQL server on Azure, but the setup and support is much greater.
Also with RemoteApp, the application will be available on Mac, tablet, mobile. We'll see if it works out.
 
At the end of the day, as Gasman suggested and since I know the Window/Application Title, I merely need to run
if Title name = abc


taskkill /fi "WindowTitle eq abc" /f
 
usually you can "see" the name of the db on the title bar.
you can use that title to kill the process.

Call killWindow("partialNameHere*")
Code:
Option Explicit

' Module Name: ModFindWindowLike
' (c) 2005 Wayne Phillips (http://www.everythingaccess.com)
' Written 02/06/2005
'
' mODIFIED bY aRNELgP FOR x64 aCCESS
'
                 
#If VBA7 Then
    Private Declare PtrSafe Function EnumWindows Lib "user32" (ByVal lpEnumFunc As LongPtr, ByVal lParam As LongPtr) As Long
    Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal Hwnd As LongPtr, ByVal lpString As String, ByVal cch As Long) As Long
       
        'Could use global variables instead, but this is nicer.
        'Custom structure for passing in the parameters in/out of the hook enumeration function
        'Could use global variables instead, but this is nicer.
        Private Type FindWindowParameters
       
            strTitle As String  'INPUT
            Hwnd As LongPtr     'OUTPUT
       
        End Type
#Else
                   
    Private Declare Function EnumWindows Lib "user32" _
       (ByVal lpEnumFunc As Long, _
        ByVal lParam As Long) As Long
   
    Private Declare Function GetWindowText Lib "user32" _
        Alias "GetWindowTextA" _
       (ByVal hwnd As Long, _
        ByVal lpString As String, _
        ByVal cch As Long) As Long
        'Could use global variables instead, but this is nicer.
        'Custom structure for passing in the parameters in/out of the hook enumeration function
        'Could use global variables instead, but this is nicer.
        Private Type FindWindowParameters
       
            strTitle As String  'INPUT
            Hwnd As Long        'OUTPUT
       
        End Type
#End If

'''experimental''''''''''''''''
Private Const WM_CHAR = &H102
Private Const BM_CLICK As Long = &HF5&

''' close the window
Private Const WM_SYSCOMMAND = &H112
Private Const SC_CLOSE = &HF060

Private Const WM_CLOSE = &H10

#If VBA7 Then
Private Declare PtrSafe Function SendMessageBynum Lib "user32" Alias "SendMessageA" (ByVal Hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal Hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As Long

#Else
private Declare Function SendMessageBynum Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As any) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
#End If
'''''''''''''''''''''



#If VBA7 Then
Public Function FnFindWindowLike(strWindowTitle As String) As LongPtr

#Else
Public Function FnFindWindowLike(strWindowTitle As String) As Long
#End If
   
    'We'll pass a custom structure in as the parameter to store our result...
    Dim Parameters As FindWindowParameters
    Parameters.strTitle = UCase(strWindowTitle) ' Input parameter
   

#If VBA7 Then
    Call EnumWindows(AddressOf EnumWindowProc, VarPtr(Parameters))
#Else
    Call EnumWindows(AddressOf EnumWindowProc, VarPtr(Parameters))
#End If
    FnFindWindowLike = Parameters.Hwnd
   
End Function

#If VBA7 Then
Private Function EnumWindowProc(ByVal Hwnd As LongPtr, _
                               lParam As FindWindowParameters) As LongPtr
#Else
Private Function EnumWindowProc(ByVal Hwnd As Long, _
                               lParam As FindWindowParameters) As Long
#End If
   Dim strWindowTitle As String

   strWindowTitle = Space(260)
   Call GetWindowText(Hwnd, strWindowTitle, 260)
   strWindowTitle = UCase(TrimNull(strWindowTitle)) ' Remove extra null terminator
                                         
   If strWindowTitle Like lParam.strTitle Then
 
        lParam.Hwnd = Hwnd 'Store the result for later.
        EnumWindowProc = 0 'This will stop enumerating more windows
 
   Else

        EnumWindowProc = 1

   End If
                         
End Function

Private Function TrimNull(strNullTerminatedString As String)

    Dim lngPos As Long

    'Remove unnecessary null terminator
   
    lngPos = InStr(strNullTerminatedString, Chr$(0))
 
    If lngPos Then
        TrimNull = Left$(strNullTerminatedString, lngPos - 1)
    Else
        TrimNull = strNullTerminatedString
    End If
   'Debug.Print TrimNull
End Function

' arnelgp
Public Function killWindow(ByVal strWindowTitle As String)
#If VBA7 Then
    Dim Hwnd As LongPtr
#Else
    Dim Hwnd As Long
#End If
    Hwnd = FnFindWindowLike(strWindowTitle & "*")
   
    If Hwnd <> 0 Then
        Call SendMessageBynum(Hwnd, WM_CLOSE, 0, 0)
    End If
End Function
If I want to use the code above for Access 2013 do I use the other code than the VBA7 code?
 

Users who are viewing this thread

Back
Top Bottom