main.cpp
上传用户:aorui801
上传日期:2022-07-20
资源大小:201k
文件大小:6k
源码类别:

Shell编程

开发平台:

Visual C++

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop 
  4. #include "main.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma resource "*.dfm"
  8. TfrmMain *frmMain;
  9. //---------------------------------------------------------------------------
  10. AnsiString __fastcall TfrmMain::SHEventName(AnsiString strPath1,AnsiString strPath2,DWORD lParam)
  11. {
  12.         AnsiString sEvent;
  13.         
  14.         switch(lParam)//根据参数设置提示消息
  15.         {
  16.                 case SHCNE_RENAMEITEM:    sEvent = "重命名文件 ["+strPath1 + "] 为 [" + strPath2 + "]"; break;
  17.                 case SHCNE_CREATE:        sEvent = "建立文件   ["+strPath1 + "]";break;
  18.                 case SHCNE_DELETE:        sEvent = "删除文件   ["+strPath1 + "]";break;
  19.                 case SHCNE_MKDIR:         sEvent = "新建目录   ["+strPath1 + "]";break;
  20.                 case SHCNE_RMDIR:         sEvent = "删除目录   ["+strPath1 + "]";break;
  21.                 case SHCNE_MEDIAINSERTED: sEvent = "[" + strPath1+"] 中插入可移动存储介质";break;
  22.                 case SHCNE_MEDIAREMOVED:  sEvent = "[" + strPath1+"] 中移去可移动存储介质"+strPath1+" "+strPath2;break;
  23.                 case SHCNE_DRIVEREMOVED:  sEvent = "移去驱动器 ["+strPath1+ "]";break;
  24.                 case SHCNE_DRIVEADD:      sEvent = "添加驱动器 ["+strPath1+ "]";break;
  25.                 case SHCNE_NETSHARE:      sEvent = "设置目录 ["+strPath1+"] 为共享";break;
  26.                 case SHCNE_NETUNSHARE:    sEvent = "取消目录 ["+strPath1+"] 的共享"; break;
  27.                 case SHCNE_ATTRIBUTES:    sEvent = "改变文件目录属性 ["+strPath1+ "]";break;
  28.                 case SHCNE_UPDATEDIR:     sEvent = "更新目录 ["+strPath1+ "]";break;
  29.                 case SHCNE_UPDATEITEM:    sEvent = "更新文件 ["+strPath1+ "]";break;
  30.                 case SHCNE_SERVERDISCONNECT: sEvent = "断开与服务器的连接 ["+strPath1+"] ["+strPath2+ "]";break;
  31.                 case SHCNE_UPDATEIMAGE:   sEvent = "执行:SHCNE_UPDATEIMAGE 操作";break;
  32.                 case SHCNE_DRIVEADDGUI:   sEvent = "执行:SHCNE_DRIVEADDGUI 操作";break;
  33.                 case SHCNE_RENAMEFOLDER:  sEvent = "重命名文件夹 ["+strPath1+"] 为 ["+strPath2+ "]";break;
  34.                 case SHCNE_FREESPACE:     sEvent = "磁盘空间大小改变 ";break;
  35.                 case SHCNE_ASSOCCHANGED:  sEvent = "改变文件关联";break;
  36.                 case SHCNE_EXTENDED_EVENT:sEvent = "此目录有变化 [" + strPath2 + "]"; break;
  37.                 default:                  sEvent = "未知操作 ["+IntToStr(lParam)+ "]";break;
  38.         }
  39.         return sEvent;
  40. }
  41. bool __fastcall TfrmMain::SHNotify_Register(HWND hWnd)
  42. {
  43.         IDLSTRUCT ps;
  44.         if( m_hSHNotify != NULL) return False;
  45.         
  46.         if( SHGetSpecialFolderLocation == NULL) return;
  47.         if( SHGetSpecialFolderLocation(0,CSIDL_DESKTOP,&m_pidlDesktop) == NOERROR)
  48.         {
  49.                 if( m_pidlDesktop )
  50.                 {
  51.                         ps.pidl = m_pidlDesktop;
  52.                         ps.bWatchSubFolders = 1;
  53.                         m_hSHNotify = SHChangeNotifyRegister(hWnd,
  54.                                         SHCNF_TYPE | SHCNF_IDLIST,
  55.                                         SHCNE_ALLEVENTS | SHCNE_INTERRUPT,
  56.                                         WM_SHNOTIFY,1,&ps);
  57.                         return True;
  58.                 }
  59.                 else
  60.                 {
  61.                         CoTaskMemFree(m_pidlDesktop);
  62.                 }
  63.         }
  64.         return False;
  65. }
  66. bool __fastcall TfrmMain::SHNotify_UnRegister()
  67. {
  68.         if( m_hSHNotify == NULL) return False;
  69.         if( SHChangeNotifyDeregister == NULL) return False;
  70.         if( SHChangeNotifyDeregister(m_hSHNotify) )
  71.         {
  72.                 m_hSHNotify = NULL;
  73.                 CoTaskMemFree(m_pidlDesktop);
  74.                 return True;
  75.         }
  76.         return False;
  77. }
  78. __fastcall TfrmMain::TfrmMain(TComponent* Owner)
  79.         : TForm(Owner)
  80. {
  81. }
  82. //---------------------------------------------------------------------------
  83. void __fastcall TfrmMain::WMShellReg(TMessage & Message)
  84. {
  85.         //TODO: Add your source code here
  86.         AnsiString strPath1;
  87.         AnsiString strPath2;
  88.         char strPath[256];
  89.         AnsiString sTmp;
  90.         PSHNOTIFYSTRUCT pidlItem;
  91.         pidlItem = (PSHNOTIFYSTRUCT)Message.WParam;
  92.         SHGetPathFromIDList(pidlItem->dwItem1,strPath);
  93.         strPath1 = strPath;
  94.         SHGetPathFromIDList(pidlItem->dwItem2,strPath);
  95.         strPath2 = strPath;
  96.         sTmp = FormatDateTime("[yyyy-mm-dd hh:mm:ss]:",Now()) + SHEventName(strPath1,strPath2,Message.LParam);
  97.         
  98.         Memo1->Lines->Add(sTmp);
  99. }
  100. void __fastcall TfrmMain::WMKeyLog(TMessage & Message)
  101. {
  102.         //TODO: Add your source code here
  103.         Memo1->Lines->AddStrings(HookList);
  104.         HookList->Clear();
  105. }
  106. void __fastcall TfrmMain::FormCreate(TObject *Sender)
  107. {
  108.         m_hSHNotify = NULL;
  109.         HMODULE DLLHandle = LoadLibrary("SHELL32.DLL");
  110.         if( DLLHandle )
  111.         {
  112.                 SHChangeNotifyRegister   = (SHNRegister)GetProcAddress(DLLHandle,(LPCSTR)2);
  113.                 SHChangeNotifyDeregister = (SHNUnRegister)GetProcAddress(DLLHandle,(LPCSTR)4);
  114.         }
  115.         FreeLibrary(DLLHandle);
  116.         
  117.         SHNotify_Register(Handle);
  118.         InstallHook(Handle);
  119. }
  120. //---------------------------------------------------------------------------
  121. void __fastcall TfrmMain::FormClose(TObject *Sender, TCloseAction &Action)
  122. {
  123.         SHNotify_UnRegister();
  124.         UnInstallHook();
  125. }
  126. //---------------------------------------------------------------------------