winim icon indicating copy to clipboard operation
winim copied to clipboard

WNDENUMPROC usage for EnumWindows/EnumDesktopWindows

Open TTSKarlsson opened this issue 10 months ago • 1 comments

TL;DR; I'm trying to enumerate all windows with one of the user32 functions, and don't know how to provide a proper callback.

Description:

The code so far, and as you can see I'm new to Nim..

import winim/lean, std/sugar

proc wAppendWindow(hwnd: HWND, windows: var seq[HWND]): WINBOOL =
  windows.add(hwnd)
  return WINBOOL(true)

proc wGetVisibleWindows(): seq[HWND] =
  var windows = newSeq[HWND]()
  let desktop: HDESK = GetThreadDesktop(GetCurrentThreadId())
  let callback = WNDENUMPROC((h: HWND, lParam: LPARAM){.stdcall.}=>wAppendWindow(h, windows))
  EnumDesktopWindows(desktop, callback, 0)
  return windows

I'm trying to wrap my head around how to create a callback for EnumWindows and EnumDesktopWindows.

I got to this code through several iterations of tries, following Nim error messages. The last one was having to "make" it a {.stdcall.}, but disregarding that it doesn't really make sense, the lsp is happy and the compiler says I can't capture windows in the anonymous function.

If it's not clear what I try to achieve I can provide more information.

TTSKarlsson avatar Feb 17 '25 17:02 TTSKarlsson

I think the other project wAuto solves this and works as documentation: window.nim

TTSKarlsson avatar Feb 17 '25 17:02 TTSKarlsson