防止文件夹被删除的技巧
libraryCopyHook;
uses
ComServ,
CopyMainin’CopyMain.pas’;
exports
DllGetClassObject,
DllCanUnloadNow,
DllRegisterServer,
DllUnregisterServer;
{$R*.TLB}
{$R*.RES}
begin
end.
////////////CopyMain.pas
unitCopyMain;
interface
usesWindows,ComObj,ShlObj;
type
TCopyHook=class(TComObject,ICopyHook)
protected
functionCopyCallback(Wnd:HWND;wFunc,wFlags:UINT;pszSrcFile:PAnsiChar;
dwSrcAttribs:DWORD;pszDestFile:PAnsiChar;dwDestAttribs:DWORD):UINT;stdcall;
end;
TCopyHookFactory=class(TComObjectFactory)
protected
functionGetProgID:string;override;
procedureApproveShellExtension(Register:Boolean;constClsID:string);
virtual;
public
procedureUpdateRegistry(Register:Boolean);override;
end;
implementation
usesComServ,SysUtils,Registry;
{TCopyHook}
//file://当Windows外壳程序执行文件夹或者打印机端口操作时,CopyCallBack
//file://方法就会被调用。
functionTCopyHook.CopyCallback(Wnd:HWND;wFunc,wFlags:UINT;
pszSrcFile:PAnsiChar;dwSrcAttribs:DWORD;pszDestFile:PAnsiChar;
dwDestAttribs:DWORD):UINT;
const
FO_COPY=2;
FO_DELETE=3;
FO_MOVE=1;
FO_RENAME=4;
var
sOp:string;
begin
CasewFuncof
FO_COPY:sOp:=format(’你确定要将%s拷贝到%s吗?’,[pszSrcFile,pszDestFile]);
FO_DELETE:sOp:=format(’你确定要将%s删除吗?’,[pszSrcFile]);
FO_MOVE:sOp:=format(’你确定要将%s转移到%s吗?’,[pszSrcFile,pszDestFile]);
FO_RENAME:sOp:=format(’你确定要将%s重命名为%s吗?’,[pszSrcFile,pszDestFile]);
else
sOp:=format(’无法识别的操作代码%d’,[wFlags]);
end;
//提示,让用户决定是否执行操作
Result:=MessageBox(Wnd,PChar(sOp),
’文件挂钩演示’,MB_YESNOCANCEL);
end;
{TCopyHookFactory}
functionTCopyHookFactory.GetProgID:string;
begin
Result:=’’;
end;
procedureTCopyHookFactory.UpdateRegistry(Register:Boolean);
var
ClsID:string;
begin
ClsID:=GUIDToString(ClassID);
inheritedUpdateRegistry(Register);
ApproveShellExtension(Register,ClsID);
ifRegisterthen
//file://将clsid加入到注册表的CopyHookHandlers中
CreateRegKey(’directory/shellex/CopyHookHandlers/’+ClassName,’’,
ClsID)
else
DeleteRegKey(’directory/shellex/CopyHookHandlers/’+ClassName);
end;
procedureTCopyHookFactory.ApproveShellExtension(Register:Boolean;
constClsID:string);
const
SApproveKey=’SOFTWARE/Microsoft/Windows/CurrentVersion/ShellExtensions/Approved’;
begin
withTRegistry.Createdo
try
RootKey:=HKEY_LOCAL_MACHINE;
ifnotOpenKey(SApproveKey,True)thenExit;
ifRegisterthenWriteString(ClsID,Description)
elseDeleteValue(ClsID);
finally
Free;
end;
end;
const
CLSID_CopyHook:TGUID=’{66CD5F60-A044-11D0-A9BF-00A024E3867F}’;
LIBID_CopyHook:TGUID=’{D2F531A0-0861-11D2-AE5C-74640BC10000}’;
initialization
TCopyHookFactory.Create(ComServer,TCopyHook,CLSID_CopyHook,
’CR_CopyHook’,’文件操作挂钩演示’,ciMultiInstance,tmApartment);
end.
uses
ComServ,
CopyMainin’CopyMain.pas’;
exports
DllGetClassObject,
DllCanUnloadNow,
DllRegisterServer,
DllUnregisterServer;
{$R*.TLB}
{$R*.RES}
begin
end.
////////////CopyMain.pas
unitCopyMain;
interface
usesWindows,ComObj,ShlObj;
type
TCopyHook=class(TComObject,ICopyHook)
protected
functionCopyCallback(Wnd:HWND;wFunc,wFlags:UINT;pszSrcFile:PAnsiChar;
dwSrcAttribs:DWORD;pszDestFile:PAnsiChar;dwDestAttribs:DWORD):UINT;stdcall;
end;
TCopyHookFactory=class(TComObjectFactory)
protected
functionGetProgID:string;override;
procedureApproveShellExtension(Register:Boolean;constClsID:string);
virtual;
public
procedureUpdateRegistry(Register:Boolean);override;
end;
implementation
usesComServ,SysUtils,Registry;
{TCopyHook}
//file://当Windows外壳程序执行文件夹或者打印机端口操作时,CopyCallBack
//file://方法就会被调用。
functionTCopyHook.CopyCallback(Wnd:HWND;wFunc,wFlags:UINT;
pszSrcFile:PAnsiChar;dwSrcAttribs:DWORD;pszDestFile:PAnsiChar;
dwDestAttribs:DWORD):UINT;
const
FO_COPY=2;
FO_DELETE=3;
FO_MOVE=1;
FO_RENAME=4;
var
sOp:string;
begin
CasewFuncof
FO_COPY:sOp:=format(’你确定要将%s拷贝到%s吗?’,[pszSrcFile,pszDestFile]);
FO_DELETE:sOp:=format(’你确定要将%s删除吗?’,[pszSrcFile]);
FO_MOVE:sOp:=format(’你确定要将%s转移到%s吗?’,[pszSrcFile,pszDestFile]);
FO_RENAME:sOp:=format(’你确定要将%s重命名为%s吗?’,[pszSrcFile,pszDestFile]);
else
sOp:=format(’无法识别的操作代码%d’,[wFlags]);
end;
//提示,让用户决定是否执行操作
Result:=MessageBox(Wnd,PChar(sOp),
’文件挂钩演示’,MB_YESNOCANCEL);
end;
{TCopyHookFactory}
functionTCopyHookFactory.GetProgID:string;
begin
Result:=’’;
end;
procedureTCopyHookFactory.UpdateRegistry(Register:Boolean);
var
ClsID:string;
begin
ClsID:=GUIDToString(ClassID);
inheritedUpdateRegistry(Register);
ApproveShellExtension(Register,ClsID);
ifRegisterthen
//file://将clsid加入到注册表的CopyHookHandlers中
CreateRegKey(’directory/shellex/CopyHookHandlers/’+ClassName,’’,
ClsID)
else
DeleteRegKey(’directory/shellex/CopyHookHandlers/’+ClassName);
end;
procedureTCopyHookFactory.ApproveShellExtension(Register:Boolean;
constClsID:string);
const
SApproveKey=’SOFTWARE/Microsoft/Windows/CurrentVersion/ShellExtensions/Approved’;
begin
withTRegistry.Createdo
try
RootKey:=HKEY_LOCAL_MACHINE;
ifnotOpenKey(SApproveKey,True)thenExit;
ifRegisterthenWriteString(ClsID,Description)
elseDeleteValue(ClsID);
finally
Free;
end;
end;
const
CLSID_CopyHook:TGUID=’{66CD5F60-A044-11D0-A9BF-00A024E3867F}’;
LIBID_CopyHook:TGUID=’{D2F531A0-0861-11D2-AE5C-74640BC10000}’;
initialization
TCopyHookFactory.Create(ComServer,TCopyHook,CLSID_CopyHook,
’CR_CopyHook’,’文件操作挂钩演示’,ciMultiInstance,tmApartment);
end.
本文地址:http://www.45fan.com/dnjc/72656.html