delphi 实现文件防删除非亢占(非HOOK)


本文整理自网络,侵删。

 function EnabledDebugPrivilege(const bEnabled: Boolean): Boolean;
var
hToken: THandle;
tp: TOKEN_PRIVILEGES;
a: DWORD;
const
SE_DEBUG_NAME = 'SeDebugPrivilege';
begin
Result := False;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, hToken)) then
begin
tp.PrivilegeCount := 1;
LookupPrivilegeValue(nil, SE_DEBUG_NAME, tp.Privileges[0].Luid);
if bEnabled then
tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
else
tp.Privileges[0].Attributes := 0;
a := 0;
AdjustTokenPrivileges(hToken, False, tp, SizeOf(tp), nil, a);
Result := GetLastError = ERROR_SUCCESS;
CloseHandle(hToken);
end;
end;

function DupFile(FileName:String;PID:Cardinal):Boolean;
var
hFile,hProcess,hTarget:THandle;
begin
Result := False;
EnabledDebugPrivilege(True);
hProcess := OpenProcess(PROCESS_DUP_HANDLE, False, PID);
Try
if hProcess <> 0 then
begin
hFile := CreateFile(PChar(FileName), GENERIC_READ, 0, nil, Create_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if hFile = INVALID_HANDLE_VALUE then Exit;
Result := DuplicateHandle(GetCurrentProcess(), hFile, hProcess, @hTarget,
0, False, DUPLICATE_SAME_ACCESS or DUPLICATE_CLOSE_SOURCE);
end;
Finally
CloseHandle(hProcess);
End;
end;

相关阅读 >>

Delphi twebbrowser 设置焦点

Delphi 验证文件签名

Delphi fullscreen全屏

Delphi 运行中申请uac权限

Delphi 根据进程pid获取程序所在路径的函数

Delphi暴力关机代码

Delphi xe webbroker 开发,解决 response 返回中文乱码问题

Delphi http 常见异常状态及Delphi idhttp 控件处理方式

Delphi xe 中的字符串生成哈希值(md5 / sha-1 / jenkins)

Delphi中使用ado连接带密码的access

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...