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.