# Win32API- GetForegroundWindows()

**URL:** <https://www.ruby-forum.com/t/win32api-getforegroundwindows/156410>\
**Category:** Ruby\
**Created:** [December 26, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/win32api-getforegroundwindows/156410 "2008-12-26T22:07:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ogeidix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/d07c76/32.png) [@ogeidix](https://www.ruby-forum.com/u/ogeidix)\
**Post date:** [December 26, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/win32api-getforegroundwindows/156410/1 "2008-12-26T22:07:09Z")

</div>

Hi!

I’m playing with the API of Windows, but I haven’t found much  
information about that (almost all is for VB -.- )

So I’d like to know where can I search for it.  
Im trying to get de windows where the user is working, i have:

a = Win32API.new(‘user32’, ‘GetForegroundWindow’, [], ‘P’)  
a.Call()

But it doesnt work… The GetForegroundWindow function doesnt need  
params and it returns a handle of the windows, so i think it should  
run…

Thanks.

P.S.: Sorry, English is not my first language 🙂

---

<div class="post-metadata">

**Author:** ![ogeidix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/d07c76/32.png) [@ogeidix](https://www.ruby-forum.com/u/ogeidix)\
**Post date:** [December 27, 2008, 12:36am UTC](https://www.ruby-forum.com/t/win32api-getforegroundwindows/156410/2 "2008-12-27T00:36:02Z")

</div>

Diego G. wrote:

> Hi!
> 
> I’m playing with the API of Windows, but I haven’t found much  
> information about that (almost all is for VB -.- )
> 
> So I’d like to know where can I search for it.  
> Im trying to get de windows where the user is working, i have:
> 
> a = Win32API.new(‘user32’, ‘GetForegroundWindow’, [], ‘P’)  
> a.Call()
> 
> But it doesnt work… The GetForegroundWindow function doesnt need  
> params and it returns a handle of the windows, so i think it should  
> run…
> 
> Thanks.
> 
> P.S.: Sorry, English is not my first language 🙂

Try changing your last parameter from ‘P’ to ‘N’:

a = Win32API.new(‘user32’, ‘GetForegroundWindow’, [], ‘N’)  
window = a.Call()

David

---

<div class="post-metadata">

**Author:** ![ogeidix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/d07c76/32.png) [@ogeidix](https://www.ruby-forum.com/u/ogeidix)\
**Post date:** [December 27, 2008, 11:10am UTC](https://www.ruby-forum.com/t/win32api-getforegroundwindows/156410/3 "2008-12-27T11:10:27Z")

</div>

> Try changing your last parameter from ‘P’ to ‘N’:
> 
> a = Win32API.new(‘user32’, ‘GetForegroundWindow’, [], ‘N’)  
> window = a.Call()
> 
> David

Thanks, now it’s running :).

Just one more question. Now i got the handle of the window, any chance  
to get the name of the window??

---

<div class="post-metadata">

**Author:** ![ogeidix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/d07c76/32.png) [@ogeidix](https://www.ruby-forum.com/u/ogeidix)\
**Post date:** [December 27, 2008, 6:38pm UTC](https://www.ruby-forum.com/t/win32api-getforegroundwindows/156410/4 "2008-12-27T18:38:06Z")

</div>

Diego G. wrote:

> > Try changing your last parameter from ‘P’ to ‘N’:
> > 
> > a = Win32API.new(‘user32’, ‘GetForegroundWindow’, [], ‘N’)  
> > window = a.Call()
> > 
> > David
> 
> Thanks, now it’s running :).
> 
> Just one more question. Now i got the handle of the window, any chance  
> to get the name of the window??

You can use the GetWindowText API call. Pass it (1) your window handle,  
(2) a string buffer into which the text is to be copied, and (3) the  
maximum number of characters to copy to the buffer:

require ‘Win32API’

getForegroundWindow = Win32API.new(‘user32’, ‘GetForegroundWindow’, [],  
‘L’)  
getWindowText = Win32API.new(‘user32’, ‘GetWindowText’, [‘L’, ‘P’, ‘I’],  
‘I’)

window\_handle = getForegroundWindow.Call()  
title\_buffer = ’ ’ \* 256  
getWindowText.Call(window\_handle, title\_buffer, 256)  
puts(title\_buffer)

David

---

<div class="post-metadata">

**Author:** ![ogeidix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/d07c76/32.png) [@ogeidix](https://www.ruby-forum.com/u/ogeidix)\
**Post date:** [December 27, 2008, 7:08pm UTC](https://www.ruby-forum.com/t/win32api-getforegroundwindows/156410/5 "2008-12-27T19:08:50Z")

</div>

> You can use the GetWindowText API call. Pass it (1) your window handle,  
> (2) a string buffer into which the text is to be copied, and (3) the  
> maximum number of characters to copy to the buffer:
> 
> require ‘Win32API’
> 
> getForegroundWindow = Win32API.new(‘user32’, ‘GetForegroundWindow’, [],  
> ‘L’)  
> getWindowText = Win32API.new(‘user32’, ‘GetWindowText’, [‘L’, ‘P’, ‘I’],  
> ‘I’)
> 
> window\_handle = getForegroundWindow.Call()  
> title\_buffer = ’ ’ \* 256  
> getWindowText.Call(window\_handle, title\_buffer, 256)  
> puts(title\_buffer)
> 
> David

Ok. Thank you very much !! Now It’s perfect.

Regards

---

<div class="post-metadata">

**Author:** ![ogeidix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/d07c76/32.png) [@ogeidix](https://www.ruby-forum.com/u/ogeidix)\
**Post date:** [March 22, 2011, 6:42pm UTC](https://www.ruby-forum.com/t/win32api-getforegroundwindows/156410/6 "2011-03-22T18:42:02Z")

</div>

Thanks a lot for the code  
do you how is possible to get the text content of the browser when  
displaying a flash app?

David M. wrote in post #763686:

> Diego G. wrote:
> 
> > > Try changing your last parameter from ‘P’ to ‘N’:

---

<div class="post-metadata">

**Author:** ![ogeidix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/d07c76/32.png) [@ogeidix](https://www.ruby-forum.com/u/ogeidix)\
**Post date:** [March 23, 2011, 3:32pm UTC](https://www.ruby-forum.com/t/win32api-getforegroundwindows/156410/7 "2011-03-23T15:32:08Z")

</div>

recommend ffi, too, for windows api stuff:

> **[Windows Examples](https://github.com/ffi/ffi/wiki/Windows-Examples)**
>
> Ruby FFI. Contribute to ffi/ffi development by creating an account on GitHub.

win32api scares me.

---

<div class="post-metadata">

**Author:** ![ogeidix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/d07c76/32.png) [@ogeidix](https://www.ruby-forum.com/u/ogeidix)\
**Post date:** [March 23, 2011, 4:29pm UTC](https://www.ruby-forum.com/t/win32api-getforegroundwindows/156410/8 "2011-03-23T16:29:05Z")

</div>

On Mar 23, 8:32am, Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> recommend ffi, too, for windows api stuff:
> 
> [Windows Examples · ffi/ffi Wiki · GitHub](https://github.com/ffi/ffi/wiki/Windows-Examples)
> 
> win32api scares me.

Yeah, well, FFI scares me. It’s a PITA declaring structs, and it won’t  
build with MSVC++.

Dan
