Background
We use virtual folders every day, control panels, recycle bins, my net work connections, etc. We can use virtual folder to manage items which are not in physical disk.
Shell Extension Namespace (NSE) is the formal name of virtual folder. To implement our own NSE handler, we need to implement a folder to contain items, view to display folder items, and other misc items such as IDataObject, PIDL manager, etc.
Generally, we can manipulate virtual folder via explorer or common file dialog.
· Explorer
· Common File Dialog
It is important that interface ICommDlgBrowser can only be exposed when it is hosted in a common dialog. Otherwise, it can’t be queried from explorer. Member function ICommDlgBrowser ::IncludeObject allows the common dialog box to filter objects that the view displays. ICommDlgBrowser:: OnDefaultCommand is called when a user double clicks in the view or pressed the ENTER key.
PIDL
However, instead of the character string used for paths, an item ID list is an ITEMIDLIST structure. This structure is an ordered sequence of one or more item IDs, terminated by a two-byte NULL. Each item ID in the item ID list corresponds to a namespace object. Their order defines a path in the namespace, much like a file system path.In files system, every file/folder has its own unique name. Consider this file “ c:"MyDocs"MyFile.html”, it has its unique ID:
The Item ID structure is defined as below:
typedef struct _SHITEMID { USHORT cb; // holds the size of the SHITEMID structure, in bytes. BYTE abID[1]; //object identifier } SHITEMI |
Typically, abID points to an extension-defined structure. In addition to the object's ID, this structure is often used to hold a variety of related information, such as the object's type or attributes. Your extension's folder objects can then quickly extract the information from the PIDL instead of having to query for it.Normally, it is part of an item ID list, which serves the same purpose as a file system path.
For performance reasons, your SHITEMID structure should be DWORD-aligned. In other words, you should construct your abID values such that cb is an integral multiple of 4
Another thing we need to pay attention is Absolute PIDL and Relative PIDL. The former one is the PIDL relative to its parent folder, while the last one is the PIDL from the root namespace.
How to implement our own NSE
The first step is to register/Unregister our NSE DLL.
1. Register the NSE in HK_CLASS
2. As shell extension handlers run in the shell process, which is a system process, we need to add our handler classID into shell prove list.
Details please refer to MSDN.
Next, let’s look at the basic objects in NSE:
Let’s dive into details with some typical scenarios:
· User opens a NSE folder from explorer, and then go to our NSE root
1. Don’t know when a folder object is created. However, it is not that important. 2. Explorer gets virtual folder’s PIDL somewhere, and then pass the PIDL to our extension by calling IShellFolder::Initialize(). Let our extension do the rest thing. 3. And then explore will call IShellFolder::CreateViewObjects to create folder view. 4. After the view object is created, IShellView::CreateViewWindow will be called. Generally, in this function, we can do following things: hook messages, get IBrowserObject. And then in its message handler method, call IShellFolder::IEnumObjects to list all the items in the folder. We can also take advantage of method ICommDlgBrowser::IncludeObject to decide whether to add items into view list or not. |
· User opens a NSE folder from common file dialog. It is a bit difference from scenario 1.
Common File Dialog will call IPersistFolder2::GetCurFolder(), or IPersistIDList::GetIDList to get the NSE file ID first instead of getting the ID from OS. Generally, IShellFolder should return the same ID passed in Initialized().
It is important to implement IPersistFolder2 or IPersistIDList if our folder will be hosted in commondialog.
· User selects an item in listview
1. Browser will get the selected item. 2. And then it will call try to get the information of selected item via IDataObject interface. We can implement IDataObject interface, in this GetData method, construct the absolute PIDL of the selected item. The format should be clipboarformat. 3. After explorer gets the PIDL of selected item, it call IShellFolder::GetObjectAttributes to check whether it is browser able or not. 4. If yes, explorer will call IShellFolder::BindToObject to create the folder for the selected item. |
· Click “OK” button to open a “folder”. We need to intercept this message.
Debug Tricks
1. Logs, logs, Logs! It is the most useful tool.
I am struggling absolute PIDL and relative PIDL for quite some time. Logs really helps a lot.
2. Set following keys in the registry to avoid kill explorer.exe: http://msdn.microsoft.com/en-us/library/cc144064(VS.85).aspx
转载于:https://www.cnblogs.com/pangpangxiong/archive/2009/05/15/1457740.html