Ctypes throws "WindowsError: [Error 193] %1 is not a valid Win32 application", but it’s not a 32/64-bit issue

I have a problem loading a Windows DLL in Ctypes, which throws the error:

WindowsError: [Error 193] %1 is not a valid Win32 application

In my case, it’s a 32-bit DLL built with VS2012 on Windows 7 64-bit, and on my development machine I can load it fine. I checked that it’s 32-bit, using dumpbin /headers:

         14C machine (x86)

The problem occurs when I try to load the same DLL via Ctypes on a production VM, which is also Windows 7 64-bit. What I’m doing is:

from ctypes import *
self.dll = CDLL(dllabspath)

I get:

File "C:UsersuserDesktopWinPython-32bit-", line 365, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 193] %1 is not a valid Win32 application

From the other questions I’ve already tried several things.

  • This, this, this and this question suggests that my environment has to be the same, i.e. 32-bit Python, 32-bit DLL. This is the case on my development system and the VM I’m testing on. On both, I’m using WinPython 32-bit, latest version. It works on the dev machine, it fails on the VM.

  • Here, it’s related to g++ and a dependency on old Visual Studio runtimes. I compiled everything with VS2012 so I don’t think this is applicable here. There is a delayed-loaded dependency on a third party library that needs MSVCR80.dll, but it’s delayed-loaded and never called.

  • I’ve also installed the Visual C++ 32-bit Runtime on the target machine.

  • This suggests that the DLL needs to export a C interface, which it does.

  • There are no error popups of missing DLL dependencies.

The error is very generic, rather cryptic. Since it works on the dev machine in the same Python env, I’m assuming it has to do with some dependencies that only a Visual Studio installation can give me?

How can I troubleshoot this properly?

Source: dll

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.