Python ctypes DLL 标准输出

标签 python ctypes

我正在使用 ctypes 从 python 调用 DLL 函数,我只是想从 dll 调用中捕获标准输出。我想在不更改 dll 代码本身的情况下重定向很多 printf。

我该怎么做呢?

最佳答案

看起来你需要做一个 wrapper .尝试这样的事情:

char* CallSomeFunc()
{
    ostrstream ostr;
    cout.rdbuf(ostr.rdbuf());
    SomeFunc();
    char* s = ostr.str();
    return s;
}

您实际上需要对该字符串做一些不同的事情而不是返回它;我会把那部分留给你。字符串指针一返回就失效,但只要在返回后释放内存,就可以进行相当简单的分配。

关于Python ctypes DLL 标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23350008/

相关文章:

python - 查找 Python 模块文件名

python - 如何记录k-means中每次迭代的质心?

python - 读取文件的最后 n 行(尾部)而不逐行读取?

python - Ctypes 返回错误的结果

python - 尝试将带有 ctypes 的 numpy 数组转换为 C 会出现段错误

python - 使用 itertools.permutation where r > n

Python:打开大型文本文件,但仅加载包含预定义列值的行

python - 在Python中使用Ctypes读取Dll的双c结构

python - 我是否正确使用 ctypes 来 pythonify 这个结构?

python - 如何在 C 函数中访问 python 变量的数据? (参数类型 : void *)