Python Forum
using ctypes to use a dll in a python module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using ctypes to use a dll in a python module
#1
Hello, working on a windows system and I need to import a dll into a python module. I use ctypes
import ctypes,sys myLib = ctypes.CDLL("./myDLL.dll")

so far so good but then I want to use a function of the dll. From the .h file
I know there is a function with prototype DWORD ICNC_GetDLLVersion().
I want to call from python, I get
>>> myLib.ICNC_GetDLLVersion() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:/msys64/mingw64/lib/python3.10/ctypes/__init__.py", line 387, in __getattr__ func = self.__getitem__(name) File "C:/msys64/mingw64/lib/python3.10/ctypes/__init__.py", line 392, in __getitem__ func = self._FuncPtr((name_or_ordinal, self)) AttributeError: function 'ICNC_GetDLLVersion' not found
but I found
>>> myLib[1] <_FuncPtr object at 0x00000202951206c0> >>>
and the same for the many functions of the dll.
How can I find the name of the functions as it is in the .h file ?
Thank you
Reply
#2
What do you get if you do print(dir(myLib))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thakyou for your answer
Python 3.10.12 (main, Jun 14 2023, 19:14:29) [GCC 13.1.0 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. pymodbus accessible >>> import sys,ctypes >>> myLib = ctypes.CDLL("./ICNC2_VS.dll") >>> print(dir(myLib)) ['_FuncPtr', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', ' __getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__redu ce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_func_flags_', '_func_restype_', '_handle', '_n ame'] >>> type(myLib) <class 'ctypes.CDLL'>
I suspect that the problem comes from the fact that the dll has been compiled in C++, so the name of the function is decorated with the type of arguments an return value.
Reply
#4
I recompiled the dll adding extern "C" { } for each function in both the .h and .cpp files, and it solved the problem since the function names are not anymore decorated.
Gribouillis and buran like this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to debug segfault in ctypes binding to Fortran bthomas 1 1,883 Sep-01-2023, 11:26 AM
Last Post: bthomas
  ctypes juliolop 7 3,712 Apr-20-2023, 03:33 PM
Last Post: Larz60+
  Issue while using ctypes in python GiggsB 6 7,403 Mar-27-2022, 03:38 AM
Last Post: GiggsB
  Ctypes and libffi.so.7 luxedo 1 8,835 Oct-23-2021, 09:24 PM
Last Post: DeaD_EyE
  possible ctypes and numpy conflict? herbal_rage 0 4,571 Apr-16-2020, 11:35 AM
Last Post: herbal_rage
  python kernell crash with a ctypes program Jstechg 1 4,729 Nov-24-2018, 02:37 PM
Last Post: Jstechg
  generating ctypes wrapper for a c library? brighteningeyes 9 11,546 Nov-04-2018, 02:31 AM
Last Post: brighteningeyes
  DLL library with ctypes Max20 0 3,926 Aug-19-2018, 11:15 AM
Last Post: Max20
  dll not loading to Ctypes Philbot 1 9,446 Jul-01-2018, 09:55 AM
Last Post: Philbot
  Error Ctypes in Windows rramosg 4 12,491 Oct-17-2017, 05:26 AM
Last Post: rramosg

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.