Accessing a dll with ctypes crashes python.exe

1

I want to access functions of a dll with python and ctypes. I use python 3.6.1 32 bit with Visual Studio Code running on Win10 64 bit. The dll is part of a test software and does some configuration and calls some further dlls. It creates an image, loads the image to a video test hardware and a DUT gets tested with this image. I have no access to the dll and can't modify it. There is only a .h available.

First, I want to describe the relevant part of the .h file:

#ifdef __cplusplus
extern "C" {
#endif

#ifdef BUILD_INTERFACE_DLL
#define EXPORT_TO_INTERFACE_DLL __declspec(dllexport)
#else
#define EXPORT_TO_INTERFACE_DLL __declspec(dllimport)
#endif

struct Interface_t;
typedef struct Interface_t Interface;
EXPORT_TO_TESTINTERFACE_DLL Interface * createInterface(const char* versionTag);
EXPORT_TO_TESTINTERFACE_DLL int initConnection(Interface* Interface, const char * deviceId);
EXPORT_TO_TESTINTERFACE_DLL int generateImage(Interface* Interface,char* pathToImage, size_t maxFileNameLength);

Here is my Python script:

import ctypes as ct
lib = ct.CDLL("./TestInterface.dll")

libEcu.createInterface.restype = ct.POINTER(ct.c_int32)
libEcu.createInterface.argtypes = [ct.c_char_p]
libEcu.initConnection.restype = ct.c_int32
libEcu.initConnection.argtypes = [ct.POINTER(ct.c_int32), ct.c_char_p]
libEcu.generateImage.restype = ct.c_int32
libEcu.generateImage.argtypes = [ct.POINTER(ct.c_int32), ct.c_char_p, ct.c_int32]

CamDeviceId = (b"SomeConfigString")
MaxFileNameLength = 1000
pathToImage = (b"generatedTestImage.bmp")
versionTag = (b"SomeVersionString")

Interface = lib.createInterface(versionTag)
lib.initConnection(Interface, CamDeviceId)
lib.generateImage(Interface, pathToImage, MaxFileNameLength)

Here is my issue: createInterface and initConnection are working OK. I get some messages on the terminal confirming that the interface is created and the connection is successfully initialized.

When calling "generateImage", i get the error message "Python stopped working" with the option to debug or close the program. No error message in Visual Studio Code. The function executes correctly, the image gets generated and sent to the test hardware. As I have no further possibility to debug in Visual Studio Code, I had a look at the Event Viewer of Windows. The crashed application is "python.exe", the crashed module is "python.dll" and the exception code "0xc0000005"

Any help on this issue? Is there something I did wrong? I'm quite new to Python. When accessing the dll with a C program, every function works properly.

Thank you and Regards

python
dll
ctypes
asked on Stack Overflow Feb 11, 2021 by visafan

1 Answer

0

@MarkTolonen

Replacing

(b"generatedTestImage.bmp")

with

ct.create_string_buffer(b"generatedTestImage.bmp", MaxFileNameLength)

solved the issue.

The issue was the length of the buffer. It needs to be defined and not too small (30 crashes, 50 works). I set it to MaxFileNameLength = 1000, which I copy&pasted from some working C-code example.

Thank you very much!

answered on Stack Overflow Feb 12, 2021 by visafan

User contributions licensed under CC BY-SA 3.0