OSError: [WinError 193] %1 is not a valid Win32 application while reading custom DLL in python with CTypes
This is a typical CPU architecture (032bit (your .dll) vs. 064bit (Python process that tries to load it)) mismatch. Check [SO]: Python Ctypes - loading dll throws OSError: [WinError 193] %1 is not a valid Win32 application (@CristiFati's answer) for more details.
Build the 064bit (pc064) version of your your .dll.
You can use the command line tools from the aforementioned URL, or you can set the VStudio IDE to do it, as explained in [MS.Docs]: How to: Configure Visual Studio C++ projects to Target 64-Bit, x64 Platforms:
- Open the C++ project that you want to configure.
- Open the property pages for that project. For more information, see Set C++ compiler and build properties in Visual Studio.
- Choose the Configuration Manager button to open the Configuration Manager dialog box.
- In the Active Solution Platform drop-down list, select the <New...> option to open the New Solution Platform dialog box.
- In the Type or select the new platform drop-down list, select a 64-bit target platform.
- Choose the OK button. The platform that you selected in the preceding step appears under Active Solution Platform in the Configuration Manager dialog box.
- Choose the Close button in the Configuration Manager dialog box, and then choose the OK button in the <Projectname> Property Pages dialog box.
OSError: [WinError 193] %1 is not a valid Win32 application - python
My problem was with the compilation code
im used gcc -shared -o a.dll main.c instead of gcc -c -fPIC main.c -o a.dll
Error loading DLL in python, not a valid win32 application
As the comments suggest, it could be an architecture problem.
If you're using a 32bit DLL with 64bit Python, or vice-versa, then you'll probably get errors.
Since I've had your error before, I recommend trying to load your DLL with 32bit Python.
One way to test if a *.dll-file is 32bit or 64bit, is to use dumpbin.exe
, e.g.
dumpbin /headers dsusb.dll
...
FILE HEADER VALUES
14C machine (x86)
...
machine (x86)
means 32bit, machine (x64)
means 64bit.
OSError: [WinError 193] %1 is not a valid Win32 application
The error is pretty clear. The file hello.py
is not an executable file. You need to specify the executable:
subprocess.call(['python.exe', 'hello.py', 'htmlfilename.htm'])
You'll need python.exe
to be visible on the search path, or you could pass the full path to the executable file that is running the calling script:
import sys
subprocess.call([sys.executable, 'hello.py', 'htmlfilename.htm'])
Ctypes throws WindowsError: [Error 193] %1 is not a valid Win32 application, but it's not a 32/64-bit issue
@eryksun commented:
Run under a debugger such as
cdb
orwindbg
. Callwindll.kernel32.DebugBreak()
just before callingCDLL(dllabspath)
. Set a breakpoint onkernelbase!LoadLibraryExW
and resume the thread viag
. When it breaks back into the debugger enterpt
to execute up to the function return. Then enter!teb
to check theLastStatusValue
for the thread. This NT status value may be of more help.
Further:
If you prefer to keep the system as clean as possible, try the following:
windll.kernelbase.LoadLibraryExW(c_wchar_p(dllabspath), None, 0); status = windll.ntdll.RtlGetLastNtStatus().
Otherwise it requires installing the debugging tools from the SDK. Symbols can be downloaded on demand from Microsoft's symbol server by setting the environment variable
_NT_SYMBOL_PATH=symsrv*symsrv.dll*C:\Symbols*http://msdl.microsoft.com/download/symbols
, which caches symbols inC:\Symbols
When debugging, this may help:
There are several status codes that produce Win32 error
ERROR_BAD_EXE_FORMAT
(193). In your case it'sSTATUS_INVALID_IMAGE_FORMAT
(0xC000007B
). Possibly in the production VM one of the dependent DLLs that it tries to load is 64-bit. At the breakpoint enterdu poi(@esp+4)
to print the first argument, which is the unicode path of the DLL it's attempting to load. Also check the stack trace viakc
.
Using this hint, I found a dependency on a 64-Bit WinPCAP DLL. Going through everything with DependencyWalker, it looked the same on both machines, complaining about a 64-Bit dependency, but apparently on the fresh machine, the DLL load path was different and it could never find the 32-Bit version.
Related Topics
How to Access the Query String in Flask Routes
Paramiko Error When Trying to Edit File: "Sudo: No Tty Present and No Askpass Program Specified"
Socketserver.Threadingtcpserver - Cannot Bind to Address After Program Restart
Why Are Scripting Languages (E.G. Perl, Python, and Ruby) Not Suitable as Shell Languages
"Getaddrinfo Failed", What Does That Mean
Python's Most Efficient Way to Choose Longest String in List
Threading in a Pyqt Application: Use Qt Threads or Python Threads
How to Exit Linux Terminal Using Python Script
Python in Raw Mode Stdin Print Adds Spaces
Which Key/Value Store Is the Most Promising/Stable
How to Dereference Variable Id'S
How to Activate an Anaconda Environment