C#語言開發Windows Shell擴展
C#語言有很多值得學習的地方,這里我們主要介紹C#語言開發Windows Shell擴展,包括介紹結構聲明等方面。
.NET平臺是微軟公司推出的作為未來軟件運行和開發的環境,C#是微軟力薦的在.NET平臺下開發應用軟件的***語言。本文將討論在.NET環境下如何使用C#語言開發Windows Shell擴展問題。如今Windows家族已發展到XP世代了,想必每個程序員都對Shell Extension不會感到陌生吧,在這里我不想花太多的時間介紹Shell Extension的原理知識,本文中將通過一個實例介紹用C#創建一個Shell Extension,在此過程中也會簡單介紹一些Shell Extension的原理知識。
本實例實現一個ShellExecuteEx Win32調用的鉤子操作,Windows Explorer常常會用到這個調用,如打開、編輯、打印等等Shell操作都要用到這個調用。在Windows注冊表HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\ShellExecuteHooks項下安裝了所有實現Shell擴展的組件信息。當Windows Explorer執行Shell操作前,先在注冊中查找到已注冊的Shell擴展組件,并將其實例化,每個Shell擴展組件必須至少實現了IShellExecuteHook接口,此接口提供了一個Execute()函數,Explorer將通過組件實例對象調用Execute()函數,如此函數返回為S_FALSE繼續后面的操作,如返回S_OK則停止后面的所有操作。根據以上原理,本實例要實現使用C#語言開發Windows Shell擴展。
結構聲明
在Execute()方法中有一個SHELLEXECUTEINFO結構體參數sei,接下來要聲明結構體:
- [StructLayout(LayoutKind.Sequential)]
- public class SHELLEXECUTEINFO {
- public int cbSize;
- public int fMask;
- public int hwnd;
- [MarshalAs(UnmanagedType.LPWStr)]
- public string lpVerb; /* 動作,如edit,open,print... */
- [MarshalAs(UnmanagedType.LPWStr)]
- public string lpFile; /* 根據lpVerb的值而定,常為文件名 */
- [MarshalAs(UnmanagedType.LPWStr)]
- public string lpParameters; /* 參數字符串 */
- [MarshalAs(UnmanagedType.LPWStr)]
- public string lpDirectory; /* 路徑名 */
- public int nShow;
- public int hInstApp;
- public int lpIDList;
- public string lpClass;
- public int hkeyClass;
- public int dwHotKey;
- public int hIcon;
- public int hProcess;
- }
【編輯推薦】


















