使用Python调用DLL函数时出现ctypes.ArgumentError

使用Python调用DLL函数时出现ctypes.ArgumentError,第1张

使用Python调用DLL函数时出现ctypes.ArgumentError

ctypes 官方文档:[Python 3.5]:ctypes-
Python的外部函数库

创建了一个虚拟 .dll模仿您的行为。

dll.c

#include <stdio.h>#include <Windows.h>__declspec(dllexport) BOOL InitNetwork(char LocalIP[], char ServerIP[], int LocalDeviceID) {    printf("From C:ntLocalIP: [%s]ntServerIP: [%s]ntLocalDeviceID: %dn", LocalIP, ServerIP, LocalDeviceID);    return TRUE;}

检查[SO]:如何编译用C编写的64位dll?(@CristiFati的答案)以获取有关如何构建它的详细信息(采取了完全相同的步骤,在这种情况下,命令是:)

cl /nologo /LD /DWIN64/DWIN32 /Tp dll.c /link /OUT:NetServerInterface.dll

(主要)问题在于,在 Python3中

char*
不再映射string ,而是映射到 bytes 对象。

pre.py

import sysimport ctypesfrom ctypes import wintypesFILE_NAME = "NetServerInterface.dll"FUNC_NAME = "?InitNetwork@@YAHQEAD0H@Z"  # Name different than yours: if func was declared as `extern "C"`, the "normal" name would be requireddef main():    net_server_interface_dll = ctypes.CDLL(FILE_NAME)    init_network = getattr(net_server_interface_dll, FUNC_NAME)    init_network.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int]    init_network.restype = wintypes.BOOL    init_network(b"192.168.1.103", b"192.168.1.103", 1111)if __name__ == "__main__":    print("Python {:s} on {:s}n".format(sys.version, sys.platform))    main()

输出

(py35x64_test)

e:WorkDevStackOverflowq050325050>”e:WorkDevVEnvspy35x64_testscriptspython.exe”
pre.py
Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit
(AMD64)] on win32

From C:        LocalIP: [192.168.1.103]        ServerIP: [192.168.1.103]        LocalDeviceID: 1111


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5673796.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存