2

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?

Namingwaysway
  • 270
  • 1
  • 17
  • 2
    Redirect the POSIX file descriptor (`os.dup2`) and also the Windows file handle (`kernel32.SetStdHandle`). See [this answer](http://stackoverflow.com/a/17953864/205580). – Eryk Sun Apr 28 '14 at 20:14

1 Answers1

1

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.

kitti
  • 14,663
  • 31
  • 49
  • So, that will have to be done in the c side of things - there is nothing I can really do from python to handle that? – Namingwaysway Apr 28 '14 at 20:15
  • 1
    It all depends on what you can access via `ctypes` - try the answer @eryksun commented above as well. – kitti Apr 28 '14 at 20:20