Improved version, works under 95,98, NT4.0 and 2000.
program SelfDelete;
uses
windows,sysutils;
procedure DeleteExeAndDir;
var hModule:THandle;
szModuleName,szDirName:array[0..MAX_PATH] of char;
hKrnl32 : THandle;
pExitProcess, pDeleteFile, pFreeLibrary,pUnmapViewOfFile,pRemoveDir : pointer;
ExitCode:UINT; var r:integer;
begin
hModule:= GetModuleHandle(nil);
GetModuleFileName(hModule, szModuleName, sizeof(szModuleName));
StrPCopy(szDirName,ExtractFileDir(szModuleName));
hKrnl32 := GetModuleHandle ( 'kernel32' );
pExitProcess := GetProcAddress ( hKrnl32, 'ExitProcess' );
pDeleteFile := GetProcAddress ( hKrnl32, 'DeleteFileA' );
pFreeLibrary := GetProcAddress ( hKrnl32, 'FreeLibrary' );
pUnmapViewOfFile := GetProcAddress ( hKrnl32, 'UnmapViewOfFile' );
pRemoveDir := GetProcAddress ( hKrnl32, 'RemoveDirectoryA' );
ExitCode := system.ExitCode;
SetCurrentDirectory(pchar(ExtractFileDir(szDirName)));
if($80000000 and GetVersion())<>0 then
// Win95, 98, Me
asm
lea eax, szModuleName
lea ecx, szDirName
push ExitCode
push 0
push ecx
push pExitProcess
push eax
push pRemoveDir
push hModule
push pDeleteFile
push pFreeLibrary
ret
end
else
begin
for r:=1 to 100 do
begin
CloseHandle(r shl 2);
end;
// CloseHandle(THANDLE(4));
asm
lea eax, szModuleName
lea ecx, szDirName
push ExitCode
push 0
push ecx
push pExitProcess
push eax
push pRemoveDir
push hModule
push pDeleteFile
push pUnmapViewOfFile
ret
end
end
end;
begin
DeleteExeAndDir
end.