Forum: Ruby Win32API- GetForegroundWindows()

Fb195186122a72e6eaaa722127151750?d=identicon&s=25 Diego Guti (protos)
on 2008-12-26 22:07
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 :)
Bfa670ef2303deb7dec5a8027367b30b?d=identicon&s=25 David Mullet (mully)
on 2008-12-27 00:36
Diego Guti 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
Fb195186122a72e6eaaa722127151750?d=identicon&s=25 Diego Guti (protos)
on 2008-12-27 11:10
> 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??
Bfa670ef2303deb7dec5a8027367b30b?d=identicon&s=25 David Mullet (mully)
on 2008-12-27 18:38
Diego Guti 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
Fb195186122a72e6eaaa722127151750?d=identicon&s=25 Diego Guti (protos)
on 2008-12-27 19:08
> 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
97334b75c93574ed3e514f33849f0953?d=identicon&s=25 Mario Ruiz (tcblues)
on 2011-03-22 18:42
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 Mullet wrote in post #763686:
> Diego Guti wrote:
>>> Try changing your last parameter from 'P' to 'N':
>>>
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2011-03-23 15:32
recommend ffi, too, for windows api stuff:

https://github.com/ffi/ffi/wiki/Windows-Examples

win32api scares me.
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (djberg96)
on 2011-03-23 16:29
(Received via mailing list)
On Mar 23, 8:32am, Roger Pack <rogerpack2...@gmail.com> wrote:
> recommend ffi, too, for windows api stuff:
>
> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.