Python:使用ctypes访问DLL函数-通过函数* name *访问失败


问题内容

myPythonClient(下图)想要调用一个ringBell函数(使用DLL从DLL中加载ctypes)。但是,尝试ringBell通过其
名称 访问将导致AttributeError。为什么?

RingBell.h 包含

namespace MyNamespace
    {
    class MyClass
        {
        public:
            static __declspec(dllexport) int ringBell ( void ) ;
        } ;
    }

RingBell.cpp 包含

#include <iostream>
#include "RingBell.h"
namespace MyNamespace
    {
    int __cdecl MyClass::ringBell ( void )
        {
        std::cout << "\a" ;
        return 0 ;
        }
    }

myPythonClient.py 包含

from ctypes import *
cdll.RingBell[1]() # this invocation works fine
cdll.RingBell.ringBell() # however, this invocation errors out
# AttributeError: function 'ringBell' not found

问题答案:

也许是因为C ++名称是由编译器破坏的,而不是从DLL导出为的RingBell。您是否检查过它在导出的名称中是否完全一样?