1

I wa testing a small python program that called underlying DLL libs.

My system is 64bit windows 7. The python executable is the one shipped with 64 bit Anaconda3, kicked off inside Anaconda's virtual environment.

First thing came to my attention is that this python code

sys.platform

return win32 even for a AMD64 Arch, "platform.machine()" seemed returning a more accurate result.

Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64)] on win32

I did find some SO posts discussing this issue, such as this one.

and then the program failed when trying to load a DLL file:

windll.LoadLibrary(realpath(join(_module_location, 'dlh', 'win64', 'I_cant_tell_you_the_name.dll')))

Error message:

OSError: [WinError 193] %1 is not a valid Win32 application

Question is, why python can't load a 64 bit DLL in a 64 bit environment? it seemed to me the compiler insisted on loading a 32 bit DLL instead. Why is that? Or that the venv that Anaconda created support only 32 bit code?

Unfortunately I didn't have a 32 bit DLL for testing, otherwise I'd like to see how that went.

J.E.Y
  • 1,173
  • 2
  • 15
  • 37
  • Run `python -c "import struct; print(struct.calcsize('P') * 8)"` to check if your python is 32- or 64-bit. If it's 32, uninstall it and install 64-bit version. – phd May 22 '19 at 19:23
  • There is no compiler invoked here. You're calling the loader via WINAPI `LoadLibraryW`. Clearly you're running 64-bit Python. So the only possible explanations are that the target DLL is either 32-bit or the file is corrupt. – Eryk Sun May 23 '19 at 00:42
  • You can save yourself some keystrokes by using `ctypes.WinDLL(path)` instead of `ctypes.windll.LoadLibrary(path)`. It's better even because it allows passing `use_last_error=True` and `use_errno=True` if the library uses either the WINAPI last error or C `errno`, which enable the use of `ctypes.get_last_error()` and `ctypes.get_errno()`. – Eryk Sun May 23 '19 at 00:46

1 Answers1

0

You can use Dependency Walker to check what's happening. It has a profiling feature that will trace all library loads.

A few possible issues it can show you:

  1. Python is 32-bit because the wrong version was executed
  2. The DLL is 32-bit somehow
  3. The DLL depends on other DLLs that are not in the path
kichik
  • 33,220
  • 7
  • 94
  • 114