python - Why do I need __declspec(dllexport) to make some functions accessible from ctypes? -
so learning make use of python ctypes module.
here simple c file compiled gcc -shared (version 4.8) on windows sample .dll:
#include <stdio.h> int addition(int a, int b){ return a+b; } i able access python this:
>>>from ctypes import * >>>mylibc = cdll(r"somepath\mysample.dll") >>>mylibc.addition <_funcptr object @ 0x00000000031456c8> >>>mylibc.addition(2,3) 5 now try same different, bigger , more complicated .c file contains function:
__declspec(dllexport) void __stdcall flopequity(hw hero[], hw villain[], double hcounters[], double vcounters[], double hsums[], double vsums[], ulong board, __int32 lenh, __int32 lenv) where hw typedef struct. compile gcc , can access function before when remove __declspec(dllexport) or _stdcall (or both) function no longer accessible.
question reason me being able access simple function first example me being unable access more complicated function. rules using calling conventions/_declspec when compiling c code , accessing ctypes ?
gcc seems export functions default, can use pe viewer pe explorer (view > export) view exported functions:

but, if try compile code vc++, won't export function you, you'll see there not exported function:

you need ask export function:
__declspec(dllexport) int addition(int a, int b){ return a+b; } as calling conventions, rule simple:
if function uses __stdcall, win32api, need import dll windll('mylib.dll') or windll.mylib, example:
> type mylib.c __declspec(dllexport) int __stdcall addition(int a, int b) { return a+b; } *********************************************************************** > cl mylib.c /link /dll /out:mylib.dll microsoft (r) 32-bit c/c++ optimizing compiler version 14.00.50727.762 80x86 copyright (c) microsoft corporation. rights reserved. mylib.c microsoft (r) incremental linker version 8.00.50727.762 copyright (c) microsoft corporation. rights reserved. /out:mylib.exe /dll /out:mylib.dll mylib.obj creating library mylib.lib , object mylib.exp *********************************************************************** > python >>> ctypes import * >>> >>> windll('mylib.dll').addition(1, 2) 3 >>> windll.mylib.addition(1, 2) 3 >>> if function uses __cdecl, witch default calling convention, need import dll cdll('mylib.dll') or cdll.mylib', example:
> type mylib.c // `__cdecl` not needed, since it's default calling convention __declspec(dllexport) int addition(int a, int b){ return a+b; } *********************************************************************** > cl mylib.c /link /dll /out:mylib.dll microsoft (r) 32-bit c/c++ optimizing compiler version 14.00.50727.762 80x86 copyright (c) microsoft corporation. rights reserved. mylib.c microsoft (r) incremental linker version 8.00.50727.762 copyright (c) microsoft corporation. rights reserved. /out:mylib.exe /dll /out:mylib.dll mylib.obj creating library mylib.lib , object mylib.exp *********************************************************************** > python >>> ctypes import * >>> >>> cdll('mylib.dll').addition(1, 2) 3 >>> cdll.mylib.addition(1, 2) 3 >>>
Comments
Post a Comment