栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/668539.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号