我想实现对系统文件的控制(不管是打开文件夹或是文件都需要输入密码),应该如何实现 - C++ Builder / Windows SDK/API
我想实现对系统文件的控制(不管是打开文件夹或是文件都需要输入密码),应该如何实现(不通过系统来做),谢谢!
[解决办法]
不通过系统来做
----
什么意思?
[解决办法]
你找那个“敲诈者”的病毒源码看下就知道了
[解决办法]
用拷贝钩子实现对文件夹的监控 妖哥网站上的 ;
http://www.ccrun.com/article.asp?i=601&d=0z20f4
[解决办法]
或者shellchangenotify
[解决办法]
参见如下源程序:
1. FileWatchUnit.h
//---------------------------------------
#ifndef FileWatchUnitH
#define FileWatchUnitH
//---------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//#include <shlobj.h>
//---------------------------------------
class TForm1 : public TForm
{
__published:// IDE-managed Components
TMemo *Memo1;
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private:
bool __fastcall SHNotify_Register(HANDLE hWnd);
bool __fastcall SHNotify_UnRegister(void);// User declarations
private:
bool m_hSHNotify;
LPITEMIDLIST *m_pidlDesktop;
bool __fastcall AppHookFunc(TMessage & Message);
AnsiString __fastcall SHEvEntName(AnsiString strPath1, AnsiString strPath2, int lParam);
public:// User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------
#endif
2. FileWatchUnit.cpp
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "FileWatchUnit.h "
#define SHCNE_RENAMEITEM 0x1
#define SHCNE_CREATE 0x2
#define SHCNE_DELETE 0x4
#define SHCNE_MKDIR 0x8
#define SHCNE_RMDIR 0x10
#define SHCNE_MEDIAINSERTED 0x20
#define SHCNE_MEDIAREMOVED 0x40
#define SHCNE_DRIVEREMOVED 0x80
#define SHCNE_DRIVEADD 0x100
#define SHCNE_NETSHARE 0x200
#define SHCNE_NETUNSHARE 0x400
#define SHCNE_ATTRIBUTES 0x800
#define SHCNE_UPDATEDIR 0x1000
#define SHCNE_UPDATEITEM 0x2000
#define SHCNE_SERVERDISCONNECT 0x4000
#define SHCNE_UPDATEIMAGE 0x8000
#define SHCNE_DRIVEADDGUI 0x10000
#define SHCNE_RENAMEFOLDER 0x20000
#define SHCNE_FREESPACE 0x40000
#define SHCNE_ASSOCCHANGED 0x8000000
#define SHCNE_DISKEVENTS 0x2381F
#define SHCNE_GLOBALEVENTS 0xC0581E0
#define SHCNE_ALLEVENTS 0x7FFFFFFF
#define SHCNE_INTERRUPT 0x80000000
#define WM_SHNOTIFY 0x401
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm "
TForm1 *Form1;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
m_hSHNotify = false;
Button1-> Caption = "打开监视 ";
// 拦截由SendMessage发送给本窗口的消息
Application-> HookMainWindow(AppHookFunc);
}
//---------------------------------------
bool __fastcall TForm1::SHNotify_Register(HANDLE hWnd)
{
PIDLSTRUCT ps;
bool res;
res = false;
if(!m_hSHNotify){
if(SHGetSpecialFolderLocation(0,CSIDL_DESKTOP,m_pidlDesktop)==NOERROR){
if(m_pidlDesktop){
ps.pidl = m_pidlDesktop;
ps.bWatchSubFolders = true;
// 注册Windows监视,将获得的句柄保存到m_hSHNotify中
m_hSHNotify = SHChangeNotifyRegister(hWnd, SHCNF_TYPE | SHCNF_IDLIST,
NE_ALLEVENTS | SHCNE_INTERRUPT, WM_SHNOTIFY, 1, ps);
if(m_hSHNotify) res = true;
}
else CoTaskMemFree(m_pidlDesktop);
}
}
return res;
}
bool __fastcall TForm1::SHNotify_UnRegister(void)
{
bool res;
res = false;
if(m_hSHNotify){
if(SHChangeNotifyDeregister(m_hSHNotify)){
m_hSHNotify = false;
CoTaskMemFree(m_pidlDesktop);
res = true;
}
}
return res;
}
bool __fastcall TForm1::AppHookFunc(TMessage & Message)
{
bool Handled;
Handled = true;
switch(Message.Msg){
// 测试发出WM_SHNOTIFY消息后的处理
case WM_SHNOTIFY:
AnsiString strPath1,strPath2;
char charPath[259];
PSHNOTIFYSTRUCT pidlItem;
pidlItem = PSHNOTIFYSTRUCT(Message.WParam);
SHGetPathFromIDList(pidlItem.dwItem1,charPath);
strPath1 = charPath;
SHGetPathFromIDList(pidlItem.dwItem2,charPath);
strPath2 = charPath;
Memo1-> Lines.Add(SHEvEntName(strPath1,strPath2,Message.LParam)+char(13)+char(10));
break;
}
return Handled;
}
AnsiString __fastcall TForm1::SHEvEntName(AnsiString strPath1, AnsiString strPath2, int lParam)
{
AnsiString sEvent;
switch (lParam)
{
case SHCNE_RENAMEITEM: sEvent = '重命名文件 '+strPath1+ '为 '+strpath2;
break;
case SHCNE_CREATE: sEvent = '建立文件 文件名: '+strPath1;
break;
case SHCNE_DELETE: sEvent = '删除文件 文件名: '+strPath1;
break;
case SHCNE_MKDIR: sEvent = '新建目录 目录名: '+strPath1;
break;
case SHCNE_RMDIR: sEvent = '删除目录 目录名: '+strPath1;
break;
case SHCNE_MEDIAINSERTED: sEvent = strPath1+ '中插入可移动存储介质 ';
break;
case SHCNE_MEDIAREMOVED: sEvent = strPath1+ '中移去可移动存储介质 '+strPath1+ ' '+strpath2;
break;
case SHCNE_DRIVEREMOVED: sEvent = '移去驱动器 '+strPath1;
break;
case SHCNE_DRIVEADD: sEvent = '添加驱动器 '+strPath1;
break;
case SHCNE_NETSHARE: sEvent = '改变目录 '+strPath1+ '的共享属性 ';
break;
case SHCNE_ATTRIBUTES: sEvent = '改变文件目录属性 文件名 '+strPath1;
break;
case SHCNE_UPDATEDIR: sEvent = '更新目录 '+strPath1;
break;
case SHCNE_UPDATEITEM: sEvent = '更新文件 文件名: '+strPath1;
break;
case SHCNE_SERVERDISCONNECT: sEvent = '断开与服务器的连接 '+strPath1+ ' '+strpath2;
break;
case SHCNE_UPDATEIMAGE: sEvent = 'SHCNE_UPDATEIMAGE ';
break;
case SHCNE_DRIVEADDGUI: sEvent = 'SHCNE_DRIVEADDGUI ';
break;
case SHCNE_RENAMEFOLDER: sEvent = '重命名文件夹 '+strPath1+ '为 '+strpath2;
break;
case SHCNE_FREESPACE: sEvent = '磁盘空间大小改变 ';
break;
case SHCNE_ASSOCCHANGED: sEvent = '改变文件关联 ';
break;
default: sEvent = '未知操作 '+IntToStr(lParam);
}
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
m_hSHNotify = false;
if(SHNotify_Register(Form1.Handle)){
Memo1-> Lines-> Add( "Shell监视程序成功注册 ");
Button1.Enabled = false;
}
else
ShowMessage( "Shell监视程序注册失败! ");
}