/examples/alien/findwindow.lua

http://luapack.googlecode.com/ · Lua · 40 lines · 17 code · 10 blank · 13 comment · 0 complexity · 0cf3aa01dcd5cf9e74b1ed7fa1a6ee80 MD5 · raw file

  1. -- using Alien to find out what the caption title of SciTE is,
  2. -- and iterating over all top-level windows.
  3. -- Ensure that SciTE is running first!
  4. require 'alien'
  5. local user = alien.load 'user32.dll'
  6. -- these are the API calls needed. Note two NB things:
  7. -- (1) functions dealing with text ending with 'A' are for ASCII
  8. -- (2) need to specify abi to get proper __stdcall
  9. user.FindWindowA:types {"string","string",abi="stdcall"}
  10. user.GetWindowTextA:types {"int","string","int",abi="stdcall"}
  11. find = user.FindWindowA
  12. gettext = user.GetWindowTextA
  13. -- find the handle of the SciTE window using its class name
  14. hwnd = find("SciTEWindow",nil)
  15. -- and grab the text of that window (will be the caption)
  16. -- create a buffer and it will be filled!
  17. buf = alien.buffer(128)
  18. gettext(hwnd,buf,128)
  19. print(buf:tostring())
  20. -- Iterating over all top-level windows.
  21. -- again, note the abi for both EnumWindows and the callback! EnumWindows is
  22. -- expecting an _integer_ back from the callback, where 1 means 'true' means
  23. -- 'continue going'
  24. function each_hwnd (hwnd,p)
  25. print(hwnd)
  26. return 1
  27. end
  28. each_hwnd_callback = alien.callback(each_hwnd,{"int","pointer",abi="stdcall"})
  29. user.EnumWindows:types {"callback","pointer",abi="stdcall"}
  30. user.EnumWindows(each_hwnd_callback,nil)