Jump to content

Control windows by using ProcessList turning the PID into a Handle?


Hyflex
 Share

Recommended Posts

Hey Everyone,

I can't quite get this working, basically I have 20 "Program.exe" processes, I want to cycle through each process and do things to it for example I've done WinActivate and WinSetState... 

I thought that I would have to get the handle in order to do this but for some reason the handle fails on most of the processes...
 

Func Wins()
    $aProcessList = ProcessList("Program.exe")
    $vProcessorCount = 0
    For $x = 1 To $aProcessList[0][0]
        $hWnd = WinGetHandle($aProcessList[$x][1])
        WinActivate($hWnd) ; Just an example
        WinSetState($hWnd, "", @SW_MINIMIZE) ; Just an example
    Next
EndFunc

If I print all of the handles I get the following:

0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000E80 ; Why do only this one and the one below have handles...
0x00000DE8
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000
0x00000000

Any ideas what im doing wrong?

 

Link to comment
Share on other sites

Maybe _WinAPI_EnumProcessWindows() will help you to get the windows handle from the pid.

 

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Ran into an error with handling arrays :/
 

If $hWnd[0][0] >= 2 Then
    While $hWnd[1][2] = "SplashWnd"
        Sleep(1000)
    WEnd
EndIf

Error = Array variable has incorrect number of subscripts or subscript dimension range exceeded.

I've tried using if @error but can't seem to get round it...

Link to comment
Share on other sites

 

Ran into an error with handling arrays :/

 

If $hWnd[0][0] >= 2 Then
    While $hWnd[1][2] = "SplashWnd"
        Sleep(1000)
    WEnd
EndIf

Error = Array variable has incorrect number of subscripts or subscript dimension range exceeded.

I've tried using if @error but can't seem to get round it...

 

Success:    The 2D array of the handles to the window and class for the specified process.
[0][0] - Number of rows in array (n)
[0][1] - Unused
[n][0] - Window handle
[n][1] - Window class name

$hWnd[1][2] is not a valid array value. ;)

you would do $hWnd[1][0] for the handle and $hWnd[1][1] for the class.

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

Maybe this is helpful:

#include <Array.au3>

Global $aProcessList = ProcessList("program.exe")
_ArrayDisplay($aProcessList, "Processes", -1, 0, "", "|", "index|process names|process ids")

Global $aWinTitlesAndHandles

For $i = 1 To $aProcessList[0][0]
    $aWinTitlesAndHandles = _PID2WinTitleAndHandle($aProcessList[$i][1])
    _ArrayDisplay($aWinTitlesAndHandles, "Titles and handles", -1, 0, "", "|", "index|window titles|window handles")
Next


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

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.

Link to comment
Share on other sites

Maybe _WinAPI_EnumProcessWindows() will help you to get the windows handle from the pid.

 

Br,

UEZ

Would have been my recommendation too.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Success:    The 2D array of the handles to the window and class for the specified process.
[0][0] - Number of rows in array (n)
[0][1] - Unused
[n][0] - Window handle
[n][1] - Window class name

$hWnd[1][2] is not a valid array value. ;)

you would do $hWnd[1][0] for the handle and $hWnd[1][1] for the class.

Not true, the array can become 4 long because in some of my windows $hWnd[1][2] gives me SplashWnd

Link to comment
Share on other sites

Not true, the array can become 4 long because in some of my windows $hWnd[1][2] gives me SplashWnd

 

True but if there is nothing in [1][2] it will error out and say Array variable has incorrect number of subscripts or subscript dimension range exceeded.

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

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