Sign in to follow this  
Followers 0
MrPPP

PID not unique?

30 posts in this topic

Hi there everybody,

I was just wondering about something that has to do with pids and handles. I try to get the handle of a window of which I already have the pid as returned by RUN. Now, I try to find the handle of iexplorer.exe window that I just opened with RUN. I found some nice things on the forum (using WinList and WinGetProcess together) and they work fine untill I have two different iexplorer.exe processes running at the same time. So I wondered what was wrong and found out that they have the same pid. They do, however have different handles. Is this normal? And is there another way of finding the handle of the process that you open with RUN? Thanks!

Greetings, Mr P

Ps.1

An example of what I found .

Google - Windows Internet Explorer 0x000605FA 4032

Google - Windows Internet Explorer 0x000906FC 4032

Google - Windows Internet Explorer 0x00200430 4032

Ps2. I used this code to match the handles to the windowtitles and pids:

$var = WinList()
For $i = 1 to $var[0][0]
    IniWrite ( "test3.ini", "A", $var[$i][0] & "=" & $var[$i][1], WinGetProcess ( $var[$i][0] , "" ) )
Next

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

A single process (PID) with multiple windows (WinList'ed handles). That's very normal.

:x

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

A single process (PID) with multiple windows (WinList'ed handles). That's very normal.

:x

Because taskmanager shows 3 different iexplorer.exe processes I would say they need to have a different PID, but they all have the same PID. So it's not that normal right?

Share this post


Link to post
Share on other sites

Because taskmanager shows 3 different iexplorer.exe processes I would say they need to have a different PID, but they all have the same PID. So it's not that normal right?

Were you really viewing the processes-tab (second tab) or did you have the first tab (Task/"program"/window tab) active?

/Manko


Yes i rush things! (I sorta do small bursts inbetween doing nothing.) Things I have rushed and reRushed:* ProDLLer - Process manager - Unload viri modules (dll) and moore...* _WinAPI_ProcessListOWNER_WTS() - Get Processes owner list...* _WinAPI_GetCommandLineFromPID() - Get commandline of target process...* _WinAPI_ThreadsnProcesses() Much info if expanded - optional Indented "Parent/Child"-style Processlist. Moore to come... eventually...

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

This is an interesting issue.

Firstly, PsaltyDS is correct, as usual, WinList will list all process and threads. Many IE windows and FF windows are usually controlled by one process, thus when one crashes, they all do.

Secondly, a PID is for every process regardless, not threads. There are separate identifiers for threads within windows.

Thirdly, if you are seeing ieexplore.exe listed in task manager three times there are three ieexplore.exe process running. I have seen IE running with no window open when testing with Selenium coz IE can't shut itself down correctly all the time.

Fourthly, if you searching on the process name and there is more than one instance of the same name (as in your case) the instance returned will be the first one it found, you need to uniquely identify the instances or only have one running.

Fifthly, Window handles are different from PIDs.

Lastly, kill all ieexplore sessions then test again. You can use the command from the cmdline "taskkill" to do this.

There is one good UDF posted that converts a PID to a Window Handle. Fount it - you can thank PsaltyDS for it.

Its worth searching for other functions if this does not work.

Also check out

Edited by bo8ster

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Share this post


Link to post
Share on other sites

Hello, I'm back. I hope you all had a nice X-mas:) So I read what the posts and I tried the following:

$sURL = "www.google.nl"

$pid = Run(@ProgramFilesDir&"\internet explorer\iexplore.exe "&$sURL, "",@SW_SHOW)
$Hwnd = _GetHwndFromPID($pid)
Sleep ( 3000 )
Winclose ( $Hwnd )


Func _GetHwndFromPID($PID)
    $hWnd = 0
    $stPID = DllStructCreate("int")
    Do
        $winlist2 = WinList()
        For $i = 1 to $winlist2[0][0]
            If $winlist2[$i][0] <> "" Then
                DllCall("user32.dll", "int", "GetWindowThreadProcessId", "hwnd", $winlist2[$i][1], "ptr", DllStructGetPtr($stPID))
                If DllStructGetData($stPID, 1) = $PID Then
                    $hWnd = $winlist2[$i][1]
                    ExitLoop
                EndIf
            EndIf
        Next
        Sleep(100)
    Until $hWnd <> 0
    Return $hWnd
EndFunc ;==>_GetHwndFromPID

Now, this works perfect but only when I have one Internet Explorer Window opened. As soon as other windows are opened the script hangs and non of them gets closed. What I would like to do is to have the HWnd of the window that was opened by "Run" also if there are other iexplorer windows opened. Does anybody know how to do that?

Greetings, MrPPP

Share this post


Link to post
Share on other sites

Excuse me if I'm mistaking, but bo8ster's first link meets the needs of yours I suppose... :x

Share this post


Link to post
Share on other sites

I cant help to much because all the IE windows on my machine have unique PID

This cheap piece of code will give you an array of all IE window handles (assuming they are all google, if not modify just to look for "Internet Explorer")

Once you have that array, you can run your new IE instance, and repeat the same code (different vars of course) and get a second array.

There will be an extra handle in the second array, which is the one you are after, I assume I can leave that for yourself to determine.

#include <Array.au3>

Dim $aIEHWND[1] = [1]

$aWinlist = WinList()

For $i = 1 To $aWinlist[0][0]
    If $aWinlist[$i][0] = "Google - Windows Internet Explorer" Then
        $aIEHWND[0] += 1
        ReDim $aIEHWND[$aIEHWND[0]]
        $aIEHWND[$aIEHWND[0]-1] = $aWinlist[$i][1]
    EndIf
Next
_ArrayDisplay($aIEHWND)

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Excuse me if I'm mistaking, but bo8ster's first link meets the needs of yours I suppose... :x

No, because you need to know the window title, and I do not want to use the window title because it is changing during the moment I want to assign a handle.

Share this post


Link to post
Share on other sites

I cant help to much because all the IE windows on my machine have unique PID

This cheap piece of code will give you an array of all IE window handles (assuming they are all google, if not modify just to look for "Internet Explorer")

Once you have that array, you can run your new IE instance, and repeat the same code (different vars of course) and get a second array.

There will be an extra handle in the second array, which is the one you are after, I assume I can leave that for yourself to determine.

#include <Array.au3>

Dim $aIEHWND[1] = [1]

$aWinlist = WinList()

For $i = 1 To $aWinlist[0][0]
    If $aWinlist[$i][0] = "Google - Windows Internet Explorer" Then
        $aIEHWND[0] += 1
        ReDim $aIEHWND[$aIEHWND[0]]
        $aIEHWND[$aIEHWND[0]-1] = $aWinlist[$i][1]
    EndIf
Next
_ArrayDisplay($aIEHWND)

This is a good idea, I'll try to do this. If I have something that works I'll post it. Thanks!

Share this post


Link to post
Share on other sites

I used your idea, the implementation is a bit different but what I have now is the following:

#include <Array.au3>

$sURL = "www.google.nl"
$WinTitle = "Internet Explorer"
Local $aWinListBefore = WinList()
Local $pid = Run(@ProgramFilesDir&"\internet explorer\iexplore.exe "&$sURL, "",@SW_SHOW)
$Hwnd = _GetHwndFromLastOpenedWindow($aWinListBefore, $WinTitle)

Winclose ( $Hwnd )

Func _GetHwndFromLastOpenedWindow(ByRef $aWinListBefore, ByRef $WinTitle)
$Timer = TimerInit()    
    While TimerDiff( $Timer ) < 10000 
        Local $aBefore[1]
        Local $aAfter[1]
        Local $aWinListAfter = WinList()
        
        For $i = 1 To $aWinListBefore[0][0]
            If StringRegExp ( $aWinListBefore[$i][0], $WinTitle, 0 ) Then
                _ArrayAdd ($aBefore, $aWinListBefore[$i][1])
            EndIf
        Next
        
        For $i = 1 To $aWinListAfter[0][0]
            If StringRegExp ( $aWinListAfter[$i][0], $WinTitle, 0 ) Then
                _ArrayAdd ($aAfter, $aWinListAfter[$i][1])
            EndIf
        Next    
        
        For $i = 0 To UBound($aAfter) - 1
            If _ArraySearch($aBefore, $aAfter[$i]) = -1 Then
            Return $aAfter[$i]
            ExitLoop(2)
            EndIf
        Next
    WEnd
EndFunc

This works but still I need the window title. I tried to do it matching the PID of every process to the PID given by "Run" but that does only work when I have one Internet Explorer Window Opened. I tried to found out why, what I found was that when I open my first IE window this window will have the same PID as the PID returned by "Run". But when I open a second IE window it gets the same PID as the first IE window I opened. This means that it does no longer match the PID returned by "Run"! Does anybody have any ideas about how to fix this?

Share this post


Link to post
Share on other sites

I use this function to get all window titles or handles from one PID.

#include <Array.au3>

Opt("WinTitleMatchMode", 2)

Local $PID = ProcessExists("iexplore.exe")
If $PID = 0 Then
$PID = Run(@ProgramFilesDir & "\Internet Explorer\iexplore.exe")
EndIf

WinWait("Internet Explorer")

Local $aTitle = _PID2WinTitle($PID)
_ArrayDisplay($aTitle)

Func _PID2WinTitle($PID)
;funkey 1st Nov 2008
Local $aWinTitle[1] = [0], $List = WinList()
For $i = 1 To $List[0][0]
  If $PID = WinGetProcess($List[$i][0]) Then
   ReDim $aWinTitle[$aWinTitle[0] + 2]
   $aWinTitle[0] += 1
   $aWinTitle[$aWinTitle[0]] = $List[$i][0]  ;$List[$i][0] for titles, $List[$i][1] for handles ;)
  EndIf
Next
Return $aWinTitle
EndFunc   ;==>_PID2WinTitle

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

And it would probably work great for me if the PID was not changing after I started IE. Because then you can not use the PID any more to find to windows associated to it... Because it does not have any windows associated to it any more!

Share this post


Link to post
Share on other sites

And it would probably work great for me if the PID was not changing after I started IE. Because then you can not use the PID any more to find to windows associated to it... Because it does not have any windows associated to it any more!

PIDs do not change once a process is started (Normally within Windows). Does not matter what you may think, that is a fact.

Display the output of WinList and Task Manager with the PID column sorted by Image Name.


Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Share this post


Link to post
Share on other sites

PIDs do not change once a process is started (Normally within Windows). Does not matter what you may think, that is a fact.

Display the output of WinList and Task Manager with the PID column sorted by Image Name.

Then something weird is happening. I did the following:

#include <Array.au3>

$sURL = "www.google.nl"
Local $pid = Run(@ProgramFilesDir&"\internet explorer\iexplore.exe "&$sURL, "",@SW_SHOW)
Run ( "notepad.exe" )
Sleep ( 1000)
Global $hOut = WinGetTitle ( "Naamloos" )

ControlSend ( $hOut, "", "[CLASS:Edit; INSTANCE:1]", "Pid as returned by Run: "&$pid&"{ENTER}{ENTER}")

        Local $aProcessList = ProcessList()
        
        For $i = 1 To $aProcessList[0][0]
        ControlSend ( $hOut, "", "[CLASS:Edit; INSTANCE:1]", $aProcessList[$i][0]&" "&$aProcessList[$i][1]&"{ENTER}")
        Next

Now, the first time I run this program. I get the following output:

Pid as returned by Run: 1156

[System Process] 0
System 4
SMSS.EXE 916
CSRSS.EXE 984
winlogon.exe 1016
SERVICES.EXE 1060
LSASS.EXE 1072
ATI2EVXX.EXE 1220
SVCHOST.EXE 1244
SVCHOST.EXE 1332
MsMpEng.exe 1376
SVCHOST.EXE 1536
EvtEng.exe 1592
ATI2EVXX.EXE 1656
S24EvMon.exe 1712
SVCHOST.EXE 1936
SVCHOST.EXE 204
SPOOLS.exe 760
SVCHOST.EXE 836
CLCapSvc.exe 924
CLMLServer.exe 1580
CLMLService.exe 1796
JQS.EXE 1868
PnkBstrA.exe 292
RegSrvc.exe 352
SVCHOST.EXE 588
SVCHOST.EXE 640
CLSched.exe 1792
EXPLORER.EXE 2796
alg.exe 2812
HControl.exe 3160
EHTRAY.EXE 3224
DMedia.exe 3248
ACMON.EXE 3256
EHMSAS.EXE 3296
wcourier.exe 3292
SynTPEnh.exe 3336
ACEngSvr.exe 3492
ZCfgSvc.exe 3668
ATKOSD.exe 3784
iFrmewrk.exe 3884
EOUWiz.exe 3940
RTHDCPL.EXE 4044
MSSECES.EXE 492
MOM.exe 624
gnotify.exe 1564
ctfmon.exe 2460
Dot1XCfg.exe 2124
ccc.exe 4056
GoogleUpdate.exe 2136
firefox.exe 196
SciTE.exe 5984
AutoIt3Help.exe 5980
AutoIt3.exe 1820
iexplore.exe 1156
NOTEPAD.EXE 5816
iexplore.exe 4268

Run returns 1156 and indeed we find an iexplorer.exe with the PID 1156 in the list. Now I don't close the opened IE window and run the program a second time. Now the output is this:

Pid as returned by Run: 5200

[System Process] 0
System 4
SMSS.EXE 916
CSRSS.EXE 984
winlogon.exe 1016
SERVICES.EXE 1060
LSASS.EXE 1072
ATI2EVXX.EXE 1220
SVCHOST.EXE 1244
SVCHOST.EXE 1332
MsMpEng.exe 1376
SVCHOST.EXE 1536
EvtEng.exe 1592
ATI2EVXX.EXE 1656
S24EvMon.exe 1712
SVCHOST.EXE 1936
SVCHOST.EXE 204
SPOOLSV.EXE 760
SVCHOST.EXE 836
CLCapSvc.exe 924
CLMLServer.exe 1580
CLMLService.exe 1796
JQS.EXE 1868
PnkBstrA.exe 292
RegSrvc.exe 352
SVCHOST.EXE 588
SVCHOST.EXE 640
CLSched.exe 1792
EXPLORER>EXE 2796
alg.exe 2812
HControl.exe 3160
EHTRAY.EXE 3224
DMedia.exe 3248
ACMON.EXE 3256
EHMSAS.EXE 3296
wcourier.exe 3292
SynTPEnh.exe 3336
ACEngSvr.exe 3492
ZCfgSvc.exe 3668
ATKOSD.exe 3784
iFrmewrk.exe 3884
EOUWiz.exe 3940
RTHDCPL.EXE 4044
MSSECES.EXE 492
MOM.exe 624
gnotify.exe 1564
ctfmon.exe 2460
Dot1XCfg.exe 2124
ccc.exe 4056
GoogleUpdate.exe 2136
firefox.exe 196
SciTE.exe 5984
AutoIt3Help.exe 5980
iexplore.exe 1156
NOTEPAD.EXE 5816
iexplore.exe 4268
AutoIt3.exe 4252
NOTEPAD.EXE 5212
iexplore.exe 2496

Now Run returns 5200 as PID. But in the list there is no process that has 5200 as PID.iexplore.exe uses the PIDs 1156, 4268 (as in the first output) and for the last opened window it uses: 2496. Not 5200 weirdly enough... My conclusion is that the PID changes. Or that the process I open closes directly only to open another one directly after. I'm not very experienced with autoit or PIDs so I might be doing something wrong... ?

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

Are you running this from scite or compiled? and do you have a #RequireAdmin in your code?

If you are running from code, try it compiled just to whittle away what it is not.

I'm running this from scite and I do not have #RequireAdmin in my code. I use XP SP 3 and #RequireAdmin is only needed in Vista and higher right? (I'm logged in with adminrights btw.) I tried it compiled but it does the same thing...

Edited by MrPPP

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

@JohnOne - Scite will compile anyway it so it shouldn't make a different (unless the code is not saved).

@MrPPP - I am not convinced as you are there is something fishy going on.

If you had ordered by image name like I said you would have seen there are two iexplore processes in the first list.

iexplore.exe    1156
iexplore.exe    4268

This will mess with your code as your system needs to have NO running iexplore process running when it starts, read my first post for more info. You can use 'taskkill /F /IM iexplore.exe' to kill all instances from the cmdline or put it as apart of your script.

The second list there are three iexplore process running

iexplore.exe    1156
iexplore.exe    4268
iexplore.exe    2496

The PIDs did not change, the system was in a different state than you expected.

Now why Run returned a PID of 5200 I have no idea. I would make the following change to tighten things up

#include <Array.au3>

$sURL = "www.google.nl"
Local Const $pid = Run(@ProgramFilesDir & "\internet explorer\iexplore.exe " & $sURL, "", @SW_SHOW) ; ensures it cannot be change once set
ConsoleWrite($pid & @CRLF) ; print out to ensure your search is correct.
Run("notepad.exe") ; why is this where?
Sleep(1000)

Global $hOut = WinGetTitle("Naamloos")

ControlSend($hOut, "", "[CLASS:Edit; INSTANCE:1]", "Pid as returned by Run: " & $pid & "{ENTER}{ENTER}")

Local $aProcessList = ProcessList()

For $i = 1 To $aProcessList[0][0]
    ControlSend($hOut, "", "[CLASS:Edit; INSTANCE:1]", $aProcessList[$i][0] & " " & $aProcessList[$i][1] & "{ENTER}")
Next

If you have any questions just ask. There my be other issues going on that I have not seen however it is important have a known good state to eliminate possible causes.

Edited by bo8ster

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

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