Sign in to follow this  
Followers 0
=sinister=

_ProcessCount?

10 posts in this topic

I'm starting a new project, and simple project that will show everything about processes and there PID Number and everything. Like an upgraded Task Manager. I've gotten the CPU Usage UDF down, but I'm stuck with counting Processes. I found this script from somewhere lost in the Scripts and Scraps forum:

$Test = Ubound(ProcessList())
MsgBox(0, "Test", $Test)

But unfortunatley, I tested it and it did not work. So I was wondering if it would be possible to count the processes?

Share this post


Link to post
Share on other sites



$procList = ProcessList()
MsgBox(0, "Test", UBound($procList))

This is what you are looking for.

Also check my signature for UDF's I am working on some that may be of interest to you. _ComputerGetProcesses and _ComputerGetThreads.

I hope that helps.

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

@JS

The script still doesn't work =(

@gafrost

Thanks for the reply, I looked threw lary's script, and I put a Ubound around variable $b, and the message box pops up saying '0'

$a = ProcessList()
$b = ""
For $n = 1 to $a[0][0]
    $b = $b & GetProcessName($a[$n][1]) & @LF
Next

MsgBox(4096,"", Ubound($b))

Func GetProcessName(ByRef $PID)
    Local $ret
    If @OSTYPE = "WIN32_WINDOWS" Then
        $ret = GetProcessName95($PID)
        SetError(@error)
        Return $ret
    Else
        $ret = GetProcessNameNT($PID)
        SetError(@error)
        Return $ret
    EndIf
EndFunc

Func GetProcessName95(ByRef $PID)
    Const $TH32CS_SNAPPROCESS = 0x00000002
    Const $INVALID_HANDLE_VALUE = -1
    Local $ToolHandle, $Process, $nRet

    $ToolHandle = DLLCall("kernel32.dll","hwnd","CreateToolhelp32Snapshot","int",$TH32CS_SNAPPROCESS,"int",0)
    If $ToolHandle[0] = $INVALID_HANDLE_VALUE Then Return ("" & SetError(1))
    
    $Process = DLLStructCreate("int;int;int;uint;int;int;int;int;int;char[260]")
    DllStructSetData($Process,1,DLLStructGetSize($Process))

    $nRet = DLLCall("kernel32.dll","int","Process32First","hwnd",$ToolHandle[0],"ptr",DllStructGetPtr($Process))
    If $nRet[0] Then
        Do
            If DLLStructGetData($Process,3) = $PID Then
                $nRet = DllStructGetData($Process,10)
                $Process = 0
                Return $nRet
            EndIf
            $nRet = DLLCall("kernel32.dll","int","Process32Next","hwnd",$ToolHandle[0],"ptr",DllStructGetPtr($Process))
        Until Not $nRet[0]
    EndIf
    $Process = 0
    DLLCall("kernel32.dll","int",$ToolHandle[0])
    SetError(1)
    Return
EndFunc

Func GetProcessNameNT(ByRef $PID)
    Local $Process, $Modules, $Ret
    Const $PROCESS_QUERY_INFORMATION = 0x0400
    Const $PROCESS_VM_READ = 0x0010
    $Process = DLLCall("kernel32.dll","hwnd","OpenProcess","int", _
            BitOR($PROCESS_QUERY_INFORMATION,$PROCESS_VM_READ),"int",0,"int",$PID)
    If $Process[0] = 0 Then Return SetError(1)
    
    $Modules = DLLStructCreate("int[1024]")
    DLLCall("psapi.dll","int","EnumProcessModules","hwnd",$Process[0],"ptr",DllStructGetPtr($Modules), _
            "int",DllStructGetSize($Modules),"int_ptr",0)
    
    $Ret = DLLCall("psapi.dll","int","GetModuleFileNameEx","hwnd",$Process[0],"int",DllStructGetData($Modules,1), _
            "str","","int",2048)
    $Modules = 0
    If StringLen($Ret[3]) = 0 Then Return SetError(1)
    Return $Ret[3]
EndFunc

Share this post


Link to post
Share on other sites

JSThePatriot must have been tired when he posted that because I can't see

any practical difference between the script you posted and the script he posted.

Or maybe it's just me who's tired..

Anyway, have you checked @error after doing ProcessList() ? Is it set to 1 ?

If so, then an error have occured. Why and how to solve it I don't know :whistle:

Have to ask the devs for that I guess...

Share this post


Link to post
Share on other sites

Hmm... I tried it a second time, and I looked at the Task Manager processes, and his. His was always 1 off. Don't know why. So I added a solution:

$ProcessCount = ProcessList()
$ProcessCountMinus = Ubound($ProcessCount) - 1
MsgBox(0, "test", $ProcessCountMinus)

Now it's the same as the Task Manager's.

Share this post


Link to post
Share on other sites

Well... duh. The first element of the array is not a process, it's the number of elements in the array. So of course it's going to be one too many. That should have been obvious to anybody who looked at the documentation for ProcessList(). You guys did look at the documentation, right?

Share this post


Link to post
Share on other sites

$Test = Ubound(ProcessList())
MsgBox(0, "Test", $Test)

But unfortunatley, I tested it and it did not work. So I was wondering if it would be possible to count the processes?

and the question is: What's the problem with the code? On my system it returns 118, which is exactly the number of the processes + 1 (see valiks post).

Cheers

Kurt


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

@JS

The script still doesn't work =(

@gafrost

Thanks for the reply, I looked threw lary's script, and I put a Ubound around variable $b, and the message box pops up saying '0'

$a = ProcessList()
$b = ""
For $n = 1 to $a[0][0]
    $b = $b & GetProcessName($a[$n][1]) & @LF
Next

MsgBox(4096,"", Ubound($b))

Func GetProcessName(ByRef $PID)
    Local $ret
    If @OSTYPE = "WIN32_WINDOWS" Then
        $ret = GetProcessName95($PID)
        SetError(@error)
        Return $ret
    Else
        $ret = GetProcessNameNT($PID)
        SetError(@error)
        Return $ret
    EndIf
EndFunc

Func GetProcessName95(ByRef $PID)
    Const $TH32CS_SNAPPROCESS = 0x00000002
    Const $INVALID_HANDLE_VALUE = -1
    Local $ToolHandle, $Process, $nRet

    $ToolHandle = DLLCall("kernel32.dll","hwnd","CreateToolhelp32Snapshot","int",$TH32CS_SNAPPROCESS,"int",0)
    If $ToolHandle[0] = $INVALID_HANDLE_VALUE Then Return ("" & SetError(1))
    
    $Process = DLLStructCreate("int;int;int;uint;int;int;int;int;int;char[260]")
    DllStructSetData($Process,1,DLLStructGetSize($Process))

    $nRet = DLLCall("kernel32.dll","int","Process32First","hwnd",$ToolHandle[0],"ptr",DllStructGetPtr($Process))
    If $nRet[0] Then
        Do
            If DLLStructGetData($Process,3) = $PID Then
                $nRet = DllStructGetData($Process,10)
                $Process = 0
                Return $nRet
            EndIf
            $nRet = DLLCall("kernel32.dll","int","Process32Next","hwnd",$ToolHandle[0],"ptr",DllStructGetPtr($Process))
        Until Not $nRet[0]
    EndIf
    $Process = 0
    DLLCall("kernel32.dll","int",$ToolHandle[0])
    SetError(1)
    Return
EndFunc

Func GetProcessNameNT(ByRef $PID)
    Local $Process, $Modules, $Ret
    Const $PROCESS_QUERY_INFORMATION = 0x0400
    Const $PROCESS_VM_READ = 0x0010
    $Process = DLLCall("kernel32.dll","hwnd","OpenProcess","int", _
            BitOR($PROCESS_QUERY_INFORMATION,$PROCESS_VM_READ),"int",0,"int",$PID)
    If $Process[0] = 0 Then Return SetError(1)
    
    $Modules = DLLStructCreate("int[1024]")
    DLLCall("psapi.dll","int","EnumProcessModules","hwnd",$Process[0],"ptr",DllStructGetPtr($Modules), _
            "int",DllStructGetSize($Modules),"int_ptr",0)
    
    $Ret = DLLCall("psapi.dll","int","GetModuleFileNameEx","hwnd",$Process[0],"int",DllStructGetData($Modules,1), _
            "str","","int",2048)
    $Modules = 0
    If StringLen($Ret[3]) = 0 Then Return SetError(1)
    Return $Ret[3]
EndFunc
And why would you Ubound $b? it's a string

You already have your answer for process count "$a[0][0]"


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

When I UBound() mine came up with the number 42, I checked my process list, and there were 42 running processes. Now... I think that is because I opened my Taskmanager after running the script come to think of it.

@Valik

Yes, I have read the documentation, but simply forgot about it and since my mistake in "checking" my output thought I was correct.

@Helge

There should be no difference in my script and the other, but I have never tried to UBound() a function.

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0