Tag Archives: Windows API

Three Ways to Find Files

Let’ say we have to implement a function that search a folder to recursively find files having an extension from a given list of extensions. This article shows three possible implementations: one using FindFirstFile and FindNextFile WinAPI functions, one using CFileFind MFC class and one using Filesystem Library.

Find files using FindFirstFile and FindNextFile WinAPI functions

Althugh this implementation uses some ATL stuff to make things easier, it appears still quite complicated.

Find files using CFileFind MFC class

Using CFileFind MFC class can make code a little bit shorter and the programmer’s life easier.

Note: IsStringInListNoCase searches a list for a string, not case sensitive. You can find its implementation in the attached demo solution.

Find files using Filesystem Library

Here it is:

Hurray, we did it in just few lines of code! :)
Of course, it can be used in MFC, ATL, Win32 and Console applications, as well.

Demo solution

The demo solution contains three projects, each having one of the above implementations.
Download: Find Files Samples (Visual Studio 2015).zip (32)

Resources and related articles

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.

Codexpert – 2014 Articles Summary

Microsoft Libraries

C++ Language

Windows Tips

See also

How to Check the Windows Version

Let’s say we have to check if our application is running under Windows 8.1 or newer. A “classic” way, often found in legacy code, is to call GetVersion or GetVersionEx.

Using GetVersion

Using GetVersionEx

However, if running under Windows 8.1 the above functions may return FALSE.
Moreover, compiling in Visual Studio 2013 Update 3 or newer, we can get warnings or even errors if SDL checks compiler option (/sdl) is set.

That’s because with the release of Windows 8.1, the behavior of the GetVersion and GetVersionEx has changed. Additionally, the MSDN documentation says that GetVersion and GetVersionEx may be altered or unavailable for releases after Windows 8.1. Instead, we still can use VerifyVersionInfo.

Using VerifyVersionInfo

Much easier and recommended in MSDN documentation, is to use Version Helper functions defined by VersionHelpers.h, which is included in the Windows 8.1 SDK. In our case we can simply call IsWindows8Point1OrGreater.

Using Version Helper functions


  • We can still use the deprecated GetVersion and GetVersionEx returning correct version under Windows 8.1 by setting Windows 8.1 target in the manifest (see Targeting your application for Windows 8.1 topic in MSDN). However, using Version Helper functions is easier and probably more reliable for the future versions.

Resources and related articles

Tree-View Control – TVS_EX_RICHTOOLTIP style

Windows Vista introduced a buch of extended styles for tree-view control (SysTreeView32). Among them, there is TVS_EX_RICHTOOLTIP style which is described in MSDN documentation as follows: “Allow rich tooltips in the tree view (custom drawn with icon and text)”. This article shows a little bit more about how to set and which is the effect of using this extended style.

How to set and remove TVS_EX_RICHTOOLTIP style

Like the other tree-view extended styles, TVS_EX_RICHTOOLTIP can be set or removed as follows:


Using TreeView_SetExtendedStyle macro

Calling CTreeCtrl::SetExtendedStyle (MFC)

Further, let’s use the above code and see what’s happen.

The effect of TVS_EX_RICHTOOLTIP style

By default, if the mouse pointer hovers a partially visible tree item, a tootip containing the item’s text is shown.

Tree item tooltip - TVS_EX_RICHTOOLTIP not set

Tree item tooltip – TVS_EX_RICHTOOLTIP not set

If the TVS_EX_RICHTOOLTIP style is set, then the tooltip displays the item’s icon in addition to the item’s text.

Tree item tooltip - TVS_EX_RICHTOOLTIP set

Tree item tooltip – TVS_EX_RICHTOOLTIP set

Note that TVS_EX_RICHTOOLTIP has no effect if TVS_INFOTIP style is set. In this case, the tooltip shows the text provided in TVN_GETINFOTIP notification handler and no icon. So far, that’s all I found about it. If somebody can reveal more, please do not hesitate to leave a reply!


Demo project (Visual Studio 2013) – TVS_EX_RICHTOOLTIP_Demo.zip (574)


Listing Windows Known Folders

If the target system is Windows Vista, Windows Server 2008 or newer, enumerating known folders (formerly known as special folders) like Desktop, Documents, Program Files, etc is quite easy by calling GetFolderIds method of IKnownFolderManager interface. It gets an array of all registered known folder IDs (KNOWNFOLDERID). Once having a folder ID, we can call IKnownFolderManager::GetFolder to get an instance of IKnownFolder interface and further get more details.

To make things even easier, I wrote two MFC-extension classes CKnownFolderManager and CKnownFolder, which are wrappers over IKnownFolderManager and IKnownFolder, respectively. Here, let’s just see brief code examples. The implementation details can be found in the attached demo project.

Using CKnownFolderManager to get an array of known folders IDs

Using CKnownFolder to fill a lisbox with known folder names


Using CKnownFolder to get special folder details

Demo project

Download: Listing Known Folders Demo.zip (793)

The demo project is a simple MFC dialog-based application that uses CKnownFolderManager and CKnownFolder. In the left side is a lisbox which displays the list of known folders names. For each selected listbox item, the edit control from the right side displays the details (name, category, path, etc). Additionally, if the user pushes the “Browse…” button, a browse for folder dialog is shown, having the selected known folder in the root.

Listing Known Folders

Listing Known Folders


Codexpert – 2013 Articles Summary

C++ Language and STL

Microsoft Libraries (Windows API, MFC, ATL, WIC, WMI, etc.)