Building a DLL with MinGW and loading it with Python's ctypes module -
i trying build c dll can loaded within python using ctypes.windll.loadlibrary(...)
i can create dll , client program in c work following mingw tutorial @ http://www.mingw.org/wiki/msvc_and_mingw_dlls.
when try load same dll within python error:
oserror: [winerrror 193] %1 not valid win32 application
can give me idea doing incorrectly?
here files:
noise_dll.h
#ifndef noise_dll_h #define noise_dll_h // declspec identify functions exported when // building dll , imported when 'including' header client #ifdef building_noise_dll #define noise_dll __declspec(dllexport) #else #define noise_dll __declspec(dllimport) #endif //this test function see if dll working // __stdcall => use ctypes.windll ... int __stdcall noise_dll hello(const char *s); #endif // noise_dll_h
noise_dll.c
#include <stdio.h> #include "noise_dll.h" __stdcall int hello(const char *s) { printf("hello %s\n", s); return 0; }
i build dll with:
gcc -c -d building_noise_dll noise_dll.c gcc -shared -o noise_dll.dll noise_dll.o -wl,--out-implib,libnoise_dll.a
the python code simply:
import ctypes my_dll = ctypes.windll.loadlibrary("noise_dll")
and error above: '%1 not not valid win32 application'
i know dll not wrong, because if create client file:
noise_client.c
#include <stdio.h> #include "noise_dll.h" int main(void) { hello("dll"); return 0; }
and build with:
gcc -c noise_client.c gcc -o noise_client.exe noise_client.o -l. -lnoise_dll
i working executable. have understanding of of goes on in code, options , preprocessor directives above, still little fuzzy on how .dll file , .a file used. know if remove .a file can still build client, not sure purpose is. know is kind of archive format multiple object files
i can ctypes.windll.loadlibrary(...) ordinary windows dll found in windows/system32 without issue.
one final point: using 64 bit python 3.3. using version of mingw tat comes recommended installer (mingw-get-inst-20120426.exe). not sure if 32 bit, or if matters.
thanks!
use 32-bit python - dll wasn't compiled 64 bit code.
Comments
Post a Comment