阻止删除文件(文件占坑)的delphi代码


本文整理自网络,侵删。

 【20180204更新修复代码失效问题】
代码:
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, OPEN_EXISTING, 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 xe 的 tdictionary

Delphi窗体自适应分辨率

Delphi 16进制字符串转原字符串

Delphi system.net.httpclient 下载

Delphi 反外挂,反破解思想代码

Delphi 如何快速读取文本文件

Delphi label设置为背景透明

Delphi jpg图片 旋转 切边 缩放

Delphi fmx app 设置 程序图标,闪屏,程序名

Delphi combobox的属性和事件、及几个鼠标事件的触发

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



打赏

取消

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

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

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

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

评论

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