Skip to content

Instantly share code, notes, and snippets.

@WindfallLabs
Last active November 20, 2015 22:01
Show Gist options
  • Save WindfallLabs/9039fcfec3ff0ebc3cf4 to your computer and use it in GitHub Desktop.
Save WindfallLabs/9039fcfec3ff0ebc3cf4 to your computer and use it in GitHub Desktop.
This script serves as a double-click DLL dependency-diagnostics utility.
# -*- coding: utf-8 -*-
"""
hellfire.py
This script serves as a double-click DLL dependency-diagnostics utility.
Usage:
1. Place this script in a folder containing one or many DLL(s) to test
2. Double-click this script
3. Read the Windows error-popup
The MIT License (MIT)
Copyright (c) 2015 Garin Wally / Windfall Spatial
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import os
import sys
import ctypes
import _ctypes
from time import sleep
# Ensure the current directory is set as the working directory
wd = os.path.dirname(__file__)
os.chdir(wd)
# Make other DLLs on PATH inaccessible by setting Python's paths only to the wd
# THIS IS TEMPORARY WITHIN THE PYTHON SESSION.
os.environ["PATH"] = wd
sys.path = [wd]
print("PATHs Used:")
print("os.environ['PATH']: {}".format(os.environ["PATH"]))
print("sys.path: {}".format(sys.path))
print("#" + "="*78)
print("\n")
# Container for loaded DLLs
dll_dict = {}
# Set of DLL files in wd
all_dlls = set([d for d in os.listdir(wd) if d.endswith('.dll')])
# Load the DLLs
for f in all_dlls:
try:
# Allow script to run multiple times in a session and not over-load DLLs
if f not in dll_dict.keys():
# Load DLLs with ctypes.WinDLL
dll_dict[f] = ctypes.WinDLL(os.path.join(wd, f))
print("Loaded: {}".format(f))
except:
# A Windows popup will show one (sub)dependency at a time
# <Code here won't execute until the popup message has closed>
print("Failed: {}".format(f))
# Make a set of DLL file names
loaded = set(dll_dict.keys())
# Unload/free DLLs
for k in dll_dict.keys():
try:
print("Unloading : {}".format(k))
_ctypes.FreeLibrary(dll_dict[k]._handle)
print("Unloaded.")
except:
print("Unload Failed: {}".format(k))
sleep(.5)
# Delete the dictionary
#del dll_dict
# Compare loaded DLLs to all DLLs (if the difference is an empty set, all were
# loaded)
difference = all_dlls.symmetric_difference(loaded)
# Show messages
if difference != set():
print("\nDLLs failed to load: \n{}".format(difference))
elif difference == set():
print("\nAll DLLs loaded.")
# Hang until user presses <Enter>
raw_input("\nPress <Enter> to quit")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment