简体   繁体   English

如何使用 python 中的 ctypes 调用 DLL function?

[英]How can I call DLL function using ctypes in python?

I want to use DLL(C++) API with python.我想将 DLL(C++) API 与 python 一起使用。 The DLL file is MMMReaderHighLevelAPI.dll from 3M SDK. DLL 文件是来自 3M SDK的 MMMReaderHighLevelAPI.dll。

First, I want to make sure if I can connect to the 3M Page Reader by getting the status of it.首先,我想通过获取它的状态来确定我是否可以连接到 3M 页面阅读器。 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.None从印刷中得到任何东西。

What should I try next?接下来我应该尝试什么?

This is the function that I want to use.这是我想使用的 function。

3M Page Reader Programmers' Guide 3M 页面阅读器程序员指南

Thank you very much for helping!非常感谢您的帮助!

Based on the header file of the SDK from here the following code should work (but wasn't tested).基于此处的 SDK 的 header 文件,以下代码应该可以工作(但未经测试)。

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):您指向的 DLL 是 64 位的,但如果您想使用 32 位变体,则 DLL 的调用约定很重要,因此可能“WinDLL”必须替换为“CDLL”(64 位只有一个调用约定):

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM