I think I'm making progress. Window Titles seem to be of no use in this situation... since there are no MS commands that can differentiate one window from the next if they have the exact same name. Also, there are no MS commands I can use to pull the Window Handles if the titles are all the same (though there may be a VBScript method... and if so, please post).
So instead I'm going to look for all running processes where Process Name=Notepad.exe and try to pull the Process IDs and monitor those.
I started with some code Marcus posted here:
Get running process info and see if a process is running
Code: Select all
VBSTART
'returns the number of copies of ProcessName that are running
'will return 0 if ProcessName is not running
Function IsProcessRunning(ProcessName)
Set oWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = oWMIService.ExecQuery ("Select Name from Win32_Process where Name='" & ProcessName & "'")
IsProcessRunning = colProcessList.count
End Function
VBEND
VBEval>IsProcessRunning("opera.exe"),res
MessageModal>res
I have two Notepad windows running now, here's how they look from the MS "View System Windows" tool. The numbers are the Window Handles:
263324 - Notepad "Untitled - Notepad"
1180650 - Notepad "Untitled - Notepad"
In Task Manager, I can see these two Notepad.exe processes and their PID (Process ID) values below:
2504
3164
I made some modifications to the VBScript, here's what I have so far:
mods in red wrote:VBSTART
'returns the number of copies of ProcessName that are running
'will return 0 if ProcessName is not running
'also shows each unique PID
Function IsProcessRunning(ProcessName)
Set oWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = oWMIService.ExecQuery ("Select Name, Handle from Win32_Process where Name='" & ProcessName & "'")
For Each oProcess in colProcessList
MsgBox oProcess.Name & "," & oProcess.Handle
Next
IsProcessRunning = colProcessList.count
End Function
VBEND
VBEval>IsProcessRunning("notepad.exe"),res
MessageModal>res
When I ran the code, it found both Notepad windows and correctly told me what the PID values were via MsgBox commands inside the VBScript. I'd like to pass those numbers back for use in the MS code... but can't seem to do that.
Can anyone with VBScript knowledge show me how to do that? Marcus? Anyone?
The idea is, I could keep calling the VBScript function, passing in "Notepad.exe" and getting back a list of PID values. I could just keep checking those against a list. When a new value appears, increment the open count. When a known value from the list is no longer being seen, increment the closed count.
Thanks for all the help so far, getting close to something workable... but this would be so much easier with either of the following:
1) An improved GetWindowList> command that could optionally return a list of Window Title / Window Handle pairs... instead of just Window Titles.
2) New OnEvent>WINDOW_JUST_OPENED and OnEvent>WINDOW_JUST_CLOSED commands that would only fire once... when a window matching the specified title parameter was instantiated or destroyed. Thinking a bit more on it... when it fires... it would be great if it returned the Window Handle involved. That would certainly make things easier. Once we know the Window Handle, there are lot of commands we can use to do whatever we want with that particular window, monitor it, etc.