本帖最后由 Minecraft程序猿 于 2023-1-22 00:30 编辑
如题
报错信息:Python 3.11.1 (tags/v3.11.1:a7a450f, Dec 6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cinterface
>>> cinterface.run_find("D:", "main.c")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\repos\ProC\slrc_build\finder\cinterface.py", line 24, in run_find
target = engine.find(c_char_p(directory.encode()), c_char_p(pattern.encode())).contents
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: NULL pointer access
问题源码:from ctypes import *
engine = windll.LoadLibrary("./refinder_engine.dll")
class File_list(Structure):
pass
File_list._fields_ = [\
("next", POINTER(File_list)),
("file_type", c_char),
("file_name", c_char_p)]
engine.find.argtypes = [c_char_p, c_char_p]
engine.find.restype = POINTER(File_list)
def run_find(directory:str, pattern:str) -> list:
'''Perform Find Operations.
Usage: run_find(directory, pattern)
e.g. run_find("C:\", "main.c")
Return:A list([(name_1, type_1), (name_2, type_2), ...])
'''
table = []
# print(engine.find(c_char_p(directory.encode()), c_char_p(pattern.encode())))
target = engine.find(c_char_p(directory.encode()), c_char_p(pattern.encode())).contents
while target != POINTER(None):
table.append((target.file_name, target.file_type))
target = target.next
return table
结构体定义:typedef struct File_type
{
struct File_type *next;
char file_type;
char file_name[261];
}file, *pfile;
需要访问的C函数原型:DLL_EXPORT pfile __stdcall find(char *workdir, char *pattern);
源代码及已编译 Dll 资源文件(编译器: gcc(MinGW-w64)):
finder.zip
(15.87 KB, 下载次数: 0)
已经解决 是Python端类型声明错误
把谁设为最佳答案?
|