Skip to content

Adopt wil/resource.h (wil::unique_*) for Win32 handles across the codebase #37

Description

@asklar

Goal

Adopt wil/resource.h RAII wrappers for raw Win32 handles across the codebase, replacing manual CloseHandle/FreeLibrary/DeleteObject/RegCloseKey/etc. This complements the COM-pointer cleanup (#10) — same "use WIL RAII" theme, handles instead of interfaces.

Do it as one PR once #23 (WinForms) and #24 (COM ptrs) have landed (so it also covers src/tap_winforms/winforms_tap_native.cpp and doesn't collide with #24's TAP edits).

Scope (audit: ~160 sites across ~14 files)

Map raw handles to the appropriate wil::unique_*:

  • Generic HANDLE (named pipes, events, files, CreateToolhelp32Snapshot) → wil::unique_handle / wil::unique_hfile / wil::unique_event
  • Process/thread handles (OpenProcess, CreateProcess PROCESS_INFORMATION) → wil::unique_process_handle / wil::unique_handle
  • HMODULE from LoadLibrarywil::unique_hmodule
  • Registry HKEYwil::unique_hkey
  • GDI objects in screenshot.cpp (HDC/HBITMAP/HFONT/HPEN/HBRUSH) → wil::unique_hdc / wil::unique_hbitmap / wil::unique_hgdiobj

Files (raw-handle site counts)

  • src/plugin_avalonia/lvt_avalonia_plugin.cpp (~22), avalonia_tap_native.cpp (~7)
  • src/providers/wpf_inject.cpp (~21), xaml_diag_common.cpp (~21), winui3_provider.cpp (~4), comctl_provider.cpp (~2)
  • src/plugin_chromium/chromium_host.cpp (~20), lvt_chromium_plugin.cpp (~19)
  • src/tap_wpf/wpf_tap_native.cpp (~12), src/tap/lvt_tap.cpp (~5), src/tap_winforms/winforms_tap_native.cpp (after Add WinForms provider (+ sample app) #23)
  • src/plugin_loader.cpp (~4), src/target.cpp (~2), src/framework_detector.cpp (~1)
  • src/screenshot.cpp (GDI objects)

Caveats

  • TAP DLLs (lvt_tap, wpf_tap_native, winforms_tap_native, avalonia_tap_native) use static CRT (/MT) and avoid C++ exceptions — use the non-throwing WIL forms there; do not introduce throwing helpers.
  • Watch for handles whose ownership is intentionally transferred (e.g. a named-pipe handle handed to a fire-and-forget worker thread, or handles closed on another thread) — don't wrap in a way that double-closes or closes early.
  • Behavior must be identical; this is a leak-hygiene/RAII refactor, not a functional change. Keep build\lvt_unit_tests.exe + integration tests green.

Relates to #10.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions