Category Archives: Visual Studio

Codexpert – 2016 Articles Summary

C++ Programming Language

Microsoft Libraries

Visual C++

See also

How to Get Visual C++ Sample Projects (2)

In a previous article I showed how to get Visual C++ sample projects either by using Visual Studio IDE or by downloading from MSDN – Developer Code Samples site. Recently, I found that we can also download an archive containing a very large number of sample projects from GitHub. Pretty cool!
Here is the magic link: Microsoft – Windows Classic Samples.

Visual Studio 2015: How to Step into MFC Framework Code

Let’s say we have installed Visual Studio 2015 and  have started an MFC application in DEBUG mode. Now, attempting to step into an MFC Framework function, e.g. CWinApp::InitInstance, the debugger simply steps over.

Step into MFC Framework failed

Step into MFC Framework failed

Having a look into Output window, we may discover a message like this: “…’C:\Windows\System32\mfc140ud.dll’. Cannot find or open the PDB file”. That’s clear, the necessary PDB file is missing. What can we do? We can buy one from Microsoft or get a free copy from Torrents site. :) Well, don’t need to do that, I was just joking. We can easily get it from Microsoft Symbol Servers, using Visual Studio. Here are the steps:

Changing Debugging/Symbols options in Visual Studio

  1. Open the Options dialog (choose Tools/Options… menu item).
  2. Expand the tree from the left pane to Debugging/Symbols.

    Default debugging symbols options

    Default debugging symbols options

  3. Under Symbol file (.pdb) locations, check Microsoft Symbol Servers. In this moment a message box appears; it suggest that we can choose to get only the debugging symbols for modules which we want, e.g. mfc140ud.dll

    Debug performance message

    Debug performance message

  4. …so let’s check Only specified modules, then click on Specify modules link and add mfc140ud.dll to the list.

    Symbols to load automatically

    Symbols to load automatically

  5. Type or browse for a folder for caching the symbols, e.g. C:\Symbols . Here is how the debugging symbols options finally looks.

    Debugging symbols options

    Debugging symbols options

  6. Hit OK to close the Options dialog.

Notes

  • The same steps can be applied for Visual Studio 2013, except that it needs the symbols for mfc120ud.dll.
  • First time the PDB file is needed it may take a pretty long time for downloading. However, next times it is taken from the cache folder, so symbol loading time becomes irrelevant.
  • If have Visual Studio 2015 with at least Update 1, still may not step into the MFC code, even the symbols has been successfully loaded. For this issue see the next topic.

Changing Linker/Debugging project properties

The Update 1 for Visual Studio 2015 comes with /DEBUG:FASTLINK liker option. That’s pretty cool for improving link times but unfortunately, if it’s set, it makes not possible stepping into the MFC Framework code, although the necessary symbols has been loaded. So let’s change it, following these steps:

  1. Open the project’s Property Pages.
  2. Choose Debug configuration and All Platforms.
  3. Expand the tree from left pane to Configuration Properties/Linker/Debugging.
  4. Change Generate Debug Info option from Optimize for faster linking, (/DEBUG:FASTLINK) to Optimize for debugging (/DEBUG).

    Optimize for debugging

    Optimize for debugging

  5. Hit OK to close project’s Property Pages.

Notes

  • The above option can also be changed for all projects in the Property Manager window.

Resources and related articles

Using Lambdas in MFC Applications – Part 3: Dealing with C++ REST SDK

C++ REST SDK (also known as Casablanca) offers support for HTTP client/server, JSON, URI, asynchronous streams, WebSockets client, oAuth and PPL Tasks.
We can get C++ REST SDK from CodePlex  or use the one shipped with Visual Studio 2013.
This article gives suggestions of how to set up a Visual C++ project (particularly one that uses MFC) in order to be able to deal with Casablanca library. It also shows some simple sample code and a demo MFC project.

Set up the project for using C++ REST SDK

First, must find the location where C++ REST SDK is installed. With Visual Studio 2013 installed, I found it in the following path:
C:\Program Files (x86)\Microsoft SDKs\Cpp REST SDK for Visual Studio 2013\SDK\ which is similar to $(ExtensionSDKDirectoryRoot)\Cpp REST SDK for Visual Studio 2013\SDK.
Next, create a new MFC project or open an existing one, right click on it’s name, choose “Properties” from the context menu, then do the following:

  1. Choose All configurations in the Configuration combo box.
  2. Go to Configuration Properties / C/C++ / General and add $(ExtensionSDKDirectoryRoot)\Cpp REST SDK for Visual Studio 2013\SDK\include to Additional Include Directories list.
  3. Go to Configuration Properties / C/C++ / Command Line and add -Zm200 in Additional Options field; this is necessary to avoid C3859 compiler error when include Casablanca headers in stdafx.h.
  4. Go to Configuration Properties / Linker / General and add $(ExtensionSDKDirectoryRoot)\Cpp REST SDK for Visual Studio 2013\SDK\lib\$(PlatformTarget)\$(Configuration) to Additional Library Directories list.
  5. Go to Configuration Properties / Linker / Input and add casablanca120.lib to Additional Dependencies.
  6. Also in Configuration Properties / Linker / Input add casablanca120.dll to Delay Loaded DLLs list; this is necessary to avoid false memory leaks reported by the MFC framework;
  7. Go to Configuration Properties / Build Events / Post-Build Event and add the following command to Command Line field
     copy /Y “$(ExtensionSDKDirectoryRoot)\Cpp REST SDK for Visual Studio 2013\SDK\bin\$(PlatformTarget)\$(Configuration)\casablanca120.dll” “$(OutDir)”; this copies the necessary Casablanca DLL from its install folder in the output directory of our project.
  8. Close the project property pages dialog.
  9. In stdafx.h include all necesary Casablanca headers, e.g. http_client.h, json.h, pplxtasks.h and so on.

Now, we are ready to go on and use C++ REST SDK stuff in our own MFC application.

Notes

  • Remember that other versions of Visual Studio may install C++ REST SDK in other location!
  • May be other methods to set up the project paths, as for example by using Property Manager and/or edit .props files; I just presented one presumed simpler.
  • Ones would prefer to get C++ REST SDK from CodePlex; probably, I’ll describe this alternative in a future article.

Next is some code sample. You can notice it uses lambda expressions and for that reason I included this article in Using Lambdas in MFC Applications series.

Using lambdas and C++ REST SDK with MFC – sample code

This sample sends a JSON structure in URI query part to a site which gives in response also a JSON which says if the sent one is valid or not and gives an error description if necessary.

More details about project settings and source code can be found in the attached demo project.

Using lambdas and C++ REST SDK with MFC – demo project

Download: JSON Verifier - Demo Application using Casablanca.zip (690)

JSON Verifier - Demo Application using Casablanca

JSON Verifier – Demo Application using Casablanca

References and related articles

Binary literals and digit separators

The C++14 standard provides two new small features to the language: binary literals and digit separators. They are already available in Clang 3.4 and GCC 4.9 and now Visual Studio 2015 RC has implemented them. They may not be something you can’t live without, but sometimes it’s convenient to have them. Let’s have a look.

In C++ it was possible to input integer literals in several bases: decimal, hexadecimal and octal. However, the binary base has been omitted (though other languages supported that). With C++14 binary is also supported and binary literals are introduced with the prefix 0b or 0B.

Binary literals can be used anywhere integral literals are expected.

What if you had to write large literals such as 0b101111000110000101001110, which is the binary representation of decimal 12345678? Sometimes it is convenient to separate groups of digits for more human readability.

C++14 defines the single quotation mark (') as a digit separator in integral and floating point literals. So the binary literal 0b101111000110000101001110 can be expressed in several ways:

or maybe in hexadecimal:

The position of the single quotation marks in the integral or floating point literals is irrelevant, they are simply ignored when determining the value of the literal. All the following are equivalent (but probably only the first and the last make sense).

Using Lambdas in MFC Applications – Part 2: Replacing Callback Functions

According to C++11 Standard, stateless lambdas, i.e. having an empty lambda introducer or capture no variables, are implicitly convertible to function pointers. Visual C++ in Visual Studio 2012 and newer, supports this feature. Moreover, in Visual C++ stateless lambdas are convertible to function pointers with arbitrary calling conventions. This is great if have to deal with Win32 API functions, most of them using __stdcall calling convention.
Let’s begin with two simple examples of enumerating top-level windows, one by using a callback function and the other by using a lambda expression.

Enumerate windows using “classic” callback functions

Usually, for that purpose we declare and implement as callback, a static member function having the signature required by EnumWindows in its first argument.

Notes

  • CALLBACK is defined in Windows SDK as __stdcall.

Enumerate windows using lambda expressions

The same result can be achieved by using a lambda expression. This is somehow simpler because does not require to declare and define a static member function.

Notes

  • as stated in the introduction, there is not necessary to explicitly convert the lambda expression to the function pointer required by EnumWindowsProc (WNDENUMPROC);
  • as stated in the introduction, there is not possible to capture neither local variables, nor this pointer; luckily, like in many other WinAPI callbacks, we can pass the necessary stuff in a parameter of type LPARAM or LPVOID.

Using nested lambda expressions

Let’s say we want to enumerate the top-level windows in a worker thread. We can write something like this:

We have passed lambdas both for callback parameter of AfxBeginThread and EnumWindows. EnumWindows is called in the first lambda body and takes a lambda as well.
This compiles with no problem in x64 builds, but may give a conversion error for the inner lambda in Win32 ones. That’s a little glitch which AFAIK has been fixed in Visual Studio 2015.
However, to be sure it compiles also in Visual Studio 2012/2013 – Win32 builds, we can simply cast explicitly the inner lambda.

Notes

  • x64 builds has no __stdcall calling convention; __stdcall keyword is still accepted but simply ignored by the compiler.
  • of course, we can use lambda expressions in the same way also in ATL applications or C++ programs that use plain WinAPI.

Resources and related articles

Using Lambdas in MFC Applications – Part 1: Sorting Arrays

Beginning with Visual Studio 2010 which supports lambda expressions introduced by C++11 standard, you can handily sort an MFC array like in the following example:

Sorting CStringArray by using a lambda expression

Of course, you can write similar code for other types of MFC arrays like CArray, CUIntArray, and so on.
But also you can easily write a kind of “generic lambda” in order to sort any type of MFC arrays.

Using decltype to sort any type of MFC array

That’s pretty cool… However would be nice to be possible to get rid of “wordy” constructions like “decltype(*arr.GetData())” in the lambda’s formal parameters list. Good news! There is a proposal for next C++ standards: using auto type-specifier in order to make generic lambda expressions (which accept any type of arguments). And that is already supported in Visual Studio 2015.

Using generic (polymorphic) lambda expressions

Notes

  • Some people may claim that using MFC collection classes is obsolete and must use STL containers instead. That’s an old subject of arguing but it’s not in scope of this short article.
    It simply presents how to sort MFC arrays by using lambda expressions.

References and related articles