C++ is fun

We all know C++ is a powerful yet complex programming language. But it can also be fun. For instance, you can use emojis and other Unicode characters in the source code. Of course, nobody sane would use emojis for identifiers. But it’s possible and you can have a little fun if have some spare time.

Identifiers are sequences of digits, underscores, lowercase and upper case Latin letters, and most Unicode characters. Identifiers are not allowed to begin with a digit, they must begin with an underscore, a Latin letter, or a non-digit Unicode character. For more information about the Unicode characters in identifiers see this.

So here is a little example of a C++ program that uses emojis. Can you figure out what it does?

This is a screenshot of the program from Visual Studio:

Can we do better (or maybe worse)? Of course, we do. We can do it the Egyptian style writing C++ in hieroglyphs.

In case that’s too small to read perhaps this screenshot is a bit better for reading:

That’s not so far off from this:

(Source)

If you want to check more about the Unicode block of Egyptian hieroglyphs see Unicode Block “Egyptian Hieroglyphs” or Egyptian Hieroglyph.

This program prints 3 to the console, altough that is harder to figure out because I cheated and didn’t show the whole program. This part actually:

The equivalent readable code of the above example is as follows:

Visual Studio seems to have problems displaying some Unicode characters, although I didn’t figure out the pattern. Sometimes they are OK, sometimes they are not. However, the source code is correct and you can successfully build and run a program written in Unicode.

You do have to save the source code using an encoding that allows you to enter the Unicode characters that you want. To do that you have to go to File > Save as … > Save with Encoding and make the appropriate selection.

If you are using Windows 10, there is an emoji application that you can use to type for them. Just type Win + . or Win + ;.

For more information on this see How to type emoji on your PC using Windows 10 Fall Creators Update or Windows 10 Tip: Get started with the emoji keyboard shortcut.

But how do you display Unicode characters in the Windows console? You need to use a code page that supports them. Here is an example:

The output of this is as follows:

However, the Windows console does not currently support emojis (and, in general, Unicode characters from the Supplementary Multilingual Plane. That support will come in the Windows Terminal, which is currently in preview. You can read more about that here. Excited?


via GIPHY

Of course, C++ is not the only programming language that allows emojis in identifiers. You can see examples from other languages here.

Memeful comments extension for Visual Studio 2019

Some time ago I published a Visual Studio extension called Memeful Comments for displaying images, including animated GIFs, alongside your code to enhance explanations with visuals or simply to express your reaction to the code. This extension is now available for Visual Studio 2019!

Download the extension from Visual Studio Marketplace.

The source code is publically available on GitHub.

In addition to the already supported languages (C#, C and C++, F#, VB.NET, JavaScript and TypeScript) the new version includes support for Python.

Another new feature included in this version is the support for the Visual Studio environment variables $(SolutionDir) and $(ProjectDir) in image URLs. Example:

You can provide feedback here.

Enabling TLS 1.2 in your .NET framework applications

A functionality of one of the products I’m working on suddenly stopped working without any code changes on our side. Our application connects to a web service to get some data and that no longer worked, our customers getting the following error in their logs “The underlying connection was closed: An unexpected error occurred on a send.” The first thing to do was checking whether the web service was still up and running as expected. The requests made with SoapUI or Postman were all successful, so it was actually something in our application that was actually wrong. So I decided to use Fiddler to look at how our requests look and what do we get back. The following showed up in Fiddler:

fiddler.network.https> HTTPS handshake to service.domain.com (for #6) failed. System.IO.IOException Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. < An existing connection was forcibly closed by the remote host

Apart from the time that was off-scale, the piece of info that drew my attention was the version, TLS 1.0. And that rang some bells about discontinuing support for this version of Transport Layer Security in some of the services we are consuming. So that looked like it happened before we had a chance to do something about it in this application that is built with C++ and .NET (the service being consumed from a .NET module).

Our target .NET framework version is 4.6.2 and by default the runtime should default to TLS 1.2. Yet, it did not work. I was able to reproduce with a demo C++ and C# application that uses a .NET class library that connects to the service. When the C# application is running it works well. When the C++ application is running, using the same class library to go to the service, it does not work. And that was the case even when I set the target to .NET framework 4.7.2 (in both the .NET class library and the C++ console project). According to all the Microsoft documents that I could find it should work well for 4.6 and above, but in my case it did not and cannot really explain it. But, there are two ways to solve this problem.

The first method involves making changes in the registry. By setting the Windows Registry key HKEY_LOCAL_MACHINE\SOFTWARE\[Wow6432Node\]Microsoft\.NETFramework\<VERSION>: SchUseStrongCrypto to 1 you enable strong cryptography that uses more secure network protocols and blocks those that are not secure. Yet, according to documentation, when I target .NET framework 4.7.2 in my application it should be implicitly set to 1:

If your app targets .NET Framework 4.6 or later versions, this key defaults to a value of 1. That’s a secure default that we recommend. If your app runs on .NET Framework 4.6, but targets an earlier version, then the key defaults to 0. In that case, you should explicitly set its value to 1.

You can set this by running the following commands in a command prompt running as administrator:

Beware, this will affect all the .NET applications running on the machine, although that shouldn’t necessarily be a bad thing.

The second method requires code changes. You can turn on TLS 1.1 and 1.2 by executing the following line of code:

This should be run before you do any HTTP requests to web services in the application. This line of code does not remove support on the client side for TLS 1.0 but instructs the runtime to negociate the best of a list of available protocols that includes TLS 1.1 and 1.2. Of course, this will affect only your application and not the others running on the same machine.

To read more about this, see the following:

Three productivity features in the Visual C++ 2017 debugger

Visual Studio 2017 has had a larger number of updates throughout its lifetime. At the time of writing this article, there have been ten major updates and countless minor ones. You can check the release notes history for details. Part of the changes was in the debugger. In this article, I will mention three features for C++ development that were introduced at different stages in Visual Studio 2017.

Run to Click (Available since 2017 15.0.)
When you are in the debugger and you hover the cursor over a statement in the editor (or the Disassembly window) a green button appears. By clicking this button you resume the execution until that point. This functionality was already available either through a (temporary) breakpoint or by the Run to Cursor command. But with this contextual button, it is made much simpler.

See more details: Navigating through code with the Visual Studio debugger

Just my code (Available since 2017 15.8.)
This is a feature that avoids stepping into frameworks, system, and other non-user code. This is available for other languages too, but for C++ it requires the /JMC compiler switch (which is enabled by default for new projects).

Here is an example. Suppose you are in the debugger at this line:

Stepping into the function call, you expect to get here:

This is exactly what you get with this feature enabled. Without it, however, the debugger used to step into standard library code, as shown below:

This feature can be enabled from Tools > Options > Debugging > General by selecting Enable Just My Code. In Visual Studio 2019, this feature is enabled by default.

When using this feature, in order to have the code classified as user-code, the PDB for the image containing it must be available to the debugger. When debugging:

  • Step Into (or F11) on non-user code steps over the code to the next line of user code and Step Out (or Shift+F11) on non-user code runs to the next line of user code.
  • when there’s no more user code, debugging continues until it ends, hits another breakpoint, or throws an error.
  • when breaking in non-user code stepping continues in the non-user code.
  • when hitting an exception, it stops on the exception, whether it is in user or non-user code. User-unhandled options in the Exception Settings dialog box are ignored.

You can customize what code should be considered non-user (it’s an opt-out feature) by creating a *.natjmc file (in the %VsInstallDirectory%\Common7\Packages\Debugger\Visualizers folder if you want to change the behavior for all users, or in the %USERPROFILE%\My Documents\Visualizers folder for a particular user). Here is an example, from Microsoft Docs, on how such a file may look like (for details on the format, you should read the documentation):

See more details: Debug only user code with Just My Code

Step back (Available since 2017 15.9.)
This latest feature enables us to debug by returning to a previous point in time during the debugging without restarting the process. How is this done? Behind the scenes, the debugger uses PssCaptureSnapshot, a Windows API, to basically span a child process and take a snapshot of its memory. This is all hidden to the user, but when you step back, the debugging of the current process is actually stopped and the debugger is attached to one of these snapshot processes with access to its snapshot state (its entire virtual memory). When you return to live debugging, the debugger is reattached to the debugged process and proceeds in a normal manner.

Snapshots are taken each time a breakpoint is hit and at each step event, but not sooner than 300ms since the last one. This is done to ensure performance remains reasonable.

To make this work, you must first enable IntelliTrace Snapshots, from Tools > Options > IntelliTrace > General. (Notice that the following screenshots are taken in Visual Studio 2019 Preview, but they are basically the same in Visual Studio 2017.)

With this enabled, the Diagnostics Tool window changes to enable you to take snapshots. You will also see the snapshots that are taken automatically and you will be able to go through them and see the values of your variables in the Auto, Local, or Watch window.

You can also move back and forth through the snapshot history with the Step Backward and Step Forward commands from the debugging toolbar.

See more details: Step Back – Going Back in C++ Time

Putting the fun in C++

The post-Kona mailing list of the new standards papers has been recently published (you can find it here). Going through the titles I could not help smiling because some of them are really funny. So I decided to make a top 10 of the funniest paper titles. Here it is:

<=> != == Barry Revzin
I Stream, You Stream, We All Stream for istream_iterator Casey Carter
Make Me A Module Nathan Sidwell
Byteswapping for fun&&nuf Isabella Muerte
Contracts That Work Joshua Berne, John Lakos
Contracts: why the house is not on fire Ville Voutilainen
Are modules fast? Rene Rivera
Don’t add to the signed/unsigned mess Bjarne Stroustrup
Tell Programmers About Signed Integer Overflow Behavior Scott Schurr
Partial program correctness S. Davis Herring

Don’t say people are not having fun when they write these papers.