DLL function call from Python

I want to call a function inside a DLL from Python. But I get this error:

"Attribute Error function not found" 

This is my code:

 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) 

But the DLL documentation shows the setDACValue function with ChannelId and DAC Voltage as inputs.

The above information is based on a piece of code available in StackOverflow.

I also tried a simple method to use cdll.LoadLibrary and then called a function, but this also gives the same error.

Can someone tell me what is wrong? Thanks.

Full trace:

 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

+6
source share
2 answers

When you specify a function prototype, you must specify not only the types of the arguments, but also the type of the return value, as the first argument to WINFUNCTYPE . Therefore line

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

should be replaced by

 CATSDllApiProto = ctypes.WINFUNCTYPE (ctypes.c_uint8, ctypes.c_uint8,ctypes.c_double) 
+2
source

Try it. You might need WinDLL , but CDLL more likely:

 from ctypes import * cats = CDLL('CATS') cats.setDACValue.argtypes = [c_uint8,c_double] cats.setDACValue.restype = c_uint8 cats.setDACValue(1,4) 

If it still cannot find setDACValue , the Microsoft dumpbin can be used to display the names of exported functions in a DLL:

 dumpbin /exports CATS.dll 

It comes with Visual Studio installation in the VC \ bin directory during installation.

0
source

Source: https://habr.com/ru/post/985785/


All Articles