'OSError: [WinError 193] %1 is not a valid Win32 application while reading custom DLL in python with CTypes
I am trying to write code that wraps a C library in python. I am planning on using CTypes to do it and I used visual studio to compile my DLL. I started with a simple function and I added the following in a header within Visual Studio that was then built to a DLL
int our_function(int num_numbers, int* numbers) {
int i;
int sum = 0;
for (i = 0; i < num_numbers; i++) {
sum += numbers[i];
}
return sum;
}
My python wrapper is the following
import ctypes
_sum = ctypes.CDLL(r"C:\Users\spl\Desktop\Ctypes Testing\Ctypes tester 2.dll")
_sum.our_function.argtypes = (ctypes.c_int, ctypes.POINTER(ctypes.c_int))
def our_function(numbers):
global _sum
num_numbers = len(numbers)
array_type = ctypes.c_int * num_numbers
result = _sum.our_function(ctypes.c_int(num_numbers), array_type(*numbers))
return int(result)
print(sum.our_function([1,2,-3,4,-5,6]))
When I execute the python code I get the following error
Traceback (most recent call last):
File "sum.py", line 3, in <module>
_sum = ctypes.CDLL(r"C:\Users\spl\Desktop\Ctypes Testing\Ctypes tester 2.dll")
File "C:\Users\spl\anaconda3\envs\Blank tester\lib\ctypes\__init__.py", line 364, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application
What causes this error and how do I fix it? I am using a 64 bit machine with windows 10 and my python build is 64 bit. I don't know much C, and the main goal is to get it working so that I can code everything in python.
Solution 1:[1]
This is a typical CPU architecture (32bit (your .dll) vs. 64bit (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 64bit 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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | CristiFati |