I used to have several open-source projects available on CodePlex. At the end of 2017, the site was put in read-only mode and it will probably be closed at some point. Therefore, I have decided to move the projects that could still be useful to GitHub.

MFC Collection Utilities
A small library that enables developers to use MFC containers (arrays, lists, maps) with range-based for loops. The library consists of a single header that you include in your MFC projects.



A header-only C++ template library for manipulating the settings of a Windows console (colors, text position, input mode, etc.)


, , , , Hits for this post: 2673 .

Microsoft recently announced that it released version 15.5 of Visual Studio 2017 (and Visual Studio for Mac version 7.3).

There are various improvements to performance and diagnostics (such as cutting the solution load times for large C# and VB projects by half), new features for C#, C++, F# development, and others. You can read the announcement and the release notes to get the complete list. Concerning VC++, the claim is that it now supports 75% of the C++17 features, although I’d still like to see an update on the supported feature list.

The VC++ compiler supports 75% of the C++ 17 features. In addition, the team has added new optimizations to the compile.

However, looking back at the previous releases I just realized how many have been. I put them all in a table (as far as I figured this should be the complete list). The total is 11 releases within the past 9 months.

Release Update Date Link
15.5 .0 2017-12-04 Release notes
15.4 .5 2017-11-27 Release notes
.4 2017-11-14 Release notes
.3 2017-11-08 Release notes
.2 2017-10-31 Release notes
.1 2017-10-19 Release notes
.0 2017-10-09 Release notes
15.3 2017-08-14 Release notes
15.2 2017-05-10 Release notes
15.1 2017-04-05 Release notes
15.0 2017-03-31 Release notes

That reminds me of this good old quote from the Gladiator movie:

Tell me, what have you been doing, busy little bee?

Tell me, what have you been doing, busy little bee?

The Visual Studio teams have been some busy little bees. Kudos!

, , , Hits for this post: 3781 .

It’s said that a picture is worth a thousand words. An animated GIF is probably worth even more. I have always wanted to show my reaction to code with pictures. Being able to display images along source code could have a lot of benefits beyond that. By inserting images within code we could better explain snippets, visualize algorithms, or other things. Luckily Visual Studio allows us to customize the WPF editor with extensions that actually enable us to do so. Therefore I have created an editor extension for Visual Studio that enables displaying images, including animated GIFs, within the source code. It’s called Memeful Comments and is available for free in the Visual Studio marketplace.

Download the extension from Visual Studio Marketplace.

The source code is publically available on GitHub.

Here is how it works: original source code like this JavaScript snippet

is displayed as shown here when the extension is enabled:

The following languages are supported:

  • C#
  • C and C++
  • F#
  • VB.NET
  • JavaScript and TypeScript

Here is a longer video showing how this works while editing text.

To display images, including animated ones, in your source code in any of the supported languages, you have to add an <image> tag. The following attributes are supported:

Attribute Required Description
url or src Yes Represents the source of the image. It can be either from a local disk or from the web. If the file is from the local disk it can be either absolute or relative to the document path.
scale No Indicates a scaling factor for the image. Notice that a very large scale might make the image impossible to display.

Here are examples in various programming languages:

  • C#
  • C++

  • VB.NET

  • F#

  • JavaScript/TypeScript

Enjoy the extension and send me feedback!

This extension is based on the image-comments extensions by Luke McQuade.
, , , , , , Hits for this post: 5240 .

Four years ago I wrote a blog post that shown how to build a web server using the http_listener from the C++ REST SDK library as well as a client application that consumed the exposed resources. Over the years there have been various changes to the API from the library and some readers complained the code no longer compiled. Eventually, I decided to revisit that post and update my code to the latest version of the library, which at this time is 2.10.

I will not reiterate all the details described in the former article. However, in summary, the server maintains a dictionary of values (both keys and values are strings). Through HTTP calls a client can retrieve the content of the dictionary, add new values, update or delete existing ones.

HTTP method Description Request Response
GET retrieves all the key-value pair from the dictionary {"one" : "100", "two" : "200"}
POST retrieves the values of the specified keys from the dictionary ["one", "two", "three"] {"one" : "100", "three" : "", "two" : "200"}
PUT inserts new pairs of key-values in the dictionary; if a key is already found its value is updated {"one" : "100", "two" : "200"} {"one" : "", "two" : ""}
DELETE deletes the specified keys from the dictionary ["one"] {"one" : ""}

Here is the server code:

And this is the client code:

Notice there are slight changes in the way output is formatted, both in the server and the client application. The rest is mostly unchanged, except for the handling of JSON, that has changed significatly since version 1.1. Again, please see the original post for an explanation of the code.

The output from running these client and server applications is shown below. On the left is the client output, and on the right the server output.

, , , Hits for this post: 4069 .

But why?

Today I wanted to rework an example that I wrote years ago. I was a client-server example using the C++ REST SDK. I haven’t worked with the library for a while, so I needed to install the latest version first.

If you go to the C++ REST SDK project page it says you need to run the following command to install it:

For that, you need the vckpg package management system for C and C++ libraries on Windows. Getting and installing it is not that hard: you have to either clone the git repo or download and unzip it locally. Then you need to run two commands:

So I did all that and then installed the C++ REST SDK. It took about one hour and 11GB (!!) of disk storage for this. It had to download, build and install the following libraries:

  • boost:x86-windows
  • boost:x64-windows
  • bzip2:x86-windows
  • bzip2:x64-windows
  • cpprestsdk:x86-windows
  • cpprestsdk:x64-windows
  • openssl:x86-windows
  • openssl:x64-windows
  • websocketpp:x86-windows
  • websocketpp:x64-windows
  • zlib:x86-windows
  • zlib:x64-windows

This is utterly insane. All that I want is some headers and a few static and dynamic libraries. But instead I got 11GB of God knows what and waited about one hour before I could start to work.

Why is all this necessary? Why do I have to care about all this provisioning process? We don’t care about your time and resources, we’re cool we can do this with scripts? Seriously? What happened to the good old fashion of downloading an archive with everything you needed already built for you? Now you first need to install 400MB of Perl to be able to build and get going with a C++ library. It’s sad.

, , Hits for this post: 3323 .

In the previous post, I have shown how you could write a simple Twitter bot using JavaScript and Node.js very quickly. However, I ran the bot on my local machine, which is probably not something that you want to do for a real application. Instead, you’d probably want to host it on a cloud platform. In this article, I will show how you can do that also in no time using Heroku. Heroku is a cloud platform for building and running applications that takes away the complexity of setting up an entire infrastructure. This makes it well suited for simple things like the bot we previously created.

A word on Azure

Since I have an Azure account and MSDN subscription my first choice for hosting the bot was Azure. However, I learned something important.

After wasting too much time looking into both web apps and Azure Cloud Services, installing Azure Power Shell, learning to use it, setting up worker roles, deploying, etc. and still not getting the bot to work (unless accessed through a URL) I decided to move on and find something that even someone unfamiliar with can quickly grasp in minutes.

Starting with Heroku

Heroku is a platform that you can use for free for experimenting if you don’t require too many resources. Of course, there are paid plans, depending on your needs. However, for the simple bot I was building there would be no incurring costs.

Pricing is based on dyno hours. Dynos are lightweight Linux containers in which an application runs. A dyno can belong to one of the three possible configurations: web (that receive HTTP traffic from routers), worker (typically background jobs, queueing systems, and timed jobs), and one-off (temporary dynos that can run detached, or with their input/output attached to your local terminal, typically used for administrative tasks, such as database migrations and console sessions). You can get up to 1000 dyno hours per month for free, which is enough to run an application with no costs. Should you need to host more, then you’d have to pay for it.

After creating the account, you should download and install the Heroku CLI (previously known as Heroku Toolbelt). This allows you to create and manage Heroku apps from the command line on Window, MacOS and Debian/Ubuntu.

Creating a Heroku app

After creating an account and signing in you can create applications from the dashboard. You must specify a unique name and a data center where it should be hosted.

Heroku has its own git to store your project sources. You can find instructions under the Deploy page for how to create the project locally and commit it to Heroku’s git repository.

Defining a Procfile

Apart from the bot source files that we created in the previous article (package.json, bot.js, and others that might be necessary), we need to declare a Procfile in the root directory of the app. This is a text file that defines what command should be executed to start the application. The content of this file should be the following (assuming that the entry point of the bot is called bot.js).

This is necessary because the Twitter bot is not a web app, but a worker app that runs on the server and does something periodically in the background, not based on HTTP requests.

After uploading the source files including the Procfile to the heroku git repository, go to the application and under the Resources tab, stop the web dyno and start the worker dyno.

Note: a web dyno is created by default for every application.

And that’s it, the Node app now runs successfuly on the platform in the background.

See more

Deploying Node.js Apps on Heroku
Getting Started on Heroku with Node.js
Dynos and the Dyno Manager

, , , , , , , Hits for this post: 3988 .

I had recently played with the idea of creating a twitter bot, not for any specific reason but to see how hard or easy that would be. And to do something that would put a bit of fun into the experience I decided to make a bot that would create nice tweets with emoji of bugs. Here is an example:

It turned out that creating such a bot is a no time consumer when done in JavaScript with Node. The result of my experiment is available here: https://twitter.com/emojibugs.

This article explains the necessary steps to build a simple Twitter bot that posts messages periodically, at some pre-defined interval.

Twitter account and app

The first thing to start with is a Twitter account. This could be either an existing account or a new one.

After setting up the account the bot should be tweeting on behalf of you need to go and create an application. That can be done from https://apps.twitter.com/. You need to create a new app, provide a name, description, website and other optional ones.

After the app is created you need to go to Keys and Access Tokens and generate them for the application. These will be used later on to sign it to Twitter from the bot, and specifically:

  • Consumer key (API key)
  • Consumer secret (API secret)
  • Access token
  • Access token secret

NOTE: should these keys and secrets be compromised new ones can be generated (which invalidates the previous ones).

Creating a bot in JavaScript

A very simple solution for a simple bot like this is creating a Node package in JavaScript. Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine that enables running JavaScript code outside the browser. Node comes with npm, which is a package manager for JavaScript.

Download and install Node.js: https://nodejs.org/en/
Learn more about npm: https://www.npmjs.com/

There is a Node package for the Twitter client API. It’s called twit and is very easy to use. The following is the only JavaSCript code you need to write to post a new tweet with the text “hello world!” on the registered account.

What you need to do here is fill in the API key and secret and the access token and secret with the ones generated for your app.

Creating a Node package

To create a new package go to an empty folder where you want to locate the package and run the npm init command in a console. This will prompt you to provide some information (some of which are already filled in for you).

There result is a file called package.json that describes the Node package and its dependencies.

To install the twit package for the Twitter client api run the following command in a console in the folder where the package is located:

The twit module and all its dependencies will be downloaded in this folder and the package.json file updated with the dependency:

Writing the bot

When we created the Node package we specified that the entry point should be a file called bot.js. We need to create this and fill it with the following code:

The function generateText creates the text to be posted in a tweet. It can do anything and for the sake of this post its content is irrelevant. Function tweetIt does a status update with a text generated by generateText. This function is executed both when the package is run the first time as well as every six hours (a timer is created with setInterval).

Running the bot locally

You can run the script locally in two ways:

  • By running the following command:
  • By adding a script to package.json and execuring that script.

    And then executing the following command:

It’s done

That is all that is necessary for a Twitter bot to run and post to a Twitter account. Of course, there are things that have been let aside, such as how to generate the content of the message, how to host the bot on a cloud platform, or how to write a bot that does more that posting text, such as posting images, replying, etc. But some of that in the next article.

, , , , , , Hits for this post: 4130 .

Computing day of year in C++

I have been recently asked on my post on the date library if the library has a function for computing the day of the year. It actually does not, although it is fairly simple to compute it.

UPDATE: Howard Hinnant has shown in a comment below how to write a day_of_year() function using the date libray.

Let’s look at the days of the year.

Day Day of year
January 1 1
January 2 2
January 31 31
February 1 32
February 28 59

Here is where things complicate a bit, because during leap years February has 29 days. So we actually need to have two counts of days.

Day Day of non-leap year Day of leap year
January 1 1 1
January 2 2 2
January 31 31 31
February 1 32 32
February 28 59 59
February 29 N/A 60
March 1 60 61
December 31 365 366

It is fairly simple to compute the day of the year based on the day of the month if we knew the day of the year of each first day of the month. That can be also put in a table.

Day of month Day of non-leap year Day of leap year
January 1 1 1
February 1 32 32
March 1 60 61
April 1 91 92
May 1 121 122
June 1 152 153
July 1 182 183
August 1 213 214
September 1 244 245
October 1 274 275
November 1 305 306
December 1 335 336

So we can compute the day of the year as:

We can simplify that a bit by subtracking 1 from the day of the year values in the table above, such that January 1st is the day 0, February 1st is day 31, etc.

The following code sample shows how this can be written in C++:

And how it can be used:

This day_of_year() function can be used with the date library too. I’ll just add one more utility function that takes a date::year_month_day value and returns the day of the year.

And we want to know what day of the year today is then we can do that too:

The day_of_year() function is very simple and does not do any argument checks. That makes it possible to compute dates such as 2017.08.55 or 2017.55.100. Obviously, these not only that do not make sense, but indexing the days_to_month array beyond its bounds is undefined behavior. That means that in practice you should write a function that validates the arguments and throws an exception upon error. However, in this case, the day_of_year() can not be constexpr anymore.

This would throw an exception on dates like 2017.13.1 or 2017.1.50, but would not do so for 2017.2.30 or 2017.11.31 that are also invalid dates. That can be further corrected by verifying that the day of the month does not exceed the number of days that month can have in the given year.

, , , Hits for this post: 6837 .

You may have multiple versions of the .NET framework installed and used on your machine. The framework has two components: the set of assemblies that provide functionalities for your application, and the common language runtime (CLR) that handles the execution of the application. These two components are versioned separately. If you what to see what versions of the framework are installed, you have to query the Windows Registry. If you want to know what versions of the CLR are installed you could either use clrver.exe or do it programmatically. In this article, we will look at this later option and how to do it in C++.

How to: Determine Which .NET Framework Versions Are Installed

To query the installed CLR versions from C++ we have to:

In order to call CLRCreateInstance we must include the metahost.h header and link with the Mscoree.lib static library.

To use the ICLRMetaHost and ICLRRuntimeInfo interfaces we must import the mscorlib.tlb type library. The _COM_SMARTPTR_TYPEDEF are used for defining COM smart pointers ICLRMetaHostPtr and ICLRRuntimeInfoPtr that automatically handle the reference counter of the underlying COM object.

The call to the EnumerateInstalledRuntimes method, when successful, returns a pointer to an IEnumUnknown interface. This enables enumerating through a component that contains multiple objects. Its method Next retrieves a specified number of items. In this implementation that number is 1. The return value is a pointer to the IUnknown interface, but what we are enumerating through are actually ICLRRuntimeInfo interfaces.

To retrieve the version info we must use the GetVersionString method of ICLRRuntimeInfo. The arguments are an array of wide characters that will receive the string and the size of the array. In order to retrieve the necessary size of the buffer, we have to first call the method with null for the first argument. In this case the function returns ERROR_INSUFFICIENT_BUFFER as a HRESULT (i.e. HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER)) and sets the second argument to the necessary size. After allocating the necessary buffer, we call the method again, providing the buffer and its size.

Running this program on my machine prints the following (which is the same as the output from clrver.exe).

, , , Hits for this post: 7220 .

Firebase is a platform for developing mobile and web application that provides analytics, authentication, real-time databases, notifications, cloud messaging, crash reporting and other services for your application. In this post, I will discuss how to use the Firebase Analytics in Cordova applications for measuring app usage and user engagement.

Analytics for Firebase

This is a free service that can be used for both Android and iOS applications. It provides unlimited reporting of up to 500 distinct events and a number of user properties.

Events allow you to see what happens in an application, such as user actions, system events, or errors. There is a number of automatically collected events and you can define your own events. However, you have to keep in mind that:

  • there is a limit of 500 distinct events that can be reported.
  • each event can have up to 25 parameters associated with it.
  • event names can be up to 40 characters long, may only contain alphanumeric characters and underscores (“_”), and must start with an alphabetic character.
  • “firebase_”, “google_” and “ga_” prefixes are reserved and cannot be used.
  • names are case-sensitive and logging two events whose names differ only in case will result in two distinct events.

See FirebaseAnalytics.Event for more information.

User properties are information about the user that is recorded together with the events. Default collected user properties include user info (age, gender, country, language, interests), device info (brand, category, model) and app info (version, app store, first open time, new/established).

Appart from the implicitly collected user properties you can define your own. For that, however, you must use the Firebase console. You must be careful with the properties that you add because for now these cannot be renamed or deleted (you can only change their description).

Getting started

To be able to use Firebase for your Android or iOS app you need to enroll it. For that, you must create a project and then add applications to it.

  1. Open the Firebase console.
  2. Create a new project (or select an existing one).
  3. Add a new Android, iOS or web app.
  4. Follow to instructions to register the app, download the config files or perform other necessary setups.

If you need more help for setting up your app read the Firebase documentation or additional web resources.

Cordova plugin

There is an official Cordova plugin called cordova-plugin-firebase that provides push notifications, analytics, event tracking, crash reporting and other services from the Firebase platform for you Cordova application.

When you start using Firebase, make sure you have the config files properly setup (present and valid). Otherwise, your app will crash on boot or Firebase features won’t work.

Recording events and user properties

To record a user property you should call setUserProperty. This has the form:

The name is the case-sensitive name you defined in the Firebase console. In the example above the name was login_type and possible values were demo and live. The purpose of this is to indicate that for an application that supports a demo mode where the users don’t have to create an account to try the app, the user is either properly authenticated on or actually using the demo feature.

When this call is made the Debug view shows the change of the user property value in the live stream.

If you look at the recorded user properties for all following events you will see the logon_type with the demo value attached to it.

As mentioned ealier, there are a series of events that are automatically recorded. These include screen_view (collected whenever the page has changed) and user_engagement (collected periodically when the app is in foreground). However, you can send any other event, each with any number of parameters. To record events you must use the logEvent function that has the following form:

Let’s suppose that in your app users can bookmark items (whatever those items might be) but also remove them from the bookmarks (or favorites). You want to record the event of adding and removing an item. You can define an event called favorites_action with two parameters: action that can be either add or remove, and type, that indicates what kind of item was added or removed.

These are added automatically to the stream of events, recorded with both the the auto collected and user defined parameters and user properties.

All the events have several implicit parameters, including firebase_screen, firebase_screen_class, and firebase_screen_id. Screen class is the class name of the Activity, and the ID is a numerical ID that is probably hard to use to figure where the event came from. The screen name on the other hand (firebase_screen) is what you probably want to look at. This can be set with a call to setScreenName. All events will have the param firebase_screen set to the last value passed to setScreenName. Therefore, you probably want to make this call whenever the user opens a new page in your application. The call has the form:

Here is an example:

Debugging Firebase Analytics

The events recorded by Firebase are not instantly sent to the servers. Instead they are cached, archived and periodically dispatched. However, you can enable live streaming of events for debugging purposes, both on Android and iOS. This was used for all the screen shots used previously.

For Android devices:

  • To enable execute:
  • To disable execute:

For iOS devices:

  • To enable specify -FIRDebugEnabled as an argument in XCode
  • To disable specify -FIRDebugDisabled as an argument in XCode

For more information see Debugging Events.

, , , , , , Hits for this post: 7574 .