我如何自动删除我的Delphi程序?我试过这段代码:
procedure DeleteSelf; var module : HMODULE; buf : array [ 0 .. MAX_PATH - 1 ] of char; p : ULONG; hKrnl32 : HMODULE; pExitProcess,pDeleteFile,pFreeLibrary : pointer; begin module := GetModuleHandle ( nil ); GetModuleFileName ( module,buf,sizeof ( buf ) ); CloseHandle ( THandle ( 4 ) ); p := ULONG ( module ) + 1; hKrnl32 := GetModuleHandle ( 'kernel32' ); pExitProcess := GetProcAddress ( hKrnl32,'ExitProcess' ); pDeleteFile := GetProcAddress ( hKrnl32,'DeleteFileA' ); pFreeLibrary := GetProcAddress ( hKrnl32,'FreeLibrary' ); asm lea eax,buf push 0 push 0 push eax push pExitProcess push p push pDeleteFile push pFreeLibrary ret end; end;
您的代码仅在Windows NT和2000下运行.因为在这些操作系统中,系统保留对内存映射文件的usermode句柄引用,该文件支持磁盘上的可执行映像.在这些Windows版本中,此句柄始终具有值0x4.
删除自己的exe的最有效方法是创建一个处于挂起状态的子进程,注入代码等待父进程(exe删除),然后检测父进程何时退出,删除父进程,最后kill孩子的过程.
您可以在这些推荐的资源中找到有关此主题的更多信息.
> Self-deleting Executables (Techniques which allow an executable file to delete itself whilst running)
> Self deleting executables
Ranju. V.