天天看點

1、VS2005(c/c++)外殼擴充程式設計之windows右鍵菜單(1)

第一部分 SHELL基本概念<?xml:namespace prefix = o />

    Windows外殼擴充(Windows Shell Extension),是一類特殊的COM對象,在這類COM對象中使用者可以加入自己的特殊功能,而Windows外殼擴充最終都會被Windows Explorer所引用[1]。

    A shell extension is a COM object that adds some kind of functionality to the Windows shell (Explorer).

    There are two parts in the term "shell extension." Shell refers to Explorer, and extension refers to code you write that gets run by Explorer when a predetermined event happens (e.g., a right-click on a .DOC file). So a shell extension is a COM object that adds features to Explorer.[7]

    A shell extension is an in-process server that implements some interfaces that handle the communication with Explorer. ATL is the easiest way to get an extension up and running quickly, since without it you'd be stuck writing QueryInterface() and AddRef() code over and over. It is also much easier to debug extensions on Windows NT-based OSes, as I will explain later.

    “Shell 擴充”從字面上分兩個部分:Shell 與 Extension。Shell 指 Windows Explorer,而Extension 則指由你編寫的當某一預先約定好的事件(如在以. doc 為字尾的檔案圖示上單擊右鍵)發生時由 Explorer調用執行的代碼。是以一個“Shell 擴充”就是一個為 Explorer 添加功能的 COM 對象。

    動态庫必須注冊才能使用。除了使用 regasm 來注冊 DLL 以外,還應該在代碼中增加 RegisterServer 和 UnregisterServer 方法,以指導 DLL 注冊時,在 Windows 系統資料庫中增加什麼鍵。關于具體鍵以下做簡單說明:

    1) 注冊DLL的Shell Extensions。具體位置是 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved,增加以 GUID 為名稱的鍵,值則是動态庫說明。(此位置裡面全是 Shell 擴充的動态庫注冊,許多相關軟體就是從裡面擷取資訊,例如 ShexView。

    2) 關聯檔案。Shell擴充一般是針對檔案或者檔案夾的,是以必須關聯;許多人都熟知“HKEY_CLASSES_ROOT\*”的作用,就是用來關聯所有檔案。而檔案夾則是“HKEY_CLASSES_ROOT\Folder”。[3]

    我們所看到的資料總管以及整個桌面,都是一個 Shell。在win32中是以外殼名字空間的形式來組織檔案系統的,在外殼名字空間裡的每一個對象(注)都實作了一個IShellFolder的接口,通過這個接口我們可以直接查詢或間接得到其他相關的接口。

    (注:這裡的對象指的是外殼名字空間中的一個節點,對象有可能是一個檔案夾,有可能是一個檔案,也有可能是一個虛拟檔案夾,例如:我的電腦,網路上的芳鄰,控制台等)[4]

    A shell extension is an in-process server that implements some interfaces that handle the communication with Explorer. ATL is the easiest way to get an extension up and running quickly, since without it you'd be stuck writing QueryInterface() and AddRef() code over and over. It is also much easier to debug extensions on Windows NT-based OSes[7]

    在外殼程式設計中,要使用 PIDL 路徑代替普通路徑“桌面”是最頂級的檔案夾,外殼名字空間中其他各項都可以用從“桌面”開始的 PIDL 加以表示。

通過API SHGetDesktopFolder擷取“桌面”的 PIDL 和其 IShellFolder 接口。

通過“桌面”,來擷取“C:\”這個路徑的 PIDL 和 IShellFolder 接口,可以通過 IShellFolder 的 ParseDisplayName 和 BindToObject 函數。

繼續閱讀