Python: simple ctypes dll load yields error

两盒软妹~` 提交于 2019-11-29 08:45:27

ctypes doesn't work with C++, which the MathFuncsDLL example is written in.

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

#ifdef __cplusplus
extern "C" {
#endif

__declspec(dllexport) double Add(double a, double b)
{
    return a + b;
}

#ifdef __cplusplus
}
#endif

Also note that the calling convention defaults to __cdecl, so use CDLL instead of WinDLL (which uses __stdcall calling convention):

>>> 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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!