If your window manager implements EWMH specification, you can also take a look at the _NET_CLIENT_LIST value of the root window. This is set by most modern window managers:
xprop -root|grep ^_NET_CLIENT_LIST
That value can easily be obtained programmatically, see your Xlib documentation!