Skip to content Skip to sidebar Skip to footer

Python Getmodulehandlew Oserror: [winerror 126] The Specified Module Could Not Be Found

The following code generates the error : OSError: [WinError 126] The specified module could not be found windll.kernel32.GetModuleHandleW.restype = wintypes.HMODULE windll.kernel3

Solution 1:

It should be windll.kernel32.GetModuleHandleW("kernel32.dll"). But please don't use windll when defining function prototypes because it's global to all modules that use ctypes, which leads to conflicting prototypes. Use kernel32 = WinDLL('kernel32', use_last_error=True). The DLL handle is kernel32._handle.

For your 2nd problem, first and foremost there's no reason to ever directly call GetProcAddress (or dlsym on POSIX systems) when using ctypes. But in principle it would be as follows:

kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)   
kernel32.GetProcAddress.restype = ctypes.c_void_p
kernel32.GetProcAddress.argtypes = (wintypes.HMODULE, wintypes.LPCSTR)

LoadLibAddy = kernel32.GetProcAddress(kernel32._handle, b'LoadLibraryA')
ifnot LoadLibAddy:
    raise ctypes.WinError(ctypes.get_last_error())

Note that the function name is passed as bytes. Anyway, you don't need this since kernel32.LoadLibraryA (or kernel32.LoadLibraryW) is a function pointer that you can pass as an argument or cast to c_void_p, etc.

Post a Comment for "Python Getmodulehandlew Oserror: [winerror 126] The Specified Module Could Not Be Found"