Python: just loading dll ctypes gives error

I created MathFuncsDll.dll from an example MSDN DLL , and the working caller .cpp worked fine. Now, trying to load this into IPython using ctypes, for example

import ctypes lib = ctypes.WinDLL('MathFuncsDll.dll') 

located in the correct folder, gives

 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 28: ordinal not in range(128) 

Similarly in the Python shell, this gives

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

What should i change? Hmm, could it be Win 7 64bit vs 32 bit DLL or something like that? I will check back later when I have time again.

+2
python dll ctypes windowserror
source share
1 answer

ctypes does not work with C ++, which is an example of MathFuncsDLL.

Instead, write in C, or at least export the "C" interface:

 #ifdef __cplusplus extern "C" { #endif __declspec(dllexport) double Add(double a, double b) { return a + b; } #ifdef __cplusplus } #endif 

Also note that __cdecl used by default for the calling convention, so use CDLL instead of WinDLL (which uses the __stdcall calling __stdcall ):

 >>> import ctypes >>> dll=ctypes.CDLL('server') >>> dll.Add.restype = ctypes.c_double >>> dll.Add.argtypes = [ctypes.c_double,ctypes.c_double] >>> dll.Add(1.5,2.7) 4.2 
+3
source share

All Articles