ExplorerShell.py
上传用户:lswyart
上传日期:2008-06-12
资源大小:3441k
文件大小:7k
源码类别:

杀毒

开发平台:

Visual C++

  1. #-----------------------------------------------------------------------------
  2. # Name:        ExplorerShell.py
  3. # Product:     ClamWin Free Antivirus
  4. #
  5. # Author:      alch [alch at users dot sourceforge dot net]
  6. #
  7. # Created:     2004/19/03
  8. # Copyright:   Copyright alch (c) 2004
  9. # Licence:     
  10. #   This program is free software; you can redistribute it and/or modify
  11. #   it under the terms of the GNU General Public License as published by
  12. #   the Free Software Foundation; either version 2 of the License, or
  13. #   (at your option) any later version.
  14. #   This program is distributed in the hope that it will be useful,
  15. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. #   GNU General Public License for more details.
  18. #   You should have received a copy of the GNU General Public License
  19. #   along with this program; if not, write to the Free Software
  20. #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. #-----------------------------------------------------------------------------
  22. # This code is based on context_menu.py demo from Mark Hammond's win32 Extensions
  23. import pythoncom
  24.     
  25. from win32com.shell import shell, shellcon
  26. import win32gui, win32con, win32api
  27. import Process
  28. import os, sys, time
  29. import RedirectStd
  30. IContextMenu_Methods = ["QueryContextMenu", "InvokeCommand", "GetCommandString"]
  31. IShellExtInit_Methods = ["Initialize"]
  32. class ShellExtension:
  33.     _reg_progid_ = "ClamWin.ShellExtension.ContextMenu"
  34.     _reg_desc_ = "ClamWin Context Menu"
  35.     _reg_clsid_ = "{94FDC9F6-8C9B-4a70-8DBB-7662FFE48EB4}"
  36.     _com_interfaces_ = [shell.IID_IShellExtInit, shell.IID_IContextMenu]
  37.     _public_methods_ = IContextMenu_Methods + IShellExtInit_Methods   
  38.     
  39.     def Initialize(self, folder, dataobj, hkey):  
  40.         print 'Initialize'      
  41.         self.dataobj = dataobj
  42.     def QueryContextMenu(self, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags):        
  43.         print 'QueryContextMenu'      
  44.         # Query the items clicked on
  45.         format_etc = win32con.CF_HDROP, None, 1, -1, pythoncom.TYMED_HGLOBAL
  46.         try:
  47.             sm = self.dataobj.GetData(format_etc)
  48.         except pythoncom.com_error:
  49.             return 0
  50.         num_files = shell.DragQueryFile(sm.data_handle, -1)
  51.         msg = "Scan For Viruses With ClamWin"
  52.         if num_files>1:
  53.             # we aren't handling multiple files
  54.             return 0
  55.         else:
  56.             self._fname = shell.DragQueryFile(sm.data_handle, 0)
  57.         idCmd = idCmdFirst
  58.         items = []
  59.         if (uFlags & 0x000F) == shellcon.CMF_NORMAL or uFlags & shellcon.CMF_EXPLORE: 
  60.             items.append(msg)
  61.         
  62.         win32gui.InsertMenu(hMenu, indexMenu,
  63.                             win32con.MF_SEPARATOR|win32con.MF_BYPOSITION,
  64.                             0, None)
  65.         indexMenu += 1
  66.         for item in items:
  67.             win32gui.InsertMenu(hMenu, indexMenu,
  68.                                 win32con.MF_STRING|win32con.MF_BYPOSITION,
  69.                                 idCmd, item)
  70.             indexMenu += 1
  71.             idCmd += 1
  72.         win32gui.InsertMenu(hMenu, indexMenu,
  73.                             win32con.MF_SEPARATOR|win32con.MF_BYPOSITION,
  74.                             0, None)
  75.         indexMenu += 1
  76.         return idCmd-idCmdFirst # Must return number of menu items we added.
  77.     def InvokeCommand(self, ci):        
  78.         print 'InvokeCommand'
  79.         mask, hwnd, verb, params, dir, nShow, hotkey, hicon = ci           
  80.         # get the directory of our dll        
  81.         try:
  82.             if hasattr(sys, "frozen"):
  83.                 # attempt to read the folder form registry first
  84.                 key = None
  85.                 try:
  86.                     key = win32api.RegOpenKeyEx(win32con.HKEY_LOCAL_MACHINE, 'Software\ClamWin')
  87.                     currentDir = win32api.RegQueryValueEx(key, 'Path')[0]                                        
  88.                     win32api.CloseHandle(key)
  89.                 except win32api.error:                      
  90.                     if key is not None:
  91.                         win32api.CloseHandle(key)
  92.                     # couldnt find it in the registry
  93.                     # get it from command line
  94.                     if sys.frozen == "dll":            
  95.                         this_filename = win32api.GetModuleFileName(sys.frozendllhandle)
  96.                     else:
  97.                         this_filename = sys.executable
  98.                     currentDir = os.path.split(this_filename)[0]
  99.             else:    
  100.                 currentDir = os.path.split(os.path.abspath(__file__))[0]                
  101.         except NameError: # No __file__ attribute (in boa debugger)
  102.             currentDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
  103.         os.chdir(currentDir)    
  104.         # we need to resort to calling external executable here
  105.         # because wxPython has some threading issues when called from
  106.         # multiple Windows Explorer instances
  107.         # read this value from registry
  108.         
  109.         exe = os.path.join(currentDir, 'ClamWin.exe')             
  110.         if not os.path.exists(exe):            
  111.             win32gui.MessageBox(hwnd, 'Could not locate file: %s'% exe, 'ClamWin', win32con.MB_OK | win32con.MB_ICONEXCLAMATION)            
  112.         else:    
  113.             cmd = '"%s" --mode=scanner --path="%s"' % (exe, self._fname)            
  114.             try:
  115.                 proc = Process.ProcessOpen(cmd) 
  116.                 proc.close()               
  117.             except Process.ProcessError:
  118.                 win32gui.MessageBox(hwnd, 'Could not execute %s.' % cmd, 'ClamWin', win32con.MB_OK | win32con.MB_ICONEXCLAMATION)                                                                                            
  119.                 
  120.     def GetCommandString(self, cmd, typ):        
  121.         return "ClamWin Free Antivirus"
  122. def DllRegisterServer():
  123.     import _winreg
  124.     keyNames = ("Folder\shellex", "*\shellex")
  125.     for name in keyNames:
  126.         key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, name)
  127.         subkey = _winreg.CreateKey(key, "ContextMenuHandlers")
  128.         subkey2 = _winreg.CreateKey(subkey, "ClamWin")
  129.         _winreg.SetValueEx(subkey2, None, 0, _winreg.REG_SZ, ShellExtension._reg_clsid_)
  130.     print ShellExtension._reg_desc_, "registration complete."
  131. def DllUnregisterServer():
  132.     import _winreg
  133.     try:
  134.         keyNames = ("Folder\shellex", "*\shellex")
  135.         for name in keyNames:
  136.             key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
  137.                                 name + "\ContextMenuHandlers\ClamWin")
  138.     except WindowsError, details:
  139.         import errno
  140.         if details.errno != errno.ENOENT:
  141.             raise
  142.     print ShellExtension._reg_desc_, "unregistration complete."
  143. if __name__=='__main__':
  144.     from win32com.server import register    
  145.     register.UseCommandLine(ShellExtension,
  146.                    finalize_register = DllRegisterServer,
  147.                    finalize_unregister = DllUnregisterServer)
  148.