MFC prevents bad_alloc from being thrown

According to the C++ standard, the new operator should throw std::bad_alloc if it fails. This will typically happen if your process has run out of memory. However, this isn’t the case if your program uses the (rather outdated) Microsoft Foundation Classes. In this post, we’ll look at what’s going on, and what you can do about it.
Read more MFC prevents bad_alloc from being thrown

Convert a number to a binary string (and back) in C++

Sometimes it’s useful to output the binary representation of a number in text, i.e. as an ASCII string of 0’s and 1’s. There are also situations where you might want convert back the other way, e.g. if you want to let a user enter a binary string manually. The bitset class in C++ makes this surprisingly quick and easy.
Read more Convert a number to a binary string (and back) in C++

Changing the emulation mode of the Microsoft Web Browser ActiveX control

I’ve been working on an MFC project which embeds a basic web-browser component in a dialog, in the form of a Microsoft Web Browser ActiveX component. (I know… these are ancient technologies… but sometimes you’ve got to work with what’s available on a project!)

Technically, the control hooks into whatever version of Internet Explorer (IE) you’re running on the system. However, it always seemed to fall-back on IE7 emulation mode for us, meaning a lot of our modern standards-compliant HTML wouldn’t work properly. Thankfully, there is a way to fix this problem, although it’s far from obvious!

Read more Changing the emulation mode of the Microsoft Web Browser ActiveX control

NSIS Access Control problem with built-in users group

I was dealing with a subtle issue recently involving setting access permissions using NSIS (Nullsoft Scriptable Install System). In the end, it turned out that the problem was not with NSIS at all. Rather, it was a misunderstanding on our part regarding an unexpected quirk in Windows. However, hopefully this post will help anybody who encounters a similar issue.

Our installer was supposed to enable read/write permissions for all users on certain important files and registry keys. However, some users were finding these files/keys were not accessible, and it was preventing the software from working correctly.

Read more NSIS Access Control problem with built-in users group

Automatically output the callstack on a breakpoint in Visual Studio

When you’re dealing with a large program and multiple developers, it’s not always obvious how and when certain things get executed. One of the very useful ways to debug unexpected behaviour is to set a breakpoint on a suspect line of code, and examine the callstack when it gets hit to see the execution path.

For infrequent events, it’s not always desirable to halt the entire program while you do that though. Instead, you can tell Visual Studio to write the callstack to the output when the breakpoint gets hit, and immediately continue execution.

Read more Automatically output the callstack on a breakpoint in Visual Studio

Understanding C++11 move semantics

If you’re a programmer, you’ll be familiar with the difference between making a copy of an object, and making a reference (or a pointer) to it. If implemented correctly, the former duplicates the data, resulting in two (or more) independent instances. The latter allows the same original data to be accessed in two (or more) different ways. These concepts are common to many languages, and are essential to passing and returning data in your program.

As of C++11, you can think of ‘move’ as being a third alternative. It’s expanded the core features of the language, adding move constructors and move-assign operators to your class design arsenal. It’s required the introduction of a new type of reference as well, known as “R-value references”, which we’ll look at later.

To quote from Scott Meyers’ excellent C++11 training materials, “move is an optimization of copy”. In practical terms it’s very similar to copying, but can offer a number of advantages, especially where copying and referencing are impossible or undesirable. In this post, I’ll try to cover the basics of moves, so you can understand what they are and how you can start using them yourself.
Read more Understanding C++11 move semantics

Improve C++ performance in debug mode with inline expansion

When you’re dealing with an intensive real-time application, such as a game or simulation, a common problem is that debug builds in C++ can run much slower than release builds. This difference in run-time behaviour means it can be hard to reproduce and analyse bugs and other problems. There are several things you can do to improve it, and one which helped me recently was enabling inline expansion (or simply “inlining”).

A quick warning though: it won’t improve performance in all situations, and it can actually hinder debugging. For performance-critical code, you should first try manually optimising your algorithms.

Read more Improve C++ performance in debug mode with inline expansion

Introduction to Win32 Named Pipes (C++)

See also: Win32 named pipe example programs on GitHub.

There are times when it’s extremely useful to be able to pass some data between different programs running on the same system. For example, you might have multiple programs forming part of the same package, and they need to share some important information or work together to process something.

There are several ways to do it, but my choice in a recent task was to use the named pipes functionality of the Win32 API (working in C++). Note that pipes on other operating systems are a little different, so not all of this information is portable. Read more Introduction to Win32 Named Pipes (C++)

FlashDevelop demo — Slider Puzzle

I’ve blogged before about using FlashDevelop to make Flash games for free. Just for fun, I decided to do a very little project tonight… a simple slider puzzle game to challenge myself. I set myself a time target of 3 hours to build the whole thing (assets included) from scratch. Ultimately, I ended up with 3 and a half hours, but I got it working. You can see the result below… Read more FlashDevelop demo — Slider Puzzle