myPythonClient
(abajo) quiere invocar una función ringBell
(cargada desde una DLL usando ctypes
). Sin embargo, intentar acceder al ringBell
a través de su nombre da como resultado un AttributeError
. ¿Por qué?Python: accediendo a la función DLL usando ctypes - acceso por función * nombre * falla
RingBell.h
contiene
namespace MyNamespace
{
class MyClass
{
public:
static __declspec(dllexport) int ringBell (void) ;
} ;
}
RingBell.cpp
contiene
#include <iostream>
#include "RingBell.h"
namespace MyNamespace
{
int __cdecl MyClass::ringBell (void)
{
std::cout << "\a" ;
return 0 ;
}
}
myPythonClient.py
contiene
from ctypes import *
cdll.RingBell[1]() # this invocation works fine
cdll.RingBell.ringBell() # however, this invocation errors out
# AttributeError: function 'ringBell' not found
Has acertado. Utilicé lo siguiente para descubrir el nombre "mutilado": link.exe/dump/exports RingBell.dll y descubrí que, en el archivo DLL, el nombre de la función era "? RingBell @ MyClass @ MyNamespace @@ SAHXZ ". ¡Gracias! –
También mencionaré que utilicé la función getattr de Python para obtener una referencia a la función ringBell: myRingBellFunction = getattr (cdll.RingBell, "? RingBell @ MyClass @ MyNamespace @@ SAHXZ") myRingBellFunction() # invoca la función otra vez –