Jump to content

Get PID of subprocess


Recommended Posts

Hello !

I am looking for a way to detect a PID of some subprocess ....

By AutoIt script i creating (dynamically) a BAT file with environment specifications...

----------------- BAT FILE --------------------------------------

set path=C:\oracle\product\10.2.0\client_1\bin;%windir%;%windir%\system32

set ORACLE_HOME=C:\mll\oracle\product\10.2.0\client_1

set NLS_LANG=AMERICAN_AMERICA.IW8ISO8859P8

set NLS_DATE_FORMAT=YYYY-MM-DD HH24:MI:SS

cd \

cd /d y:\centura

start /SEPARATE /HIGH c:\soft1.exe

exit

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

After creation that Autoit script - execute this BAT file.

From a Bat file I call to EXE file (For example soft1.exe), witch call to another exe file (soft2.exe)

I am looking for a way to detect PID of soft1.exe and soft2.exe

Please help !

Edited by Yigalr01
Link to comment
Share on other sites

Just yesterday saw the function _ProcessGetChildren($i_pid) as part of this question: Enhancing AutoIT ScreenSaver Example. From a quick look it might be what you're searching :blink:, give it a try...

Edit: Gave it a quick test, be aware that this seems limited to 32bit though...

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_UseX64=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <array.au3>

$aRes = _ProcessGetChildren(ProcessExists("explorer.exe"))
ConsoleWrite(@error)
_ArrayDisplay($aRes)

; _ProcessGetChildren
Func _ProcessGetChildren($i_pid)
    Local Const $TH32CS_SNAPPROCESS = 0x00000002

    Local $a_tool_help = DllCall('Kernel32.dll', 'long', 'CreateToolhelp32Snapshot', 'int', $TH32CS_SNAPPROCESS, 'int', 0)
    If IsArray($a_tool_help) = 0 Or $a_tool_help[0] = -1 Then Return SetError(1, 0, $i_pid)

    Local $tagPROCESSENTRY32 = _
            DllStructCreate _
            ( _
            'dword dwsize;' & _
            'dword cntUsage;' & _
            'dword th32ProcessID;' & _
            'uint th32DefaultHeapID;' & _
            'dword th32ModuleID;' & _
            'dword cntThreads;' & _
            'dword th32ParentProcessID;' & _
            'long pcPriClassBase;' & _
            'dword dwFlags;' & _
            'char szExeFile[260]' _
            )
    DllStructSetData($tagPROCESSENTRY32, 1, DllStructGetSize($tagPROCESSENTRY32))

    Local $p_PROCESSENTRY32 = DllStructGetPtr($tagPROCESSENTRY32)

    Local $a_pfirst = DllCall('Kernel32.dll', 'int', 'Process32First', 'long', $a_tool_help[0], 'ptr', $p_PROCESSENTRY32)
    If IsArray($a_pfirst) = 0 Then Return SetError(2, 0, $i_pid)

    Local $a_pnext, $a_children[11] = [10], $i_child_pid, $i_parent_pid, $i_add = 0
    $i_child_pid = DllStructGetData($tagPROCESSENTRY32, 'th32ProcessID')
    If $i_child_pid <> $i_pid Then
        $i_parent_pid = DllStructGetData($tagPROCESSENTRY32, 'th32ParentProcessID')
        If $i_parent_pid = $i_pid Then
            $i_add += 1
            $a_children[$i_add] = $i_child_pid
        EndIf
    EndIf

    While 1
        $a_pnext = DllCall('Kernel32.dll', 'int', 'Process32Next', 'long', $a_tool_help[0], 'ptr', $p_PROCESSENTRY32)
        If IsArray($a_pnext) And $a_pnext[0] = 0 Then ExitLoop
        $i_child_pid = DllStructGetData($tagPROCESSENTRY32, 'th32ProcessID')
        If $i_child_pid <> $i_pid Then
            $i_parent_pid = DllStructGetData($tagPROCESSENTRY32, 'th32ParentProcessID')
            If $i_parent_pid = $i_pid Then
                If $i_add = $a_children[0] Then
                    ReDim $a_children[$a_children[0] + 10]
                    $a_children[0] = $a_children[0] + 10
                EndIf
                $i_add += 1
                $a_children[$i_add] = $i_child_pid
            EndIf
        EndIf
    WEnd

    If $i_add <> 0 Then
        ReDim $a_children[$i_add + 1]
        $a_children[0] = $i_add
    EndIf

    DllCall('Kernel32.dll', 'int', 'CloseHandle', 'long', $a_tool_help[0])
    If $i_add Then Return $a_children
    Return SetError(3, 0, 0)
EndFunc   ;==>_ProcessGetChildren

Edit 2: And why created a bat file at all? Take a look at EnvSet().

Edited by KaFu
Link to comment
Share on other sites

There's a function in my Process Functions UDF that does the same thing as above basically, but has Unicode support, is x64 safe, and allows more than just PID filtering.

However, I believe all that needs to be changed for the above code for x64 safety is the tagPROCESSENTRY32 struct, namely 'th32DefaultHeapID' needs to be made a 'ulong_ptr'. If you want to add Unicode support, you'll have to change a few other things as well (char to wchar, function names for Process32First/Next need 'W' appended..)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...