How can I call DLL function using ctypes in python?

Viewed 41

I want to use DLL(C++) API with python. The DLL file is MMMReaderHighLevelAPI.dll from 3M SDK.

First, I want to make sure if I can connect to the 3M Page Reader by getting the status of it. But, It does not work.

Here is my code that I have tried.

import ctypes

lib = ctypes.WinDLL(r"C:\Program Files\3M\3M Page Reader x64\3.3.3.10\Bin\MMMReaderHighLevelAPI.dll")
lib.MMMReader_GetState.argtypes = None
lib.MMMReader_GetState.restype = ctypes.c_char_p

call = lib.MMMReader_GetState()

print(call)

And I got None from the printing.

What should I try next?

This is the function that I want to use.

3M Page Reader Programmers' Guide

Thank you very much for helping!

1 Answers

Based on the header file of the SDK from here the following code should work (but wasn't tested).

The DLL you pointed to is 64 bit but in case you ever want to use a 32 bit variant, the calling convention of the DLL matters, so maybe "WinDLL" must be replaced by "CDLL" then (64 bit has only one calling convention):

import ctypes

# Constants to compare to return value (may be incomplete, header file has more)
READER_NOT_INITIALISED = 0
READER_ENABLED = 1
READER_DISABLED = 2


lib = ctypes.WinDLL(r"C:\Program Files\3M\3M Page Reader x64\3.3.3.10\Bin\MMMReaderHighLevelAPI.dll")
lib.MMMReader_GetState.argtypes = None
lib.MMMReader_GetState.restype = ctypes.c_int

call = lib.MMMReader_GetState()

print(call)
Related