![]() |
using ctypes to use a dll in a python module - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: using ctypes to use a dll in a python module (/thread-41714.html) |
using ctypes to use a dll in a python module - dauriac - Mar-05-2024 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 foundbut 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 RE: using ctypes to use a dll in a python module - buran - Mar-05-2024 What do you get if you do print(dir(myLib))
RE: using ctypes to use a dll in a python module - dauriac - Mar-06-2024 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. [solved] using ctypes to use a dll in a python module - dauriac - Mar-06-2024 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. |