I have some MEX code that calls functions from a DLL. The DLL has a bunch of printf statements scattered around that are useful for debugging. However, I cannot see their output in the MATLAB command window. I've done some reading and apparently this is because the command windows is not treated as a console application where the output from printf can be sent. I have tried using this link's solution to no avail. I have access to the source code of the DLL. I am compiling for Windows 7 in Visual Studio 2010. I have tried using cout and that works; however, I do not want to rewrite all of the functions to use cout. Is there something I can do to redirect the output of printf to MATLAB's command window?
Asked
Active
Viewed 1,036 times
1 Answers
1
You need to call SetStdHandle
before the DLL loads and initializes its runtime library. You can attach the write end of a pipe, and read from the other end.
Once the runtime library initializes, it will have global data tied to the original stdout handle, and may not be affected by future calls to SetStdHandle
.

Ben Voigt
- 277,958
- 43
- 419
- 720
-
Thanks. I'm assuming that the DLL loads and initializes its runtime libray when the mex file that uses it runs for the first time, correct? If so, where would I call `SetStdHandle`? – Damian Jul 14 '15 at 14:51
-
@Damian: Before calling the mex function? – Ben Voigt Jul 14 '15 at 14:54
-
Would it be to late to do this in `DLLMain` as shown [here](http://stackoverflow.com/a/20577537/2778484)? Otherwise, I don't understand how to do this without a wrapper mex file that uses the actual mec file. – chappjc Jul 14 '15 at 16:16
-
@Ben: The mex function was linked against the library I mentioned. Therefore, whenever I run the mex function, I'm assuming it will load the DLL. So where should I run that SetStdHandle? – Damian Jul 14 '15 at 17:05
-
@chappjc: I see what you are saying: put `SetStdHandle` in `DLLMain`, correct? Alternatively, I could create a Mex file that does this and run that at the beginning. Is `SetStdHandle` permanent like that? – Damian Jul 14 '15 at 17:15
-
@Damian I've never tried this before, but perhaps something like this could work: https://github.com/diablodale/jit.openni/blob/master/src/dllmain.cpp – chappjc Jul 14 '15 at 17:41
-
`SetStdHandle` changes OS-level process state, so it will persist between mex calls. You can call it from a mex, but you want to do so without causing C++ runtime library initialization... either use a different runtime library instance (via static linking) from the mex you're trying to redirect, or have the setup mex use no runtime library function at all (compile with `/NODEFAULTLIB` and `/ENTRYPOINT`). `DllMain` in the target mex won't work, since it runs after any runtime library initialization happens. – Ben Voigt Jul 14 '15 at 17:56
-
@Ben: So in this setup mex function, would I have to create a console myself with something like a `freopen("CONOUT$", "w", stdout)` and the use `SetStdHandle`? How would I redirect the output to a console? – Damian Jul 14 '15 at 18:30
-
@BenVoigt So you could make a mex file, say `setupOutputConsole`, build with no runtime library or independent statically-linked runtime (using those switches for none or `/MT` for static?), and call it before any other mex file? Thanks for the info -- very interesting! – chappjc Jul 14 '15 at 18:33
-
@Damian: You can pass a write pipe handle to `SetStdHandle`. And then you can collect the output from the read end of the same pipe. See [`CreatePipe`](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365152(v=vs.85).aspx). – Ben Voigt Jul 14 '15 at 18:48
-
@chappjc: Yes... the one exception would be the runtime library instance used by Matlab, which is initialized long before any user code. If your target MEX is sharing with Matlab itself (by using the same Visual Studio version Matlab was compiled with), you'd need to use `freopen` to update its global state. – Ben Voigt Jul 14 '15 at 18:51