# How to get xid on Windows?

**URL:** <https://www.ruby-forum.com/t/how-to-get-xid-on-windows/212370>\
**Category:** Ruby-Gnome 2\
**Created:** [October 1, 2011, 6:39am UTC](https://www.ruby-forum.com/t/how-to-get-xid-on-windows/212370 "2011-10-01T06:39:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [October 1, 2011, 6:39am UTC](https://www.ruby-forum.com/t/how-to-get-xid-on-windows/212370/1 "2011-10-01T06:39:33Z")

</div>

How to get xid on Windows?

Hi folks,

When I ran the following tiny snippet on my Windows 7,

require ‘gtk2’  
win = Gtk::Window.new  
win.signal\_connect(‘expose-event’) do  
p win.window.xid  
end  
win.show\_all  
Gtk.main

I got the error: undefined method `xid’ for #\<Gdk::Window:02735E38  
ptr=026D9170\>

But on Ubuntu on VirtualBox for Windows 7, it works well.  
(I got the number of xid.)

Could you show me how to get a xid on Windows?

Thanks,  
ashbb

---

<div class="post-metadata">

**Author:** ![satoshi\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@satoshi\_a](https://www.ruby-forum.com/u/satoshi_a)\
**Post date:** [October 1, 2011, 9:56pm UTC](https://www.ruby-forum.com/t/how-to-get-xid-on-windows/212370/2 "2011-10-01T21:56:24Z")

</div>

hi ash,

interesting - confirmed the same issue comparing Ubuntu and WinXP.

running this…

require ‘gtk2’  
win = Gtk::Window.new  
win.signal\_connect(‘expose-event’) do  
win.window.methods.sort.each{|m| p m}  
end  
win.show\_all  
Gtk.main

…shows that the #xid method exists in ubuntu, but not in windows.  
strange…

- j

---

<div class="post-metadata">

**Author:** ![satoshi\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@satoshi\_a](https://www.ruby-forum.com/u/satoshi_a)\
**Post date:** [October 2, 2011, 2:20pm UTC](https://www.ruby-forum.com/t/how-to-get-xid-on-windows/212370/3 "2011-10-02T14:20:50Z")

</div>

Satoshi A. wrote in post #1024450:

> How to get xid on Windows?
> 
> Hi folks,
> 
> When I ran the following tiny snippet on my Windows 7,
> 
> require ‘gtk2’  
> win = Gtk::Window.new  
> win.signal\_connect(‘expose-event’) do  
> p win.window.xid  
> end  
> win.show\_all  
> Gtk.main
> 
> I got the error: undefined method `xid’ for #\<Gdk:🪟02735E38  
> ptr=026D9170\>

[http://ruby-gnome2.sourceforge.jp/hiki.cgi?cmd=view&p=Gdk%3A%3ADrawable&key=xid#xid](http://ruby-gnome2.sourceforge.jp/hiki.cgi?cmd=view&p=Gdk%3A%3ADrawable&key=xid#xid)  
:  
Returns the X11 (X Window) XID of this Gdk::Drawable, as a number. _X  
only_

Notice the “X only”. Windows does not use X.

Simon

---

<div class="post-metadata">

**Author:** ![satoshi\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@satoshi\_a](https://www.ruby-forum.com/u/satoshi_a)\
**Post date:** [October 3, 2011, 12:23pm UTC](https://www.ruby-forum.com/t/how-to-get-xid-on-windows/212370/4 "2011-10-03T12:23:12Z")

</div>

@J,  
Thank you for the confirmation. 🙂

@Simon,  
I see. Thank you for the information.

@all,  
Now then, I have two more questions.

1st:  
“Windows does not use X.” … I see.  
But can I use xid on Windows or can’t?

I ran the following snippet on my Windows 7 and I got a xid for a video  
window created by playbin2 plugin.

require ‘gtk2’  
require ‘gst’  
play\_bin = Gst::ElementFactory.make(‘playbin2’)  
bus = play\_bin.bus  
bus.add\_watch do |bus, message|  
case message.type  
when Gst::Message::ELEMENT  
p message.structure[“xwindow-id”] #=\> Got a number, e.g. 18023540  
end  
true  
end  
play\_bin.uri = ‘file://d/tmp/input.mp4’  
window = Gtk::Window.new  
window.show\_all  
play\_bin.play  
Gtk.main

2nd: If the above first questions’s answer is “can’t”,  
are there any alternative ways to identify a window on Windows?  
A native window handler or somthing…

I’m now looking for the way to embed the video window in my Ruby/GTK2  
application.  
I found GstXOverlay[\*1]. So, I want to know how to identify a window.

ashbb

\*1:  
[http://gstreamer.freedesktop.org/data/doc/gstreamer/head/gst-plugins-base-libs/html/gst-plugins-base-libs-gstxoverlay.html#GstXOverlay](http://gstreamer.freedesktop.org/data/doc/gstreamer/head/gst-plugins-base-libs/html/gst-plugins-base-libs-gstxoverlay.html#GstXOverlay)

---

<div class="post-metadata">

**Author:** ![satoshi\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@satoshi\_a](https://www.ruby-forum.com/u/satoshi_a)\
**Post date:** [October 4, 2011, 2:10am UTC](https://www.ruby-forum.com/t/how-to-get-xid-on-windows/212370/5 "2011-10-04T02:10:08Z")

</div>

hi ashbb,

maybe this could help?

[FindWindowA function (winuser.h) - Win32 apps | Microsoft Learn](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx)

i don’t know if the handle returned by the FindWindow function relates  
in any way to the value returned by the GstStructure, but maybe?  
hopefully 😉

- j

---

<div class="post-metadata">

**Author:** ![satoshi\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@satoshi\_a](https://www.ruby-forum.com/u/satoshi_a)\
**Post date:** [October 3, 2011, 3:04pm UTC](https://www.ruby-forum.com/t/how-to-get-xid-on-windows/212370/6 "2011-10-03T15:04:17Z")

</div>

Satoshi A. wrote in post #1024748:

> 1st:  
> “Windows does not use X.” … I see.  
> But can I use xid on Windows or can’t?

You can’t. The source is :  
#ifdef GDK\_WINDOWING\_X11  
rb\_define\_method(gdkDrawable, “xid”, gdkdraw\_get\_xid, 0);  
#endif

And, as I said, windows does not use X.

> I ran the following snippet on my Windows 7 and I got a xid for a video  
> window created by playbin2 plugin.
> 
> require ‘gtk2’  
> require ‘gst’  
> play\_bin = Gst::ElementFactory.make(‘playbin2’)  
> bus = play\_bin.bus  
> bus.add\_watch do |bus, message|  
> case message.type  
> when Gst::Message::ELEMENT  
> p message.structure[“xwindow-id”] #=\> Got a number, e.g. 18023540  
> end  
> true  
> end  
> play\_bin.uri = ‘file://d/tmp/input.mp4’  
> window = Gtk::Window.new  
> window.show\_all  
> play\_bin.play  
> Gtk.main

You do not get a ‘xid’, you retrieve a value from a GstStructure, with  
the key ‘xwindow-id’. I don’t know how GStreamer fills this value.

> 2nd: If the above first questions’s answer is “can’t”,  
> are there any alternative ways to identify a window on Windows?  
> A native window handler or somthing…
> 
> I’m now looking for the way to embed the video window in my Ruby/GTK2  
> application.  
> I found GstXOverlay[\*1]. So, I want to know how to identify a window.

I think GstXOverlay is only for X too, but I don’t know much GStreamer.

This discussion [Gstreamer: linking playbin2 - Ruby-Gnome 2 - Ruby-Forum](http://www.ruby-forum.com/topic/1329197) talks about  
that, and it seems no solution was found.

I’m not sure you will be able to do what you want with ruby.

Simon

---

<div class="post-metadata">

**Author:** ![satoshi\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@satoshi\_a](https://www.ruby-forum.com/u/satoshi_a)\
**Post date:** [October 4, 2011, 1:22pm UTC](https://www.ruby-forum.com/t/how-to-get-xid-on-windows/212370/7 "2011-10-04T13:22:37Z")

</div>

Hi J, Simon at al,

> [Win32API- GetForegroundWindows() - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/174270)  
> Wow, bingo!

Try out the following. The video is embedded in my Ruby/GTK2 app window!

require ‘gtk2’  
require ‘gst’  
require ‘win32api’  
play\_bin = Gst::ElementFactory.make(‘playbin2’)  
play\_bin.uri = ‘file://d/tmp/input.mp4’  
play\_bin.video\_sink = Gst::ElementFactory.make(‘dshowvideosink’)  
window = Gtk::Window.new  
window.signal\_connect(‘expose-event’)do  
play\_bin.video\_sink.xwindow\_id = Win32API.new(‘user32’,  
‘GetForegroundWindow’, , ‘N’).call  
end  
window.show\_all  
play\_bin.play  
Gtk.main

Thanks,  
ashbb

---

<div class="post-metadata">

**Author:** ![satoshi\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@satoshi\_a](https://www.ruby-forum.com/u/satoshi_a)\
**Post date:** [October 4, 2011, 2:47am UTC](https://www.ruby-forum.com/t/how-to-get-xid-on-windows/212370/8 "2011-10-04T02:47:38Z")

</div>

and maybe these?

[Win32-api 1.0.0 - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/124778)

[ruby - Win32 - Can one Enumerate the windows belonging to the calling thread? - Stack Overflow](http://stackoverflow.com/questions/4548354/win32-can-one-enumerate-the-windows-belonging-to-the-calling-thread)

[Win32API- GetForegroundWindows() - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/174270)

again, i’m not sure that these are at all useful, but hopefully!

- j

---

<div class="post-metadata">

**Author:** ![satoshi\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@satoshi\_a](https://www.ruby-forum.com/u/satoshi_a)\
**Post date:** [October 4, 2011, 3:01pm UTC](https://www.ruby-forum.com/t/how-to-get-xid-on-windows/212370/9 "2011-10-04T15:01:29Z")

</div>

awesome! glad to hear it!

- j

---

<div class="post-metadata">

**Author:** ![satoshi\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@satoshi\_a](https://www.ruby-forum.com/u/satoshi_a)\
**Post date:** [October 11, 2011, 6:17am UTC](https://www.ruby-forum.com/t/how-to-get-xid-on-windows/212370/10 "2011-10-11T06:17:57Z")

</div>

Wow, very useful!

Shall I add this to the Wiki or is this too platform specific?

Figured that it could be helpful for windows users…
