简体   繁体   中英

Python ctypes DLL stdout

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.

How would I go about doing that?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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