# Detecting active windows process

**URL:** <https://www.ruby-forum.com/t/detecting-active-windows-process/121285>\
**Category:** Ruby\
**Created:** [November 26, 2007, 2:15pm UTC](https://www.ruby-forum.com/t/detecting-active-windows-process/121285 "2007-11-26T14:15:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dannyheathcote](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@dannyheathcote](https://www.ruby-forum.com/u/dannyheathcote)\
**Post date:** [November 26, 2007, 2:15pm UTC](https://www.ruby-forum.com/t/detecting-active-windows-process/121285/1 "2007-11-26T14:15:47Z")

</div>

Hi,

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

Thanks in advance

---

<div class="post-metadata">

**Author:** ![dannyheathcote](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@dannyheathcote](https://www.ruby-forum.com/u/dannyheathcote)\
**Post date:** [November 26, 2007, 2:45pm UTC](https://www.ruby-forum.com/t/detecting-active-windows-process/121285/2 "2007-11-26T14:45:40Z")

</div>

Danny H. wrote:

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

---

<div class="post-metadata">

**Author:** ![dannyheathcote](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@dannyheathcote](https://www.ruby-forum.com/u/dannyheathcote)\
**Post date:** [November 26, 2007, 3:30pm UTC](https://www.ruby-forum.com/t/detecting-active-windows-process/121285/3 "2007-11-26T15:30:52Z")

</div>

On 26 nov, 14:44, Jari W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Best regards,
> 
> Jari W.

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

---

<div class="post-metadata">

**Author:** ![dannyheathcote](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@dannyheathcote](https://www.ruby-forum.com/u/dannyheathcote)\
**Post date:** [November 26, 2007, 6:30pm UTC](https://www.ruby-forum.com/t/detecting-active-windows-process/121285/4 "2007-11-26T18:30:52Z")

</div>

On Nov 26, 6:10 am, Danny H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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
