Created
December 15, 2009 02:50
-
-
Save neetpgr/256660 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ffi' | |
module FFI::Library | |
def callback(*args) | |
raise ArgumentError, "wrong number of arguments" if args.length < 2 || args.length > 3 | |
name, params, ret = if args.length == 3 | |
args | |
else | |
[ nil, args[0], args[1] ] | |
end | |
options = Hash.new | |
options[:convention] = defined?(@ffi_convention) ? @ffi_convention : :default | |
options[:enums] = @ffi_enums if defined?(@ffi_enums) | |
cb = FFI::CallbackInfo.new(find_type(ret), params.map { |e| find_type(e) }, options) | |
# Add to the symbol -> type map (unless there was no name) | |
unless name.nil? | |
@ffi_callbacks = Hash.new unless defined?(@ffi_callbacks) | |
@ffi_callbacks[name] = cb | |
end | |
cb | |
end | |
end | |
module Win | |
extend FFI::Library | |
ffi_lib 'user32' | |
ffi_convention :stdcall | |
# BOOL CALLBACK EnumWindowProc(HWND hwnd, LPARAM lParam) | |
callback :enum_callback, [ :pointer, :long ], :bool | |
# BOOL WINAPI EnumDesktopWindows(HDESK hDesktop, WNDENUMPROC lpfn, LPARAM lParam) | |
attach_function :enum_desktop_windows, :EnumDesktopWindows, | |
[ :pointer, :enum_callback, :long ], :bool | |
# int GetWindowTextA(HWND hWnd, LPTSTR lpString, int nMaxCount) | |
attach_function :get_window_text, :GetWindowTextA, | |
[ :pointer, :pointer, :int ], :int | |
end | |
win_count = 0 | |
title = FFI::MemoryPointer.new :char, 512 | |
Win::EnumWindowCallback = Proc.new do |wnd, param| | |
title.clear | |
Win.get_window_text(wnd, title, title.size) | |
puts "[%03i] Found '%s'" % [ win_count += 1, title.get_string(0) ] | |
true | |
end | |
if not Win.enum_desktop_windows(nil, Win::EnumWindowCallback, 0) | |
puts 'Unable to enumerate current desktop\'s top-level windows' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment