Python ctypes DLL标准输出 [英] Python ctypes DLL stdout

查看:163
本文介绍了Python ctypes DLL标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am calling a DLL function from python using ctypes, and simply I want to capture the stdout from the dll call. There are a lot of printf's I would like to redirect without changing the dll code itself.

我该怎么做?

推荐答案

看起来您需要制作一个包装器。尝试如下操作:

Looks like you'll need to make a wrapper. Try something like this:

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

实际上,您需要对该字符串进行其他操作,而不是返回它;我将把这部分留给你。返回后,字符串指针将立即失效,但是只要在返回后取消分配内存,就可以进行相当简单的分配。

You will actually need to do something different with that string rather than returning it; I'll leave that part up to you. The string pointer becomes invalid as soon as it is returned, but you could do a fairly simple allocation as long as you deallocate the memory after it is returned.

这篇关于Python ctypes DLL标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆