C++ – How to convert anything to string?

Well, at least anything that can be printed. It’s quite easy.

  1. Create a string stream
  2. “Print” the value to the stream
  3. Get a string from the string stream

I like to wrap it in a template function like that:

#include <string>
#include <sstream>

template <typename T>
string to_string(T x)
    stringstream ss;
    return ss.str();

You can make the function inline, and get the argument as a reference to const if you want.

Have you got a simpler generic solution?

Betrayal at Krondor – How to make it sound great as you remember?

I’ve just purchased Betrayal at Krondor from gog.com. This game was one of my favorites when I was a kid, not knowing a word in English. I haven’t played it since then.

As I started to play, it was obvious that three things about the sound were different:

  1. Sound effects were totally different than what I remembered.
  2. The music, in stores or chest for instance, wasn’t looping as it should have been.
  3. The quality of the music was much better than before.

Solve 1

After a bit of researche I found what was the problem. Games of that time had different drivers for different sound cards. In a config file, it is possible to select which sound driver you want to use. Now that I run the game in DosBox, the game works regardless of the driver I choose, but the choice does change something. It changes the sound bank (sound font) used in the game. My old 486 had a SoundBlaster, so I just had to tell the game to use its SoundBlaster driver like that:
In the file


change the line

soundDrv  = MT32.DRV


soundDrv  = SNDBLAST.DRV

That’s all.

Solve 2

This is caused by the fact that the music used by the game is no longer MIDI as it was back then, but “CD music”, which means, pre-recorded, high quality music. They improved the music, but in the process lost the loops. In order to bring the loops back, all you have to do is go to Krondor’s preferences, in-game, and disable CD music, that’s all. You’ll get a significantly lower quality music, but I prefer that over silence in long battles and while solving riddle-chests.

What about 3?

So we lost the high quality music, isn’t there anything we can do to get the good from both world?
Well, I don’t know whether it’s possible to make the CD music loop or not, but I do know that it is possible to significantly improve the quality of the MIDI music. I haven’t done that, because I’m happy with the music as I remember it, but if you want, you can follow [url=http://www.gog.com/forum/krondor_series/guide_to_betrayal_at_krondor_music_and_sound_effects_settings]this guide[/url] which is more complicated but achieves more.


Microsoft or Windows Update stopped working with error 0x8024402F

It happened to me twice on a new installation of Windows XP. In the first time I reinstalled Windows to fix it. On the second time I researched a bit and found the problem to be the installation of .NET 4.

The only way I found to fix it was to uninstall .NET 4 completely. Repair of .NET 4 or of Windows update did not help.
As far as I know, if you use Windows XP you have to live without .NET 4 or without Windows Update. If you use newer versions of Windows you can try installing .NET 4.5 since it’s a replacement of .NET 4. Unfortunately Windows XP is not supported by .NET 4.5.

I would love to know if you have a solution for Windows XP users.

([url=https://social.technet.microsoft.com/Forums/en-US/itproxpsp/thread/fdd723f8-af81-4331-8c56-0b09432d92bc]a conversation about the topic[/url])

Update: I have asked the quesion on superuser.com and some time after trying to solve the problem it started working. I don’t know what helped or if it is something from this page but [url=http://superuser.com/questions/636554/how-to-install-net-4-on-windows-xp-without-getting-error-0x8024402f-from-window]you can try[/url].

Why Windows XP becomes very slow permanently after disk I/O errors and how to fix it

I have two hard drives, one is SATA and the other one is an old IDE (PATA). I use mostly the IDE because it is larger.
The SATA was causing errors which crashed my Windows and Linux OS’s so I disconnected it altogether. The crashes vanished, but XP became inconceivably slow. Boot was taking about 30 seconds, it became a few minutes. Opening applications was also very slow.

I didn’t have a clue what could cause that so I searched, and after some time I have found this: [url=http://support.microsoft.com/default.aspx?scid=kb;en-us;817472]Link to Microsoft support[/url]

Basically what it says is that for some reason, after too many I/O errors, Windows XP changes the way it interacts with the hard drive to be very slow and reliable. When the I/O errors stop happening it should have been reverted to the fast method again but that does not happen.

To fix that, first eliminate the source of I/O errors (in my case, the SATA drive), and then uninstall the IDE controller driver through Device Manager. Reboot, and Windows will install the correct driver, reboot again and voila, your XP is fast again.

How to install SublimeClang on Archlinux

SublimeClang is a cool and useful extension for SublimeText which enables IDE features for C++ programmers.
Here are the installation instructions for Linux from [url=https://github.com/quarnster/SublimeClang#additional-prerequisites-linux-only]their github page[/url].

There are two problems with the installation on Archlinux.

Problem A

SublimeText looks for Python 2.6. Archlinux has Python 3 and Python 2.7,


Step 1

Install [url=https://aur.archlinux.org/packages/python26]python26[/url] from AUR.

Step 2

Create a symlink to the new Python 2.6 installation in the right place for SublimeText to find, [b]as root[/b] (sorry, I don’t know how to solve it without creating a symlink as root).

ln -s /lib/python2.6/ /opt/sublime-text/lib/python2.6

Problem B

When trying to build libcache the build environment won’t find libclang.so and will try to build it and fail.


Copy the file to a place where it will be found.

cp /usr/lib/llvm/libclang.so ~/.config/sublime-text-2/Packages/SublimeClang

Now clean the build environment if needed and run the cmake and make commands as written in the instructions.

It should work now. Tested on Archlinux 64 bit.

How to tell ruby (rails) which version of rake to use?

I am playing a bit with Rails on Archlinux. and the first command I ran errored on me:

% rails new Site
run bundle install
Fetching gem metadata from https://rubygems.org/...........
Fetching gem metadata from https://rubygems.org/..

Errno::EACCES: Permission denied - /usr/lib/ruby/gems/1.9.1/cache
An error occurred while installing rake (10.0.2), and Bundler cannot continue.
Make sure that `gem install rake -v '10.0.2'` succeeds before bundling.

I do have ‘rake 10.0.2’ installed but I also have ‘rake’ because Archlinux bundles it with the Ruby package. I struggled to find a way to tell Rails to use the newer package.

The solution turned to be quite easy. Just set the GEM_HOME environment variable to where your gems are installed. For example:

export GEM_HOME=/home/username/.gem/

If you want it to be persistent add it to a file which is sourced every time you log in, like for zsh you can add to the file /home/username/.zshenv :


Now it will work:

% rails new Site
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

How to install Microsoft Security Essentials on Windows Server 2012 or The best antivirus for Windows Sever 2012

In my experience, MSE (Microsoft Security Essentials) is the best antivirus considering price, efficiency and lightness. Unfortunately it is impossible to install it on Windows Server 2012. Microsoft does not allow it for some reason.

Fortunately, Microsoft has another product which is so similar to MSE that you won’t notice the difference. It is part of the System Center 2012 suit from Microsoft. The whole suit is free but weighs 1.5GB so it is quite inconvenient to download it and find the Antivirus inside. So lucky you are, someone extracted the antivirus for you.
Tested, confirmed to work, and it is clean for sure because it is extracted right from a Microsoft archive.

Note: you may want to look at pytex’s comment for an updated version. I did not test it.

How to run Starcraft Brood War and Diablo II on Windows 7? (Color correction)

Old games and new versions of windows not alwasy go well together, but sometimes there are games which stay with us for a very long time and require some fiddling in order to work.

Most of the problems with old games can be solved by simply running them in compatibility mode or as administrator. Starcraft does not need it, and Diablo does. These two games have another problem. Although they run, their colors are messed up. Luckily, once you know the solution it is very simple. Close explorer.exe and viola! the colors are good again.

Closing explorer.exe has a price though. You can’t change volume, and it is harder to start up your games. In order to help with these two tasks, I wrote a little C# application which allows you to close explorer, run your games and open the sound mixer in order to control the sound. Extremely simple but useful.

It can be downloaded from here: [url=http://www.kfr.co.il/files/Windows_7_Gaming_Compatibility/Windows_7_Gaming_Compatibility_32bit.7z]32-bit[/url], [url=http://www.kfr.co.il/files/Windows_7_Gaming_Compatibility/Windows_7_Gaming_Compatibility_x64.7z]64-bit[/url], and the [url=http://www.kfr.co.il/files/Windows_7_Gaming_Compatibility/Windows_7_Gaming_Compatibility.src.7z]source code[/url].
Make sure to use the right architecture because manipulating explorer with the wrong version won’t work.


How to remove selected items from a Gtk::TreeView using gtkmm? The painless way.

I am building an application using C++/gtkmm-3.0. The development process is sometimes nice and sometimes too complicated in my opinion. The worst part until now was when I wanted to remove the selected items from a tree view.

This is a tree view:
Behind a tree view there is a “store” which holds the information for the view, like a model in MVC. Sometimes you need to enable the user to choose multiple items from the view and delete them. Should be simple, right?

I was expecting something like




but no, it won’t be that simple.
According to the official documentation you have to reviece a “selection” object from the treeview and keep it. Then you have to create a (wasn’t so easy for me to find it’s structure) callback function that deletes an item, and when the user presses delete you tell the “selection” something like

selection.selected_foreach_iter(pointer to the callback function);

And that’s supposed to do it. I was surprised to see the steps needed, and even after it did somewhat work, Gtk wrote on the command line that this way of iterating over a selection is for reading only and for manipulating the store I have to use another way. Well, I wasn’t the one who came up with that method, I saw it in a few examples and tutorials and it was used to delete items.

I became tired of the over complication and found a simple little way to do it. I don’t know if it “right”, but it is simple and it works.

We have our class, with the view, store, and selection as private members.

Gtk::TreeView treeview;
Glib::RefPtr&lt;Gtk::ListStore&gt; store;
Glib::RefPtr&lt;Gtk::TreeSelection&gt; selection;

In the constructor we initialize the selection

selection = treeview.get_selection();
selection-&gt;set_mode(Gtk::SELECTION_MULTIPLE); // enable multiple selection

And then, all we have to do in order to remove the selecte items is:

void MainWindow::_remove_selected()
    vector&lt;Gtk::TreeModel::Path&gt; paths = selection-&gt;get_selected_rows();
    for(int i = paths.size()-1; i&gt;=0; i--)

That’s all. 3 lines of code. Why not do it like that? Why the over complication? Who knows.