Python: simple ctypes dll load yields error

前端 未结 1 1889
渐次进展
渐次进展 2020-12-18 14:33

I created the MathFuncsDll.dll from MSDN DLL example and running the calling .cpp worked fine. Now, trying to load this in IPython with ctypes like

import ct         


        
相关标签:
1条回答
  • 2020-12-18 15:11

    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
    
    0 讨论(0)
提交回复
热议问题