Skip to content

Use the Toolhelp32 API to enumerate loaded Win32 modules.#892

Merged
grynspan merged 1 commit into
mainfrom
jgrynspan/use-toolhelp-to-enumerate-win32-modules
Jan 6, 2025
Merged

Use the Toolhelp32 API to enumerate loaded Win32 modules.#892
grynspan merged 1 commit into
mainfrom
jgrynspan/use-toolhelp-to-enumerate-win32-modules

Conversation

@grynspan

@grynspan grynspan commented Jan 6, 2025

Copy link
Copy Markdown
Contributor

This PR replaces our call to EnumProcessModules() with one to CreateToolhelp32Snapshot(TH32CS_SNAPMODULE). Three reasons:

  1. EnumProcessModules() requires us to specify a large, fixed-size buffer to contain all the HMODULE handles;
  2. EnumProcessModules() does not own any references to the handles it returns, meaning that a module can be unloaded while we are iterating over them (while CreateToolhelp32Snapshot() temporarily bumps the refcounts of the handles it produces); and
  3. CreateToolhelp32Snapshot() lets us produce a lazy sequence of HMODULE values rather than an array, letting us write somewhat Swiftier code that uses it.

The overhead of using CreateToolhelp32Snapshot() was negligible (below the noise level when measuring).

Checklist:

  • Code and documentation should follow the style of the Style Guide.
  • If public symbols are renamed or modified, DocC references should be updated.

This PR replaces our call to `EnumProcessModules()` with one to
`CreateToolhelp32Snapshot(TH32CS_SNAPMODULE)`. Three reasons:

1. `EnumProcessModules()` requires us to specify a large, fixed-size buffer to
   contain all the `HMODULE` handles;
2. `EnumProcessModules()` does not own any references to the handles it returns,
   meaning that a module can be unloaded while we are iterating over them (while
   `CreateToolhelp32Snapshot()` temporarily bumps the refcounts of the handles
   it produces); and
3. `CreateToolhelp32Snapshot()` lets us produce a lazy sequence of `HMODULE`
   values rather than an array, letting us write somewhat Swiftier code that
   uses it.

The overhead of using `CreateToolhelp32Snapshot()` was negligible (below the
noise level when measuring).
@grynspan grynspan added enhancement New feature or request windows 🪟 Windows support labels Jan 6, 2025
@grynspan grynspan added this to the Swift 6.x milestone Jan 6, 2025
@grynspan grynspan requested a review from compnerd January 6, 2025 20:28
@grynspan grynspan self-assigned this Jan 6, 2025
@grynspan

grynspan commented Jan 6, 2025

Copy link
Copy Markdown
Contributor Author

@swift-ci test

Comment thread Sources/Testing/Support/Additions/WinSDKAdditions.swift
Comment thread Sources/Testing/Support/Additions/WinSDKAdditions.swift
@grynspan grynspan merged commit 65b7ef2 into main Jan 6, 2025
@grynspan grynspan deleted the jgrynspan/use-toolhelp-to-enumerate-win32-modules branch January 6, 2025 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request windows 🪟 Windows support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants