2015-04-23 39 views
6

Voglio chiamare una funzione all'interno di una DLL da Python. Ma sto ottenendo questo errore:Chiamare una funzione DLL da Python

"Attribute Error function not found" 

Questo è il mio codice:

import os 
import ctypes 


os.chdir("C:\\Program Files\\Compact Automated Testing System V2.0")  

# Load DLL into memory. 
CATSDll = ctypes.WinDLL ("CATS.dll") 

# Set up prototype and parameters for the desired function call. 
CATSDllApiProto = ctypes.WINFUNCTYPE (ctypes.c_uint8,ctypes.c_double) 

CATSDllApiParams = (1, "p1", 0), (1, "p2", 0), 

# Actually map the call (setDACValue) to a Python name. 
CATSDllApi = CATSDllApiProto (("setDACValue", CATSDll), CATSDllApiParams) 

# Set up the variables and call the Python name with them. 
p1 = ctypes.c_uint8 (1) 
p2 = ctypes.c_double (4) 

CATSDllApi(p1,p2) 

Ma la documentazione DLL mostra una funzione setDACValue con ChannelId & DAC Voltage come gli ingressi.

Quanto sopra si basa su un pezzo di codice disponibile da StackOverflow.

Ho anche provato il metodo semplice di usare cdll.LoadLibrary & quindi chiamando la funzione, ma anche questo dà lo stesso errore.

Qualcuno può suggerirmi cosa c'è che non va? Grazie.

completa Traceback:

Traceback (most recent call last): 
    File "C:\Users\AEC_FULL\Softwares\eclipse\plugins\org.python.pydev_3.9.2.201502050007\pysrc\pydevd.py", line 2235, in <module> 
    globals = debugger.run(setup['file'], None, None) 
    File "C:\Users\AEC_FULL\Softwares\eclipse\plugins\org.python.pydev_3.9.2.201502050007\pysrc\pydevd.py", line 1661, in run 
    pydev_imports.execfile(file, globals, locals) # execute the script 
    File "C:\Users\AEC_FULL\Saravanan\Workspace\CATS\CATS.py", line 18, in <module> 
    CATSDllApi = CATSDllApiProto (("setDACValue", CATSDll), CATSDllApiParams) 
AttributeError: function 'setDACValue' not found 

Signature of the setDACValue

+0

Potrebbe fornire la firma della funzione 'setDACValue'? – kvorobiev

+0

Ciao kvorobiev ..aggiunto la firma .. – user3561075

risposta

2

Quando si specifiy prototipo della funzione è necessario specificare non solo i tipi di argomenti, ma il tipo di ritorno come primo arg a WINFUNCTYPE. Pertanto, la linea

CATSDllApiProto = ctypes.WINFUNCTYPE (ctypes.c_uint8,ctypes.c_double) 

dovrebbe essere sostituito con

CATSDllApiProto = ctypes.WINFUNCTYPE (ctypes.c_uint8, ctypes.c_uint8,ctypes.c_double) 
+0

Ciao @kvorobiev grazie per il tuo contributo ... Ma anche dopo aver aggiunto che sto ottenendo lo stesso errore. – user3561075

+0

@ user3561075 Provare ad usare 'CFUNCTYPE' invece di' WINFUNCTYPE' – kvorobiev

+0

Nope.Still non funziona. – user3561075

0

Prova questo. Potrebbe essere necessario utilizzare WinDLL, ma CDLL è più probabile:

from ctypes import * 

cats = CDLL('CATS') 
cats.setDACValue.argtypes = [c_uint8,c_double] 
cats.setDACValue.restype = c_uint8 

cats.setDACValue(1,4) 

Se ancora non riesce a trovare setDACValue, lo strumento Microsoft dumpbin può essere utilizzato per elencare i nomi funzioni esportate nella DLL:

dumpbin /exports CATS.dll 

Viene fornito con installazioni di Visual Studio nella directory VC \ bin sotto l'installazione.

+0

Ciao @ Mark Tolonen: Ho provato a esportare le funzioni all'interno della DLL usando lo strumento di esportazione DLL e PE Explorer. Non sono in grado di ottenere le funzioni in nessuno di questi strumenti. – user3561075

Problemi correlati