本文介绍了Python:使用ctypes访问DLL函数 - 通过function * name *访问失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
myPythonClient
(以下)想要调用 ringBell
函数(从DLL使用 ctypes的
)。但是,尝试通过其名称访问 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
包含
myPythonClient.py
contains
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函数 - 通过function * name *访问失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!