Terminate running process

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

Previous topic - Next topic

Vortex

#27
Hi Quin,

Did you also try the 64-bit version? Probably, an insue related with my system as I compile and run the C++ equivalent of MrBcx's code without any problem.

Quin

I just tried STOP in a Windows 7 Ultimate, SP2 VMWare VM, and it works flawlessly. Not sure what's up there, Vortex  ???

nagrom

thanks.
no crash here
virtualbox - windows7 - bcx 8.25


nagrom

interesting.
can you post the one that had issues
on win 7 ?

Vortex

Hi Kevin,

I tried your new example at work on a Windows 11 2024 H2 system and it worked without any issues. Thanks for your help, much appreciated.

MrBcx

Quote from: Vortex on March 12, 2025, 04:12:58 PM
Hi Kevin,

Many thanks for your code. Testing the application on Windows 7 Sp1 64-bit, the 64-bit version build of the application is crashing after displaying the report. No issue with the 32-bit version.

I'm sure you have your reasons for running a woefully out-of-date version of Windows.

My code compiles to 64-bit using Pelles, MSVC (crt and ucrt), Mingw, Clang and runs fine on Windows 11 24H2.

Anyway, you now have a BCX version of your ChatGPT code, to help understand the changes I made to get things working.



Vortex

#20
Hi Kevin,

Many thanks for your code. Testing the application on Windows 7 Sp1 64-bit, the 64-bit version of the application is crashing after displaying the report. No issue with the 32-bit version.

MrBcx

#19
Erol,

The following was tested with Pelles V12.

It will compile and run in both 32-bit and 64-bit.

It produces, more or less, the same output as the vbscript that ChatGPT provided to you.

See attached sample runs.



' VBScript to display command line of a specific process using Win32_Process class
' Option Explicit

' Specify the process name here (e.g., "notepad.exe")

BCX_SHOW_COM_ERRORS(TRUE)                                                     ' Edited by MrBcx

DIM processName AS STRING                                                     ' Edited by MrBcx
processName = "chrome.exe"

' Create WMI object to access system information

DIM AS OBJECT objWMIService, colProcessList, objProcess                       ' Edited by MrBcx       
DIM strQuery AS STRING                                                        ' Edited by MrBcx

' Set the WMI query to get processes with the specified name
strQuery = "SELECT * FROM Win32_Process WHERE Name = '" + processName + "'"   ' Edited by MrBcx

' Connect to WMI service
COMSET objWMIService = GetObject("winmgmts:\\\\.\\root\\cimv2")               ' Edited by MrBcx

' Execute the query and get the collection of processes
SET colProcessList = objWMIService.ExecQuery(strQuery)

DIM oCount AS INTEGER                                                         ' Edited by MrBcx
' Check if any processes were found

oCount = colProcessList.Count                                                 ' Edited by MrBcx
' IF colProcessList.Count = 0 THEN                                            ' Edited by MrBcx

IF oCount = 0 THEN                                                            ' Edited by MrBcx

    PRINT "No processes found with the name: " & processName
ELSE
    ' Loop through each process and display its command line
 
    DIM Tmp$                                                                  ' Edited by MrBcx
 
    FOR EACH objProcess IN colProcessList
        Tmp$ =  objProcess.ProcessID                                          ' Edited by MrBcx
        Tmp$ = "Process ID: "  + Tmp$                                         ' Edited by MrBcx
        PRINT Tmp$                                                            ' Edited by MrBcx   
       
        Tmp$ =  objProcess.CommandLine                                        ' Edited by MrBcx
        Tmp$ = "Command Line " + Tmp$                                         ' Edited by MrBcx
        PRINT Tmp$                                                            ' Edited by MrBcx
       
        PRINT  "-------------------------"
    NEXT
END IF

' Cleanup
SET objWMIService = NOTHING
SET colProcessList = NOTHING


Vortex

Hi Kevin,

A script proposed by ChatGPT :

' VBScript to display command line of a specific process using Win32_Process class
Option Explicit

' Specify the process name here (e.g., "notepad.exe")
Dim processName
processName = "chrome.exe"

' Create WMI object to access system information
Dim objWMIService, colProcessList, objProcess
Dim strQuery

' Set the WMI query to get processes with the specified name
strQuery = "SELECT * FROM Win32_Process WHERE Name = '" & processName & "'"

' Connect to WMI service
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

' Execute the query and get the collection of processes
Set colProcessList = objWMIService.ExecQuery(strQuery)

' Check if any processes were found
If colProcessList.Count = 0 Then
    WScript.Echo "No processes found with the name: " & processName
Else
    ' Loop through each process and display its command line
    For Each objProcess In colProcessList
        WScript.Echo "Process ID: " & objProcess.ProcessID
        WScript.Echo "Command Line: " & objProcess.CommandLine
        WScript.Echo "-------------------------"
    Next
End If

' Cleanup
Set objWMIService = Nothing
Set colProcessList = Nothing


Executing the script :

cscript cmdline.vbs

MrBcx

Erol,

I think you need to find a working vbscript of what you're trying to do.

If your search is successful then we can have a conversation about converting it to BCX.

Vortex

Hi Kevin,

Trying this one :
Sql$ = "select CommandLine from win32_process where name=" + Cmd$

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

FOR EACH instance IN result
  PRINT instance
NEXT


D:\BCX\bed\test.c(1353): error #2051: Cast from 'OBJECT (aka struct _OBJECT)' to 'double' is invalid.
D:\BCX\bed\test.c(1353): error #2141: Type error in argument 2 to 'printf'; 'void' is invalid.

Vortex

Hi Kevin,

Sorry for asking, how to change your code to display the command line parameters of a specific process? The win32_process class is providing a property named CommandLine :

https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-process

Pseudo-code :

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

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

FOR EACH instance IN result
  PRINT instance.CommandLine
NEXT

or
Sql$ = "select CommandLine from win32_process where name=" + Cmd$

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

FOR EACH instance IN result
  PRINT instance
NEXT


Saleh

Quote from: airr on March 04, 2025, 01:34:04 PM

'********************************************************************

    ' 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



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

AIR.

Be sure that desired access constant PROCESS_TERMINATE is always 1

MrBcx

#13
Quote from: nagrom on March 09, 2025, 08:44:50 PM

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

It sounds like you made some progress.

I keep stop.exe in a folder named C:\Utils\ which is excluded from MS Defender and it is in my system path.

That allows me to use  stop.exe from any folder at any time.