Python 如何使用ctypes调用具有双下划线函数名的DLL函数?

Python 如何使用ctypes调用具有双下划线函数名的DLL函数?,python,dll,ctypes,private,name-mangling,Python,Dll,Ctypes,Private,Name Mangling,我试图使用Python3.8和ctypes模块调用DLL中的函数 DLL中的函数名是\uu apiJob()。请注意,此函数以双下划线开头 我想在自定义对象中调用它,如: class Job: def __init__(self,dll_path): self.windll = ctypes.WinDLL(dll_path) def execute(self): self.windll.__apiJob() a = Job('api64.dl

我试图使用Python3.8和
ctypes
模块调用DLL中的函数

DLL中的函数名是
\uu apiJob()
。请注意,此函数以双下划线开头

我想在自定义对象中调用它,如:

class Job:

    def __init__(self,dll_path):
        self.windll = ctypes.WinDLL(dll_path)

    def execute(self):
        self.windll.__apiJob()

a = Job('api64.dll')
a.execute()
但由于函数名以双下划线开头,而Python中的函数名为mangling,因此它将被视为私有方法。因此,运行此脚本时,
\uuuuApiJob
将重命名为
\uJob\uApiJob
,这将导致错误:
“\uJob\uApiJob”未找到


如何处理这种情况?

也可以使用以下语法调用该函数,并绕过Python应用于类实例的“dunder”属性的模糊处理:

self.windll['__apiJob']()
示例如下:

测试.cpp

extern“C”\u declspec(dllexport)
int _uapijob(){
返回123;
}
test.py

导入ctypes
班级工作:
定义初始化(自):
dll=ctypes.CDLL(“./test”)
self.apiJob=dll[''apiJob']#绕过“dunder”类名损坏
self.apiJob.argtypes=()
self.apiJob.restype=ctypes.c_int
def执行(自我):
return self.apiJob()
a=作业()
结果=a.execute()
打印(结果)
输出:

123
另一方面,
windle
用于DLL声明在32位DLL中使用
\uu stdcall
调用约定的函数
CDLL
用于默认的
\uu cdecl
调用约定。64位DLL只有一个调用约定,所以两者都可以,但为了便于移植,请记住这一点