6

I want to call a function inside a DLL from Python. But I'm getting 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 a function setDACValue with ChannelId & DAC Voltage as the inputs.

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

I've also tried the simple method of using cdll.LoadLibrary & then calling the function, but that also gives the same error.

Can anyone suggest me what is wrong? Thanks.

Full 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

2
  • Could you provide signature of function setDACValue? Commented Apr 23, 2015 at 13:44
  • Hi kvorobiev ..added the signature.. Commented Apr 23, 2015 at 13:56

2 Answers 2

2

When you specifiy prototype of function you should specify not only the types of arguments, but the return type as first arg to WINFUNCTYPE. Therefore, the line

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

should be replaced with

CATSDllApiProto = ctypes.WINFUNCTYPE (ctypes.c_uint8, ctypes.c_uint8,ctypes.c_double)
Sign up to request clarification or add additional context in comments.

6 Comments

Hi @kvorobiev thanks for your input...But even after adding that 'm getting the same error.
@user3561075 Try to use CFUNCTYPE instead of WINFUNCTYPE
@user3561075 Could you attach your DLL file, so that I was able to check it?
I could attach the DLL file but the problem is it is dependent on some additional file which is part of a SW installation. I can share that SW, DLL as well as the documentation if we have some file sharing option.
@user3561075 To begin with I only need DLL file. You can use any file-sharing services
|
0

Try this. You may need to use WinDLL, but CDLL is 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 can't find setDACValue, the Microsoft tool dumpbin can be used to list the exported functions names in the DLL:

dumpbin /exports CATS.dll

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

1 Comment

Hi @Mark Tolonen : I tried to to export the functions inside the DLL using DLL Export tool & PE Explorer. I'm not able to get the functions in neither of those tools.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.