Application of Shell extension technology based on COM interface

Source: Internet
Author: User
With the continuous improvement of technology, Microsoft Windows has become the mainstream operating system in the field of personal computer applications. Windows operating system provides users with a favorite graphics

Preface:With the continuous improvement of technology, Microsoft Windows has become the mainstream operating system in the field of personal computer applications. The Windows operating system provides a user-friendly graphical user interface (GUI). Microsoft retains the scalability for this Windows user interface, it enables 32-bit Windows applications to enhance the operating environment (also known as Shell) provided by the system in multiple ways ). By extending the shell, developers can provide users with other file object operations, or simplify the browsing of file systems and networks, or you can call tools that process various objects in the file system more conveniently. To illustrate the shell extension technology based on the COM interface technology, this article first compiled a simple audio player, and then compiled several shell extension processing programs to facilitate user operations on the specified file.

I. shell programming method

There are two ways to compile Shell extension applications. The first one is Shell extension that can be implemented without programming, you only need to modify the corresponding registry entries so that the Shell can act as needed. The second Shell extension needs to be implemented by programming. its function is much more powerful than the first Shell extension. the Shell extension mentioned in this article refers to this one.

In Windows, the Shell extension handler is based on the COM interface. it interacts with the Windows Shell normally through a specific COM interface. To allow Windows Shell to find and interact with the extended processing program, the extended processing program of Windows Shell must follow certain rules, this rule is also the design concept to be followed when writing Windows Shell extended processing programs. This rule includes two aspects: (1) the Windows Shell extension handler should register itself at a predefined location in the registry so that Windows Shell can find it;

(2) the Windows Shell extension handler should implement several specific COM interfaces known by the Windows Shell for normal interaction with the Windows Shell.

To implement Shell extension by programming, follow these steps: (1) create a server project (usually DLL); (2) add a COM class for the project to implement a specific Shell interface; (3) implement a class factory for the COM class; (4) implement some framework code for the server; (5) compile the link to generate the COM server; (6) edit the required registration file; (7) test and debug Shell extensions.

II. Shell Extension programming example

As the basic steps for compiling the shell extended handler are the same, this article only provides detailed procedures for compiling the context-related menu Handler. Context-related menu of the file class, when you right-click an element in the Shell namespace (such as a file, directory, server, workgroup, etc., this article specifically refers to the AC-3 audio files ), shell will generate default context-related menus for such elements (note that the default menus generated by different file objects are different), and Shell will search the registry, loads the context-related menu extensions registered by these file objects, so that these extensions can add custom menu items to the generated context-related menus. If you want to add a context menu extension for a file class object, you need to register it at the following location in the Windows Registry:

HKEY_CLASSES_ROOT

<. Extension >=< File Class Description>

......

<File Class Description>

Shellex

ContextMenuHandlers

ContextMenuHandler_Name = {GUID of the context-related menu handler}

It should be noted that you also need to register the context-related menu Handler so that Shell can know where to find the handler:

HKEY_CLASS_ROOT

CLSID

{The context-related menu Handler GUID }=< handler description>

InProcServer32 = <full server path>

"Threading Model" = "Apartment"

After this problem is solved, the remaining problem is the interaction between Windows Shell and context-related menu handlers. Therefore, the context-related menu Handler needs to implement two interfaces known to the Shell in the code, IContextMenu interface and IShellExtInit interface. The IShellExtInit interface has only one member function, namely Initialize. when the Shell decides to call the context-related menu handler for the selected call, it will first call the Initialize method of the IshellExtInit interface, to require the handler to initialize itself. The IContexMenu interface defines three methods: QueryContexMenu, InvokeCommand, and GetCommandString (). When you right-click a file object, Shell displays its context menu.

At this time, the system sends the address of the context menu of the file object to the context menu Handler. However, this address is only used in the processing program to add menu items to context-related menus, rather than modifying or deleting existing menu items, because there may be other processing programs that add food items to the menu before or after this. Finally, when all context-related menu handlers are called, Shell adds its menu options to the menu. The menu items in the menu can be related to a specific class (that is, all files of a certain type) or related to a specific case (that is, only applicable to a single file object ).

When Windows Shell displays the context-related menu (or File menu on the menu bar) for a File object, the system calls the QueryContexMenu member function of the IContextMenu interface in the context-related menu Handler. In this case, the context menu Handler can call the InsertMenu function to add the menu options to the column context menu by position (MF_POSITION. The added menu item can be a common string (MF_STRING) or a bitmap (MF_BITMAP ). When a user selects a menu option maintained by the context-related menu Handler, Shell will call the InvokeCommand member function of the IContextMenu interface of the handler so that the handler has the opportunity to process the commands selected by the user. If there are multiple context-related menu handlers for a type of file registration, the order of each command is determined by the ContextMenuHandlers keyword in the file class.

The specific compiling process of context-related menu Handler is as follows:

(1) create an empty DLL project

The context-related menu Handler will still exist in the form of a COM server in the process. Therefore, you need to create a DLL project that should be empty for the context-related menu Handler. in this article, the project is named ContextMenuExt;

(2) add a class CContextMenuExt to the project

First, consider the functions to be implemented by DLL. the context-related menu Handler needs a class to implement the COM interface IContextMenu and IShellExtInit. Therefore, the author adds a class CContextMenuExt to the project. After the CContextMenuExt class is added, we can see from the project file view that two files are added in the project, which are the header files (CContextMenuExt. h) and source code files (CContextMenuExt. cpp ). Next, modify the CContextMenuExt class declaration to inherit the IContextMenu and IShellExtInit interfaces, and add three member variables m_cRef, m_pDataObj, m_szFileName, and a protected member function. In the public declaration section of the CContextMenuExt class, the constructor and destructor of the class and the member functions set to implement the IUnknown, IContextMenu, and IShellExtInit interfaces are defined. The IUnknown interface is implemented because the IUnknown interface is the ancestor of all interfaces. In addition to inheriting the three basic methods of the IUnknown interface, the IContextMenu interface also defines the QueryContextMenu, InvokeCommand, and GetCommandString methods that actually implement menu expansion, A brief description has been made earlier. I will not describe it here.

In the protected section of the CContextMenuEx class, a member function ClickSample is declared. This function is set to process the added menu commands. Defines an array m_szFileName to save the complete path of the selected file;

(3) add implementation code for class CContextMenuExt

For the sake of clarity, I will gradually give a description of each member function. First, let's take a look at the contextmenuext class constructor. Two member variables are initialized in the constructor. m_cRef indicates the reference count of the class, and m_pDataObj indicates the pointer to the IDataObject interface sent by the system, which will be used later. G_nDllRefCount is a global variable to be defined in the DLL. it is used to record the number of times the entire DLL is referenced, and determines whether to run Shell to uninstall the DLL server. When you select the menu maintained by the DLL server for the context menu of an AC3 File Class, Shell calls the InvokeCommand member function of the IContextMenu interface in the CContextMenuExt class to process the user's selection. Here, CContextMenuExt uses the lpVerb parameter of lpcmi to find the flag of the menu selected by the user, and calls the corresponding member function for processing. In this article, this member function starts the AC3 player to play the files stored in the m_szFileName array;

(4) Add another class CcontextMenuExtFactory to the project. After the Dll has the CContextMenuExt class, you also need to create this function class in the DLL. Therefore, you need to add another class CContextMenuExtFactory as the class CContextMenuExt class factory. The so-called "Class factory" is a COM object specifically used to produce other functional classes. It needs to implement the IClassfactory interface, and of course also needs to implement the IUnknown interface. The CreateInstance and LockServer member functions are required to implement the IClassFactory interface;

(5) implement some framework code for the server

You need to define a reference counting variable g_nDllRefCount for the entire DLL and a variable g_hThisDll used to record the module handle of the DLL. As a Win32 dynamic link library, you need to implement several standard functions for the DLL, such as DLLMain, DLLCanUnloadNow, and DLLGet ClassObject. First, add a prototype for these functions. The function DllMain is the entry function of all Win32 dynamic link libraries. Save the hInstance of the process in the global variable g_hThisDll of the DLL for future use. When the system process or thread is initialized or terminated, the DllMain function in the Dll is called by the system with corresponding parameters. In addition, when other processes or threads call the LoadLibrary function to load the Dll or call the FreddLibray function to uninstall the Dll, the DllMain function is also called by the system. The DllCanUnloadNow function uses the global variable g_nDllRefCount to determine whether the DLL implementing the function is in use. if not, the caller can safely uninstall the DLL from the inner layer. The DllGetClassObject function is used to obtain the object instance of the class defined in the DLL. The DllGetClassObject function is usually called in the CoGetClassObject function.

(6) edit the registration file of the context menu Handler to create one. after the reg file is written in a certain format, double-click the file in the operating system, and the registration entries of the file table are merged into the registry, it should be emphasized that the globally unique identifier of the handler class and interface is generated using the UUIDGEN utility provided by Visual C ++ 6.0. It should also be noted that because the operating system used by the author is Windows2000, the ContextMenuExt. dll file compiled and connected should be placed under C: WINNTSystem.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.