Tag Archives: Visual Studio

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 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

How to Get Visual C++ Sample Projects

Latest versions of Visual Studio come with few or no source code project samples. However, there are hundreds available on Microsoft sites. Here is how to get them.

Install samples using Visual Studio 2012 or 2013

For getting samples from Visual Studio IDE, follow next steps:

  1. On the Help menu, click Samples.
    Get Visual C++ samples - step 1

    Get Visual C++ samples – step 1

    If you have Visual Studio 2012 or Visual Studio 2013, the New Project dialog is open.

  2. Expand left-pane tree to the desired category, e.g Visual C++ / Desktop / Win32 / Graphics and 3D, select a sample project from the list, them click the OK button.

    Get Visual C++ samples - step 2

    Get Visual C++ samples – step 2

  3. In the Download and Install dialog, click on Install button.

    Get Visual C++ samples - step 3

    Get Visual C++ samples – step 3

Now, the sample project is added to the installed samples list, which are available each time you want to have a look inside.

Installed samples

Installed samples

Additionally, each time when open an installed sample project, Visual Studio shows a page containing the sample description, a link to download page, plus links to useful resources and tutorials. That’s pretty cool but notice that Visual Studio gets only the samples written for its own version. To get access to all samples, see the next method.

Download Visual C++ samples from MSDN Developer Code Samples

Here is the magic link: http://code.msdn.microsoft.com/
Click it here or type in a browser and get the main Developer Code Samples page. You can filter the articles according to your interests (programming language: C++, platform: Desktop and so on). Moreover, you can type a keyword to refine the filter (e.g. Direct2D) like in the below picture.

Developer Code Samples page

Developer Code Samples page

All to do next is to click on desired link and download.

Notes

  • If click Help / Samples in a version prior to 2012, Viusal Studio shows a page containing a link to Visual Studio Samples site.
    Visual Studio 2010 Samples

    Visual Studio 2010 Samples

     

See also

Resources

Downloads

Command Link Button Control

What are Command Links?

Among other new controls supported in Windows Vista and newer Windows versions, we can find  Command Link controls, that contain:

  • an icon;
  • a label text;
  • an additional explanatory text.

Command Link Control Example

In fact, it is a common Windows button control (of class “Button“) that has BS_COMMANDLINK or BS_DEFCOMMANDLINK type style set.
We’ll further show how to deal with such type of controls in our own aplications.

How to add a Command Link Button

If working with Microsoft Visual Studio 2008 or newer, then we can find “Command Button Control” in the resource editor toolbox.
So first, we just have to drag it from toolbox in the resource dialog template.

Adding Command Control to dialog template

The label text can be set at design time by changing the Caption property; it can be also changed at run-time by calling CWnd::SetWindowText.
The additional explanatory text has to be set at run-time by calling CButton::SetNote.
The default arrow icon can be changed at run-time using CButton::SetIcon.
Here is an example:

Further, we can work like with any other classic push button, as for example handling BN_CLICKED  notification.

Additional notes

  1. Command Link Button controls are available in Windows Vista and newer.
  2. For for additional environment info, see “Build Requirements for Windows Vista Common Controls” link below.
  3. In raw-WinAPI applications (not using MFC), can use the corresponding Windows SDK functions/macros instead of of CWnd/CButton methods.
    Example:
  4. Command Button Control is available in the resource editor, beginning with Visual Studio 2008. However, you can still use this kind of control in older Visual Studio versions. For this purpose you can:
    • Add to dialog template a regular push button then manually edit the resource script (.rc) file or, a little bit better…
    • …add to dialog template a Custom Control.
      Adding Command Button Control (VS2005)
      As shown in the image, you must set “BUTTON” class property and “0x5001000e” style property (WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_COMMANDLINK).
      Also, in the source code you have to send BCM_SETNOTE message instead of calling CButton::SetNote. For more details, see the attached VS 2005 demo project.

References

Downloads