I have a C++-Library (example.dll) with some functions and want to use them from Python. I got it working with
ctypes and Python 2.7, but not with Python 3.6.
Here an excerpt from example.h :
extern "C" __declspec(dllexport) LONG _stdcall func1(LPTSTR filename, long cbAddress);
The function is parsing a txt-file, loads some data to the memory and returns a handle to this data.
My code for Python 2.7 is:
from ctypes import * mydll = WinDLL(r'C:\temp\example.dll') txt_path = c_char_p(r'C:\temp\file.txt') func1 = mydll['func1'] func1.restype = c_long func1.argtypes = (c_char_p , c_void_p) handle = func1(txt_path, None)
This is working and returns a valid handle.
With Python 3.6, line 3 causes an error:
>>> txt_path = c_char_p(r'C:\temp\file.txt') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: bytes or integer address expected instead of str instance
so I changed it to
txt_path = c_char_p(b'C:\temp\file.txt')
which is working, but leads to a problem with the function.
func1 returns now 0, which is an intern error code for "could not read txt-file."
I tried to use different types, but it didn't work and I'm kind of stuck now.
Can anyone help?