0

I'm using python 3.10 64 bits and I want to load a 64 bits dll library using ctypes.

import os.path
import ctypes

dll_name = "USBLib.dll"
dllabspath = os.path.dirname(os.path.abspath(__file__)) + os.path.sep + dll_name
myDll = ctypes.CDLL(dllabspath)

However, with this code I get the following error: FileNotFoundError: Could not find module 'c:\Users\DELL\Documents\python\USBlib\USBLib.dll' (or one of its dependencies). Try using the full path with constructor syntax..

Even when I use os.add_dll_directory(os.path.dirname(os.path.abspath(__file__))), it does not change.

What is strange is that I also have the 32 bits version of the library and ctypes is able to find it. I just get the error [WinError 193] %1 is not a valid Win32 application which is normal.

What is wrong in the way I am loading the library ?

louisld
  • 323
  • 2
  • 15
  • 1
    “Or one of its dependencies“. It’s loading the main DLL. – Mark Tolonen Jul 19 '22 at 15:49
  • Yes you are right. I was missing a microsoft c++ dll. – louisld Jul 19 '22 at 16:09
  • Do you know if which dll is missing can be displayed ? In case I have the same problem again. – louisld Jul 19 '22 at 16:10
  • 1
    Tools like dumpbin and dependency walker can list the DLL dependencies, but not which one is missing. Process Monitor (see [this answer](https://stackoverflow.com/a/18650202/235698)) can help figure out which one is not found. – Mark Tolonen Jul 19 '22 at 16:46
  • The *3bit* part is not strange at all. When attempting to load the *.dll*, it figures out it can't, so it doesn't even search for dependencies. For more details, might want to check https://stackoverflow.com/questions/57187566/python-ctypes-loading-dll-throws-oserror-winerror-193-1-is-not-a-valid-win. – CristiFati Jul 21 '22 at 13:55

0 Answers0