Windows中删除自己的程序(Delphi版本)
修改一下,在Delphi中也可以用。
program DeleteSelf;
uses
Windows, Classes, Forms, ShellAPI, ShlObj;
function SelfRemove: Boolean;
var
sei: TSHELLEXECUTEINFO;
szModule, szComSpec, szParams: String;
begin
SetLength(szComSpec, MAX_PATH +1);
szModule := Application.ExeName;
if((GetEnvironmentVariable('COMSPEC', PChar(szComSpec), MAX_PATH) <>0)) then
begin
szParams := '/c del' + szModule + ' >nul';
// set struct members
sei.cbSize := sizeof(sei);
sei.wnd := 0;
sei.lpVerb := 'Open';
sei.lpFile := PChar(szComspec);
sei.lpParameters := PChar(szParams);
sei.lpDirectory := 0;
sei.nShow := SW_HIDE;
sei.fMask := SEE_MASK_NOCLOSEPROCESS;
// increase resource allocation to program
SetPriorityClass(GetCurrentProcess(),
REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(),
THREAD_PRIORITY_TIME_CRITICAL);
// invoke command shell
if(ShellExecuteEx(@sei)) then
begin
// suppress command shell process until program exits
SetPriorityClass(sei.hProcess,IDLE_PRIORITY_CLASS);
SetProcessPriorityBoost(sei.hProcess,TRUE);
// notify explorer shell of deletion
SHChangeNotify(SHCNE_DELETE,SHCNF_PATH,PChar(szModule),0);
end
else // if error, normalize allocation
begin
SetPriorityClass(GetCurrentProcess(),
NORMAL_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(),
THREAD_PRIORITY_NORMAL);
end;
end;
end;
begin
Application.MessageBox('本程序将删除自己', '提示', MB_OK);
if not SelfRemove then
begin
Application.MessageBox('删除成功!', '提示', MB_OK);
end;
end.
program DeleteSelf;
uses
Windows, Classes, Forms, ShellAPI, ShlObj;
function SelfRemove: Boolean;
var
sei: TSHELLEXECUTEINFO;
szModule, szComSpec, szParams: String;
begin
SetLength(szComSpec, MAX_PATH +1);
szModule := Application.ExeName;
if((GetEnvironmentVariable('COMSPEC', PChar(szComSpec), MAX_PATH) <>0)) then
begin
szParams := '/c del' + szModule + ' >nul';
// set struct members
sei.cbSize := sizeof(sei);
sei.wnd := 0;
sei.lpVerb := 'Open';
sei.lpFile := PChar(szComspec);
sei.lpParameters := PChar(szParams);
sei.lpDirectory := 0;
sei.nShow := SW_HIDE;
sei.fMask := SEE_MASK_NOCLOSEPROCESS;
// increase resource allocation to program
SetPriorityClass(GetCurrentProcess(),
REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(),
THREAD_PRIORITY_TIME_CRITICAL);
// invoke command shell
if(ShellExecuteEx(@sei)) then
begin
// suppress command shell process until program exits
SetPriorityClass(sei.hProcess,IDLE_PRIORITY_CLASS);
SetProcessPriorityBoost(sei.hProcess,TRUE);
// notify explorer shell of deletion
SHChangeNotify(SHCNE_DELETE,SHCNF_PATH,PChar(szModule),0);
end
else // if error, normalize allocation
begin
SetPriorityClass(GetCurrentProcess(),
NORMAL_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(),
THREAD_PRIORITY_NORMAL);
end;
end;
end;
begin
Application.MessageBox('本程序将删除自己', '提示', MB_OK);
if not SelfRemove then
begin
Application.MessageBox('删除成功!', '提示', MB_OK);
end;
end.

0 条评论:
发表评论
<< 主页