Python:使用 ctypes 访问 DLL 函数——按函数 *name* 访问失败 [英] Python: accessing DLL function using ctypes -- access by function *name* fails

查看:33
本文介绍了Python:使用 ctypes 访问 DLL 函数——按函数 *name* 访问失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

myPythonClient(下面)想要调用一个 ringBell 函数(使用 ctypes 从 DLL 加载).但是,尝试通过 name 访问 ringBell 会导致 AttributeError.为什么?

myPythonClient (below) wants to invoke a ringBell function (loaded from a DLL using ctypes). However, attempting to access ringBell via its name results in an AttributeError. Why?

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.您是否检查过它是否与导出的名称完全一样?

Perhaps because the C++ name is mangled by the compiler and not exported from the DLL as RingBell. Have you checked that it appears in the exported names exactly like that?

这篇关于Python:使用 ctypes 访问 DLL 函数——按函数 *name* 访问失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆