Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up some of Windows code #4

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions dllist/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ def get_module_filename(hModule: HMODULE) -> Optional[str]:
_get_module_filename.restype = DWORD

nSize = 32768 # MAX_PATH
lpFilename = ctypes.create_unicode_buffer(nSize)
if _get_module_filename(hModule, lpFilename, nSize) != 0:
return lpFilename.value
else:
try:
lpFilename = ctypes.create_unicode_buffer(nSize)
if _get_module_filename(hModule, lpFilename, nSize) != 0:
return lpFilename.value
else:
warnings.warn(
f"Failed to get module file name for module {hModule}", stacklevel=2
)
except:
warnings.warn(
f"Failed to get module file name for module {hModule}", stacklevel=2
)
Expand Down Expand Up @@ -68,15 +73,19 @@ def get_process_module_handles_partial(

def get_process_module_handles() -> List[HMODULE]:
hProcess = get_current_process()
first_attempt = 1024
hModules, buffer_needed = get_process_module_handles_partial(
hProcess, maxbuffsize=1024
hProcess, maxbuffsize=first_attempt
)
if buffer_needed > 1024:
# retry with larger buffer
if buffer_needed > first_attempt:
# We need a bigger buffer, but luckily we know how big it needs to be
hModules, buffer_needed = get_process_module_handles_partial(
hProcess, maxbuffsize=buffer_needed
)
return hModules[:buffer_needed]

# skip first entry, which is the executable itself,
# and trim the list to the number of modules actually loaded
return hModules[1:buffer_needed]


def _platform_specific_dllist() -> List[str]:
Expand All @@ -86,8 +95,4 @@ def _platform_specific_dllist() -> List[str]:
name for hMod in hModules if (name := get_module_filename(hMod)) is not None
]

if libraries:
# remove the first entry, which is the executable itself
libraries.pop(0)

return libraries