国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

NET Framework 用C#創建SHELL擴展

2024-07-21 02:29:44
字體:
來源:轉載
供稿:網友

一、前言

.net平臺是微軟公司推出的作為未來軟件運行和開發的環境,c#是微軟力薦的在.net平臺下開發應用軟件的首選語言。本文將討論在.net環境下如何使用c#語言開發windows shell擴展問題。如今windows家族已發展到xp世代了,想必每個程序員都對shell extension不會感到陌生吧,在這里我不想花太多的時間介紹shell extension的原理知識,本文中將通過一個實例介紹用c#創建一個shell extension,在此過程中也會簡單介紹一些shell extension的原理知識(如果想詳細了解shell擴展原理知識,請參閱msdn)。

二、開發環境

(1)windows2000 專業版。
(2)
visual studio.net beta 2.0或正式版1.0。

三、原理介紹

本實例實現一個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則停止后面的所有操作。根據以上原理,本實例要實現shell擴展就必須要實現一個支持ishellexecutehook接口的com組件。

接口聲明

c#不能像c++那樣用一句#include "shlguid.h"語句就可以完成ishellexecutehook接口聲明,它必須要求在程序中聲明接口的具體信息,聲明如下:

[comimpor,interfacetype(cominterfacetype.interfaceisiunknown), guid("000214fb-0000-0000-c000-000000000046")]
/* guid("000214fb-0000-0000-c000-000000000046")
相當于shlguid.h中的define_shlguid(iid_ishellexecutehookw, 0x000214fbl, 0, 0); */
public interface ishellexecutehook{
[preservesig()] /*
允許返回值為
com hresult */
int execute(shellexecuteinfo sei);
}

結構聲明

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;
}

shellexecuteinfo結構體的元素是不是夠多的,它們的具體說明就不一一介紹了,如果你有空的話可以看看msdn。

四、實現步驟

介紹了isellexecutehook接口的聲明以及shellexecuteinfo結構體的聲明后,我們就著手實現這個應用實例,這個實例很簡單,每當explorer對一個shell對象執行某動作前將會彈出一個對話框,在其上顯示執行的動作內容、對象名以及參數內容。

打開vs.net,按下面步驟工作:

1.新建一個空項目(項目名:extenshell)。

2.添加一個新類(類名:extenshell.cs)。
  
3.
將下面代碼作為extenshell.cs的內容。

   /* extenshell.cs */
   using system;
   using system.reflection;
   using system.runtime.interopservices;
   using system.windows.forms;

   [assembly: assemblykeyfile(@"../../eskey.snk")] /*密鑰文件*/
   namespace shellextension
   {
     //
接口聲明。

     [comimport,interfacetype(cominterfacetype.interfaceisiunknown), guid("000214fb-0000-0000-c000-000000000046")]
        /* guid("000214fb-0000-0000-c000-000000000046")
相當于shlguid.h中的define_shlguid(iid_ishellexecutehookw, 0x000214fbl, 0, 0); */
     public interface ishellexecutehook
     {
         [preservesig()] /*
允許返回值為
com hresult */
         int execute(shellexecuteinfo sei);
     }

     //結構聲明。
     [structlayout(layoutkind.sequential)]
     public class shellexecuteinfo
     {
         public int cbsize;
         public int fmask;
         public int hwnd;
         [marshalas(unmanagedtype.lpwstr)]
         public string lpverb;
         [marshalas(unmanagedtype.lpwstr)]
         public string lpfile;
         [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;
      }

      [guid("027f9368-a83e-42cc-85b2-1dc5e23c4608"), comvisible(true)]
         /*
guid生成工具創建一個新的guid作為類對象的guid標識。 */
      public class extenshell : ishellexecutehook
      {
           private int s_ok=0;
           private int s_false=1;
           public int execute(shellexecuteinfo sei)
           {
               try
               {
                   messagebox.show(null, "[ verb ]: " + sei.lpverb + "/n[ file ]: " + sei.lpfile + "/n[ parameters ]:" + sei.lpparameters + "/n[ directory ]:" + sei.lpdirectory , "shellextensionhook",messageboxbuttons.ok, messageboxicon.information);

               }
               catch(exception e)
               {
                   console.error.writeline("unknown exception : " + e.tostring());
               }

               return s_false;
               //
如果返回值為s_okshell將停止對shell對象的以后的所有動作。
            }
       }
  }

4. 在命令行上運行:sn -k eskey.snk ( sn.exe c:/programe files/microsoft.net/frameworksdk/bin下可以找到 ),將eskey.snk添加到項目中。
 
    5.
打開<項目> --> <屬性>,將輸出類型改成類庫。
 
 
    6.
編譯完成。
 
    7.
.net可控代碼生成的com組件注冊后要到assembly目錄中尋找實體執行,故應將編譯好的extenshell.dll文件拷貝到c:/winnt/assembly目錄中?!?span lang=en-us>

8. 注冊組件。在命令行上運行:regasm {項目路徑}/bin/debug/extenshell.dll。( regasm.exec:/winnt/microsoft.net/framework/v1.0.2914下可以找到)
 

    9.
最后,在hklm/software/microsoft/windows/currentversion/explorer/shellexecutehooks項下新建一個字符串值,其名為{027f9368-a83e-42cc-85b2-1dc5e23c4608},值可以為空也可以加入一串描述性文字。

五、結 束

    這是一個簡單的shell擴展的例子,雖然不是一個完整的應用,但是作者想通過此實例向讀者介紹shell擴展和.net平臺下的com組件開發技術,希望它能起拋磚引玉的作用。



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沧州市| 沂水县| 淳安县| 个旧市| 石林| 江川县| 水城县| 浦城县| 思南县| 平昌县| 开平市| 乐至县| 色达县| 丹棱县| 方城县| 阿拉善右旗| 九龙城区| 洛隆县| 米易县| 西充县| 手机| 砀山县| 隆安县| 崇义县| 大庆市| 莱阳市| 通渭县| 连平县| 日土县| 万宁市| 淳化县| 阿克陶县| 武宣县| 全州县| 鄂托克前旗| 当雄县| 论坛| 宕昌县| 太白县| 孝义市| 大厂|