Namingwaysway
Namingwaysway

Reputation: 270

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?

Upvotes: 2

Views: 1550

Answers (1)

kitti
kitti

Reputation: 14794

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.

Upvotes: 1

Related Questions