Wednesday 8 May 2019

ListConsole.exe - similar to Unix's $SHLVL variable - list the processes in the current console and returns an errorlevel saying how many.

This uses the inbuilt compilers in Windows 10 - there are three VB.NET compilers and three C# compilers - just copy each text file into the same folder and double click the batch file to make the program.

Similar to Unix's $SHLVL variable but adds ProcessIDs and command lines. List the processes in the current console and returns an ErrorLevel saying how many.




REM ListConsole.bat
REM This file compiles ListConsole.vb to ListConsole.exe
REM ListConsole.exe list the processes in the console and returns an errorlevel saying how many.
REM Similar to Unix's $SHLVL variable but adds ProcessIDs.
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%~dp0\ListConsole.vb" /out:"%~dp0\ListConsole.exe" /target:exe








---------------------------------------------------------------------------------






REM ListConsoleTest.bat
REM Shows ListConsole.exe in action.
cmd /c "%~dp0ListConsole.exe"
Echo Number of Processes is %errorlevel%
pause








-------------------------------------------------------------------------------





'ListConsole.vb
imports System.Runtime.InteropServices
Public Module MyApplication 

Public Declare Function GetConsoleProcessList Lib "Kernel32" (ByRef ProcessList as Integer, ByVal Count as Integer) As Integer
                
Public Sub Main ()
                Dim ProcessList(0 To 9) As Integer
                Dim Count As Integer
                Dim Ret As Integer
                Dim x as Integer
                Dim ColItems as Object
                Dim objWMIService As Object
                objWMIService = GetObject("winmgmts:\\.\root\cimv2")
                Count = 10
                'subtract one to account for this program
                Ret = GetConsoleProcessList(ProcessList(0), 10) - 1
                Console.Writeline("Level = " & Ret)

                For x = Ret  to 1 step -1
                    colItems = objWMIService.ExecQuery("Select * From Win32_Process where ProcessID=" & ProcessList(x))
                    For Each objItem in colItems
                        Console.writeline("PID : " & objItem.ProcessID & "  Command line : " &  objItem.CommandLine)
                    Next
                Next
                Environment.ExitCode = Ret         
End Sub
End Module



No comments:

Post a Comment