鱼C论坛

 找回密码
 立即注册
查看: 4375|回复: 4

[已解决]ctypes模块访问C语言函数返回指针报错

[复制链接]
发表于 2023-1-21 18:13:37 | 显示全部楼层 |阅读模式
5鱼币
本帖最后由 Minecraft程序猿 于 2023-1-22 00:30 编辑

如题
报错信息:
  1. Python 3.11.1 (tags/v3.11.1:a7a450f, Dec  6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] on win32
  2. Type "help", "copyright", "credits" or "license" for more information.
  3. >>> import cinterface
  4. >>> cinterface.run_find("D:", "main.c")
  5. Traceback (most recent call last):
  6.   File "<stdin>", line 1, in <module>
  7.   File "D:\repos\ProC\slrc_build\finder\cinterface.py", line 24, in run_find
  8.     target = engine.find(c_char_p(directory.encode()), c_char_p(pattern.encode())).contents
  9.              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  10. ValueError: NULL pointer access
复制代码

问题源码:
  1. from ctypes import *

  2. engine = windll.LoadLibrary("./refinder_engine.dll")

  3. class File_list(Structure):
  4.     pass

  5. File_list._fields_ = [\
  6.     ("next", POINTER(File_list)),
  7.     ("file_type", c_char),
  8.     ("file_name", c_char_p)]

  9. engine.find.argtypes = [c_char_p, c_char_p]
  10. engine.find.restype = POINTER(File_list)

  11. def run_find(directory:str, pattern:str) -> list:
  12.     '''Perform Find Operations.
  13.     Usage: run_find(directory, pattern)
  14.       e.g. run_find("C:\", "main.c")
  15.     Return:A list([(name_1, type_1), (name_2, type_2), ...])
  16.     '''
  17.     table = []
  18.     # print(engine.find(c_char_p(directory.encode()), c_char_p(pattern.encode())))
  19.     target = engine.find(c_char_p(directory.encode()), c_char_p(pattern.encode())).contents
  20.     while target != POINTER(None):
  21.         table.append((target.file_name, target.file_type))
  22.         target = target.next
  23.     return table
复制代码

结构体定义:
  1. typedef struct File_type
  2. {
  3.         struct File_type *next;
  4.         char file_type;
  5.         char file_name[261];
  6. }file, *pfile;
复制代码

需要访问的C函数原型:
  1. DLL_EXPORT pfile __stdcall find(char *workdir, char *pattern);
复制代码

源代码及已编译 Dll 资源文件(编译器: gcc(MinGW-w64)):
finder.zip (15.87 KB, 下载次数: 0)
已经解决 是Python端类型声明错误
最佳答案
2023-1-21 18:13:38
把谁设为最佳答案?

最佳答案

查看完整内容

把谁设为最佳答案?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-1-21 18:13:38 | 显示全部楼层    本楼为最佳答案   
把谁设为最佳答案?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-1-21 18:25:55 | 显示全部楼层
等下 我似乎找到原因了,路径要加上分隔符,第一个参数传"D:"会出问题,而"D:\\"就不会(引向了下一个bug...),另外一点,第8行_fields_要改成__fields__...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-1-21 23:42:46 | 显示全部楼层
不需要回答了 问题已解决 最后附上完整的项目文件 refinder_release.zip (16 KB, 下载次数: 1)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-2-1 15:37:17 | 显示全部楼层
本帖最后由 Minecraft程序猿 于 2023-2-1 15:39 编辑
KeyError 发表于 2023-1-31 17:31
把谁设为最佳答案?

emmm...我项目都搞完了(利用高一寒假仅有的3天)
GitHub项目地址 https://github.com/Littlebox-Logic/Refinder
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-27 15:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表