Terminate running process

Started by airr, March 04, 2025, 01:34:04 PM

Previous topic - Next topic

nagrom

#12
Quote from: MrBcx on March 09, 2025, 07:02:38 PM
Quote from: nagrom on March 09, 2025, 03:41:35 PM

i already tried this before i posted.
right click, run as admin.


NAGROM,

STOP.exe is designed to be run from a console or batch file or shell command inside another app.

Right click, run as Admin but passing no command line argument is completely pointless.

What I recommend is change the properties of Stop.exe as shown in the attached screenshot.

Then test :

1) Start Notepad

2) Start a command prompt (console window)

3) CHDIR to the folder where Stop.exe is located

4) Type STOP notepad and press ENTER

5) Notepad should close


This works for me on Windows 11 Pro 24H2

If it works for you then you can resume testing STOP.exe for your own purposes.
i forgot to mention
i changed the code a little
and replaced the cmdline args
with a process i ran .
it's a media player.

i will try more.

EDIT:
it works when i run it from the process folder

MrBcx

#11
Quote from: nagrom on March 09, 2025, 03:41:35 PM

i already tried this before i posted.
right click, run as admin.


NAGROM,

STOP.exe is designed to be run from a console or batch file or shell command inside another app.

Right click, run as Admin but passing no command line argument is completely pointless.

What I recommend is change the properties of Stop.exe as shown in the attached screenshot.

Then test :

1) Start Notepad

2) Start a command prompt (console window)

3) CHDIR to the folder where Stop.exe is located

4) Type STOP notepad and press ENTER

5) Notepad should close


This works for me on Windows 11 Pro 24H2

If it works for you then you can resume testing STOP.exe for your own purposes.

Vortex

Hi nagrom,

I tested MrBcx's application on Window 7 Sp1 and it works without any issues. What's your operating system?

nagrom

#9
Quote from: airr on March 09, 2025, 03:10:21 PM
Hi, nagrom.

With any of these proposed approaches, you may need to run with administrator rights if the process was launched or is running at the system level.

I just tried MrB's solution on a process that is launched at system start up, and it would not terminate until I launched the Stop program with administrator rights.

HTH.

AIR.
i already tried this before i posted.
right click, run as admin.

i also searched the net before posting.
i  found that there are problems with this method.
i didn't tried all the vbs suggested solutions though

airr

Hi, nagrom.

With any of these proposed approaches, you may need to run with administrator rights if the process was launched or is running at the system level.

I just tried MrB's solution on a process that is launched at system start up, and it would not terminate until I launched the Stop program with administrator rights.

HTH.

AIR.

nagrom

Quote from: MrBcx on March 04, 2025, 05:45:14 PM
Here is variation on the theme that I shared a long time ago.

I keep the executable in my path, so I can use it in batch files or to drop
into a command prompt to kill a non-responding browser and the like.

It compiles with everything.



GLOBAL wmi AS OBJECT
GLOBAL result AS OBJECT
GLOBAL Cmd$
GLOBAL Sql$

Cmd$ = COMMAND$(1)

IF Cmd$ = "" THEN
  PRINT "STOP - by Kevin Diggins - Freeware - 2010"
  PRINT "Usage: STOP ExecutableName[.exe]"
  PRINT "Will STOP all running instances of 'ExecutableName' found"
  END
END IF

Cmd$ = LCASE$(Cmd$)
Cmd$ = EXTRACT$(Cmd$,".exe")
Cmd$ = Cmd$ + ".exe"
Cmd$ = "'" + Cmd$ + "'"

Sql$ = "select * from win32_process where name=" + Cmd$

SET wmi = GetObject("winmgmts:")
SET result = wmi.ExecQuery(Sql$)

FOR EACH instance IN result
  instance.Terminate(0)
NEXT

SET wmi = NOTHING
SET result = NOTHING


compiled but does not terminate

airr

Quote from: Vortex on March 05, 2025, 11:31:33 AM
Hello,

Thanks for your efforts. Another method to terminate a process is to use the Windows built-in command taskkill.

Hi, Vortex.

I actually use this command frequently when creating automation scripts.  Thanks for mentioning it!


@MrB.

Your solution using WMI is really short and sweet, thanks for (re)sharing that!!

AIR.

MrBcx

Quote from: Vortex on March 05, 2025, 11:31:33 AM
Hello,

Thanks for your efforts. Another method to terminate a process is to use the Windows built-in command taskkill.

Use them all together and you'll have an international application death squad.   :D

Vortex

Hello,

Thanks for your efforts. Another method to terminate a process is to use the Windows built-in command taskkill.

MrBcx

Here is variation on the theme that I shared a long time ago.

I keep the executable in my path, so I can use it in batch files or to drop
into a command prompt to kill a non-responding browser and the like.

It compiles with everything.



GLOBAL wmi AS OBJECT
GLOBAL result AS OBJECT
GLOBAL Cmd$
GLOBAL Sql$

Cmd$ = COMMAND$(1)

IF Cmd$ = "" THEN
  PRINT "STOP - by Kevin Diggins - Freeware - 2010"
  PRINT "Usage: STOP ExecutableName[.exe]"
  PRINT "Will STOP all running instances of 'ExecutableName' found"
  END
END IF

Cmd$ = LCASE$(Cmd$)
Cmd$ = EXTRACT$(Cmd$,".exe")
Cmd$ = Cmd$ + ".exe"
Cmd$ = "'" + Cmd$ + "'"

Sql$ = "select * from win32_process where name=" + Cmd$

SET wmi = GetObject("winmgmts:")
SET result = wmi.ExecQuery(Sql$)

FOR EACH instance IN result
  instance.Terminate(0)
NEXT

SET wmi = NOTHING
SET result = NOTHING




Quin

Works well here with MinGW, Pelles, and MSVC 2022, both with and without the UCRT. Thanks for sharing! :)

airr


'********************************************************************
' KillProcess.bas
'
' Demonstrates how to terminate a running process
'
' Author:   Armando I. Rivera (AIR)
' Date:     2025-03-04
' NOTE:     If you wish to terminate a process running at the system
'           level, you will have to execute this with admin rights.
'
' Compiles with PellesC, MinGW, and MSVC
'********************************************************************

Function main(argc as integer, argv as pchar ptr)

    Dim As String processName

    if ARGC < 2 then print "Usage: ", appexename$, " <Name of process to terminate>": end = 1

    processName = command$(1)
    Dim result = KillProcess(processName) 

End Function

$HEADER
    #include <tlhelp32.h>
$HEADER

Function KillProcess(filename$) As Integer
    Dim As DWORD processPID = 0
    Dim As HANDLE hSnapshot, hProcess
    Dim As PROCESSENTRY32 pe32
    Dim As BOOL found = FALSE
    Dim As Integer result = 0

    ' Take a snapshot of all processes
    hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    If hSnapshot = INVALID_HANDLE_VALUE Then
        Print "Failed to create process snapshot. Error: ", GetLastError()
        Return 1
    End If

    ' Set the size of the structure before using it
    pe32.dwSize = sizeof(PROCESSENTRY32)

    ' Get the first process
    If Not Process32First(hSnapshot, &pe32) Then
        Print "Failed to get first process. Error: ", GetLastError()
        CloseHandle(hSnapshot);
        Return 2
    End IF

    ' Find the process
    Do
        If pe32.szExeFile$ = filename$ Then
            processPID = pe32.th32ProcessID
            found = TRUE
            Exit Do
        End IF
    Loop While Process32Next(hSnapshot, &pe32)

    CloseHandle(hSnapshot)

    If Not found Then
        Print filename$," process not found"
        Return 3
    End If

    ' Open the requested process
    hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, processPID)
    If hProcess = NULL Then
        print "Failed to open ", filename$, " process. Error:  ", GetLastError()
        return 4
    End If

    ' Terminate the requested process
    If Not TerminateProcess(hProcess, 0) Then
        print "Failed to terminate ", filename$, " process. Error: ", GetLastError()
        CloseHandle(hProcess)
        Return 5
    End If

    CloseHandle(hProcess)
   
    print filename$," terminated successfully."

    ' Wait a moment to ensure the process is fully terminated
    Sleep(1000)


    Return result
End Function



I needed something like this at work today, and threw this together....

AIR.