How Can I Search And Get The Directory Of A Dll File In Python
Let's say if I have a dll file called banana.dll, and I have a module called banana.py which will use ctypes to load banana.dll, and they are stored in the same directory, for exma
Solution 1:
If you have pywin32 installed:
import _win32sysloader
mod = 'banana'
path_to_mod = _win32sysloader.GetModuleFilename(mod) or _win32sysloader.LoadModule(mod)
Or
importwin32apimod='banana'
path_to_mod = win32api.GetModuleFileName(win32api.LoadLibrary(mod))
If you don't have pywin32, you can use ctypes to access win32 api:
import ctypes
from ctypes.wintypes import HANDLE, LPWSTR, DWORD
GetModuleFileName = ctypes.windll.kernel32.GetModuleFileNameW
GetModuleFileName.argtypes = HANDLE, LPWSTR, DWORD
GetModuleFileName.restype = DWORD
mod = 'banana'
MAX_PATH = 260
dll = ctypes.CDLL(mod) or ctypes.WINDLL(mod)
buf = ctypes.create_unicode_buffer(MAX_PATH)
GetModuleFileName(dll._handle, buf, MAX_PATH)
path_to_mod = buf.value
Don't forget to handle WindowsError and other possible exceptions.
Solution 2:
Try:
import banana
import os.path
module_dirname = os.path.dirname(banana.__file__)
Post a Comment for "How Can I Search And Get The Directory Of A Dll File In Python"