First, the preface
In the Windows Explorer window, we have seen software such as Winzip,winrar can add a few menu items to the default shortcut menu in a file or folder, which allows users to operate directly in the windows without having to go inside the software, and is very user-friendly. This is certainly a good application model, it is what we call shell extension technology. This article will use a common source code statistics program as an example to illustrate how to implement shell expansion technology. Here's how the program works:
Figure One example code run effect diagram one
Figure Two example code run effect figure two
Ii. Principle of realization
In order to extend the default menu of files or folders in any Windows window, we must have Windows load our program segments in the Display shortcut menu, which we typically use with COM components to achieve this goal. COM components are divided into three kinds: in-process service programs, local service programs, and remote service programs. To get the Explorer to load and execute our code, of course, we have to use an in-process service, which is represented by a DLL, which is mapped to the virtual address space of the executable program after it is loaded, and we provide some interfaces to the explorer that the Explorer will call when the shortcut menu is displayed , we can do something we want to do in those interfaces, such as adding a shortcut menu, Implementing a menu item, and so on, to achieve the shell expansion.
As for source code statistics, it is not difficult to implement. Here I take the C + + style source code as an example, and apply one of the simplest statistical rules, when the statistics file, we read the contents of the code into the cache, to determine whether each character is a newline (\ n), if, the Count plus 1. Of course we are making statistics more meaningful, so we can use the recursive method to traverse all the files in the folder, find a valid file (here I only statistics C + + program, so only processing suffix named. C. Cpp. H's file), according to the previous method one by one statistics can find out all the code in the folder total number of rows.