Jump to content
Sign in to follow this  
jftuga

Getting the win title and pid of active window

Recommended Posts

jftuga

I need to run an operation [the _ReduceMemory() function] on several groups of processes, such as IE, Outlook, and another 3rd party program. This function requires a PID.  However, I want to be able to skip the active window.

I can use WinGetTitle(["active"]) to see if the current active window matches my criteria.

I can use WinGetProcess() to get the PID, however there are always multiple instances of the 3rd party program running and this process only returns one PID, not all of them.

I can use WinList() to get an HWND, but don't know how to convert this to PID.

I want to run _ReduceMemory() on the non-active instances while skipping the active foreground process.

I can't seem to reconcile how to do this.  Any help would be greatly appreciated.

 

'?do=embed' frameborder='0' data-embedContent>>

Thanks,

-John

Share this post


Link to post
Share on other sites
KaFu

Local $var = WinList()
Local $s_PID_Buffer
For $i = 2 To $var[0][0]
If BitAND(WinGetState($var[$i][1]), 2) Then ; is visible
if not StringInStr($s_PID_Buffer,";" & WinGetProcess($var[$i][1]) & ";") then ; show PID only once
$s_PID_Buffer &= ";" & WinGetProcess($var[$i][1]) & ";"
ConsoleWrite($i & @TAB & "Title=" & $var[$i][0] & @LF & "Handle=" & $var[$i][1] & @CRLF & "PID: " & WinGetProcess($var[$i][1]) & @CRLF & @CRLF)
endif
EndIf
Next
"Is visible" and "Show PID only once" are of course optional.

Afaik Winlist() returns the windows in z-order, meaning the first one is the currently active one. So starting the enumeration with $i = 2 should skip the active window.

Edited by KaFu

Share this post


Link to post
Share on other sites
jftuga
Melba23

jftuga.

 

How can I then determine which of these is the active window?

As KaFu told you:

Afaik Winlist() returns the windows in z-order, meaning the first one is the currently active one. So starting the enumeration with $i = 2 should skip the active window.

which is also my experience. ;)

You could also get the active handle directly and so avoid it when looping through the list: :)

$hActive = WinGetHandle("[ACTIVE]")
M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______Open Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
jftuga

I did a sleep(2000) at the beginning and set $i=1, just to see what happened. 

In this 2 second period, I switched to IE.  The program returned:

 

i=8
Title=Start
Handle=0x0003001A
PID: 5800

i=25
Title=Remote Manager
Handle=0x00010164
PID: 568

i=43
Title=Getting the win title and pid of active window - General Help and Support - AutoIt Forums - Windows Internet Explorer
Handle=0x00040A1C
PID: 5852

i=45
Title=Windows Command Processor
Handle=0x00190886
PID: 4800

 

So I am not 100% sure z-order is returned for me on Win 7.

That being said,

$hActive = WinGetHandle("[ACTIVE]")

will be very helpful.  I think this is exactly what I needed.  Thanks M23!

Share this post


Link to post
Share on other sites
BrewManNH

Just an FYI, the _ReduceMemory function doesn't actually reduce the memory used by a program, it just takes what's in memory and swaps it out to the hard drive to the swap file. This just makes your programs run slower, doesn't really do much in the way of reducing memory used, and generally isn't a useful thing to do.

Let Windows manage the memory used, the developers of it probably have a much better idea of how to manage memory than the average user.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
jdelaney

to get the pid

#include <WinAPI.au3>
$iProcessID = ""
_WinAPI_GetWindowThreadProcessId(WinGetHandle("[ACTIVE]"), $iProcessID)
ConsoleWrite($iProcessID & @CRLF)

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

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  

×