Terminate running process

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

Previous topic - Next topic

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.

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,

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

#18
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

#19
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

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

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.

nagrom

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


nagrom

thanks.
no crash here
virtualbox - windows7 - bcx 8.25

Quin

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

Vortex

#26
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.