Feb 2, 2010

Redirecting std::cout in managed apps

I recently needed to write a library in Managed C++ that wrapped another native C++ library. I wanted to display the output from the native lib in a C# application. With a little help from Google I ended up with this.


It is simply an implementation of a streambuf, that has a System::String object as an internal buffer and a System.IO.TextWriter to redirect the output to. Thanks to the gcroot functionality that allows us to have a managed reference in an unmanaged class.

The following code installs the redirector:

COutputRedirector *redirectedOut = new COutputRedirector(writer);
std::streambuf* stdCout = std::cout.rdbuf(redirectedOut);

With writer a managed reference to the TextWriter of your choice. Keep a pointer to the standard streambuf to put it back if needed.

This captures all output to std::cout (and possible std::cerr). But for capturing output with printf we need another trick. I didn't need that yet so I didn't delve into that, but maybe later I will, and then I'll post an update.

Feel free to comment.

Jul 19, 2007

Map Shower

In 3DStudio Max you can view a certain map from a certain material in the viewport by clicking the "Show Map in Viewport" button.

If an artist wants to view all diffuse maps (or normal maps, or specular maps, ...) in the scene then he needs to select all diffuse maps of all his materials and enable the button. Not really a job one would do very often, although that it is very interesting to view all maps in a scene of a specific type.

This macroscript displays a dialog with a list of radio buttons, corresponding to the most used maps in a max material. Just click a button to enable all the maps in the complete scene. You can select None to disable them all.

If you want to add more or other maps, you can easily alter the script.




Draggable NumericUpDown

XNumericUpDown inherits from System.Windows.Forms.NumericUpDown. With this control, as you click on the up or down button and then drag up and down, the value is incremented/decremented according whether you go up or down with the mouse.

I created this behaviour based on the 3d Studio Max up/down controls, which have similar functionality. I use this control in all applications that need to be used by artists working also with Max, it's a feature they really miss elsewhere. And I think it handy too :-).

Obviously, I've written this in C#, enjoy.