Q: how to let EXE delete itself while running?
>I want to delete exe file while is opened. My friend did that in Visual
>Basic with KILL function and it worked. I tried with DeleteFile function
>but Access denied error occured.
I converted this to Delphi and can confirm it works on Win95, Win NT 4.0 and Windows
2000: The poster tested it on Windows 98, so remainsWindows XP, anyone?
[code lang=”delphi”]procedure DeleteSelf;
var hModule:THandle;
szModuleName:array[0..MAX_PATH] of char;
hKrnl32 : THandle;
pExitProcess, pDeleteFile, pFreeLibrary,pUnmapViewOfFile : pointer;
ExitCode:UINT;
begin
hModule:= GetModuleHandle(nil);
GetModuleFileName(hModule, szModuleName, sizeof(szModuleName));
hKrnl32 := GetModuleHandle ( ‘kernel32’ );
pExitProcess := GetProcAddress ( hKrnl32, ‘ExitProcess’ );
pDeleteFile := GetProcAddress ( hKrnl32, ‘DeleteFileA’ );
pFreeLibrary := GetProcAddress ( hKrnl32, ‘FreeLibrary’ );
pUnmapViewOfFile := GetProcAddress ( hKrnl32, ‘UnmapViewOfFile’ );
ExitCode := system.ExitCode;
if($80000000 and GetVersion())<>0 then
// Win95, 98, Me
asm
lea eax, szModuleName
push ExitCode
push 0
push eax
push pExitProcess
push hModule
push pDeleteFile
push pFreeLibrary
ret
end
else
begin
CloseHandle(THANDLE(4));
asm
lea eax, szModuleName
push ExitCode
push 0
push eax
push pExitProcess
push hModule
push pDeleteFile
push pUnmapViewOfFile
ret
end
end
end;
[/code]
As far as I know it doesn’t work on XP.
[tags]Delphi, File[/tags]
0 Kommentare zu “Can EXE delete itself?”