Detecting active windows process

D

Danny Hiemstra

Hi,

Is there a way to detect if a specified program e.g. firefox.exe is
active / on top?

Thanks in advance
 
J

Jari Williamsson

Danny said:
Is there a way to detect if a specified program e.g. firefox.exe is
active / on top?

One way is to use the GetForegroundWindow() Windows API. In case the
user is in a dialog window, you might need to step back through the
ownership chain to get the real main window. Then check the window class
name for the application's main window, often that gives away the
application.


Best regards,

Jari Williamsson
 
D

Danny Hiemstra

One way is to use the GetForegroundWindow() Windows API. In case the
user is in a dialog window, you might need to step back through the
ownership chain to get the real main window. Then check the window class
name for the application's main window, often that gives away the
application.

Best regards,

Jari Williamsson

Thank you very much, this seems to work:
Win32API.new("user32","GetForegroundWindow",[],"N").call

The only problem I have now is that the above example returns an
integer of a pointer / program id?
Now I have searched on google for an half an hour but I cannot find
out how to get the name of the process returned by the Win32API.

Thanks in advance
 
D

Daniel Berger

Hi,

Is there a way to detect if a specified program e.g. firefox.exe is
active / on top?

require 'sys/proctable'
require 'win32/api'
include Win32
include Sys

GetForegroundWindow = API.new('GetForegroundWindow', 'V', 'L',
'user32')
GetWindowThreadProcessId = API.new('GetWindowThreadProcessId', 'LP',
'L', 'user32')

pid = [0].pack('L')
hwnd = GetForegroundWindow.call
GetWindowThreadProcessId.call(hwnd, pid)
pid = pid.unpack('L')[0]

p ProcTable.ps(pid).comm

Regards,

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,270
Messages
2,571,353
Members
48,041
Latest member
Oliwen826

Latest Threads

Top