[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Detecting active windows process

Danny Hiemstra

11/26/2007 1:10:00 PM

Hi,

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

Thanks in advance
3 Answers

Jari Williamsson

11/26/2007 1:45:00 PM

0

Danny Hiemstra 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 Williamsson

Danny Hiemstra

11/26/2007 2:26:00 PM

0

On 26 nov, 14:44, Jari Williamsson
<jari.williams...@mailbox.swipnet.se> wrote:
> Danny Hiemstra 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 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

Daniel Berger

11/26/2007 5:27:00 PM

0

On Nov 26, 6:10 am, Danny Hiemstra <dannyhiems...@gmail.com> 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