简体   繁体   English

如何在python例程中使用ctypes在dll例程中传递文件指针

[英]How to pass file pointer in dll routines using ctypes in python

I'm accessing one function from dll which need file pointer (char *filename). 我正在从dll访问一个需要文件指针(char *文件名)的函数。 How can I pass it through python? 我如何通过python传递它?

handle = cdll.dsp

p = open("signal.txt", "r")

handle.filter(p)

A file pointer is FILE * . 文件指针是FILE * But you mentioned you need char *filename , which is a char pointer (ie a null-terminated string). 但是您提到您需要char *filename ,这是一个char指针(即,以空字符结尾的字符串)。 I'm going to assume the C function has a prototype void filter(char *filename) . 我将假设C函数具有原型void filter(char *filename)

First you must define the function argument types and the return type : 首先,您必须定义函数参数类型和返回类型

import ctypes
filter = ctypes.cdll.dsp.filter
filter.argtypes = (ctypes.c_char_p,)
filter.restype = None  # Or whatever type it returns.

Then you call it: 然后,您将其称为:

filter("signal.txt")

By the way, you tagged your question as Python 2.7, but nowadays you should move on to Python 3, unless you have a really good reason to stay at the older version. 顺便说一句,您已将问题标记为Python 2.7,但如今,除非有充分的理由继续使用旧版本,否则应继续使用Python 3。

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

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