Tag: Visual Studio

Redirecting standard output to console in Windows

When I first started making Windows programs in the early 2000s I needed a way to display logging information visually to the user. With some internet sleuthing I discovered how to create a windows console and using black magic, how to redirect the standard input/output streams to print to this window. With that problem solved I happily used that code unchanged for many years.

At some point it stopped working – I suspect when Microsoft updated their standard library implementation in a newer version of Visual Studio it caused the solution to stop working. Curious, I looked at the code for the first time in nearly a decade and recoiled in horror. The old code redirected output by mucking around with handles and copying the contents of internal FILE structs!

While researching a better solution to the problem of input redirection, I came across an answer on Stack Exchange. It turns out someone else was having the same problem I did, and one of the answers had the same story I did about using the code above and discovering how hacky it was years later! He gives a pretty detailed explanation for what the old code was doing, and how to use freopen to accomplish this behavior correctly.

And now for the solution! Here’s the new code I use to create a Windows console, and redirect standard input/error/output to the console:

That should do it! Remember to properly close the file pointers before the program closes. (It’s not the end of the world if you don’t – Windows will clean it up when the program exits).

Getting rid of C#’s obj folders

A small tip for developers who are irritated with Visual Studio doing this to your pristine source tree:

Why Visual Studio, WHY?!
obj folders mucking up your day

To get the obj directories out of your source tree, and somewhere nicer (your project’s build output directory for example) open up your project’s .csproj file in an external editor. Locate any line that has the <OutputPath> tag on it. On the line below, insert the following:

For example, in my Dungeon Crawler project I have the following in Dungeon Crawler Shared.csproj

Hope that helped!

Compiling Qt with Visual Studio

So your awesome project that you’re developing with Visual Studio, and you want to use Trolltech’s Qt library? Luckily Trolltech provides an awesome Visual Studio plugin that does almost everything Qt creator does (minus Intellisense support for slots). The bad news is that Trolltech doesn’t directly tell you how to go about doing this:

  1. Download and install the Qt for Visual Studio plugin.
  2. Download the source code for Qt 4.7
  3. Start the Visual Studio 2010 Command Prompt ( Microsoft Visual Studio 2010 -> Visual Studio Tools -> Visual Studio Command Prompt)
  4. Navigate to the directory with your downloaded Qt SDK
  5. Run the configure program like so: “configure -platform win32-msvc2010 -debug-and-release -static -no-gif -no-qt3support -no-dbus -no-phonon-backend”
  6. Wait ten or so minutes for configure to finish
  7. Now run nmake. And make dinner, because this will take a very long time
  8. You have Qt ready to go! Yay!

The configure parameters I provide will configure Visual Studio to build the vast majority of Qt. There’s a lot of extra options you can explore if you desire more control – for instance, you can enable exception support.

Have fun!