{"id":185,"date":"2007-05-12T16:47:45","date_gmt":"2007-05-12T14:47:45","guid":{"rendered":" http:\/\/www.nsonic.de\/blog\/2007\/05\/how-to-let-exe-delete-itself-while-running\/"},"modified":"2007-05-12T16:47:47","modified_gmt":"2007-05-12T14:47:47","slug":"how-to-let-exe-delete-itself-while-running","status":"publish","type":"post","link":"https:\/\/www.nsonic.de\/blog\/2007\/05\/how-to-let-exe-delete-itself-while-running\/","title":{"rendered":"how to let EXE delete itself while running?"},"content":{"rendered":"<p><em>Q: how to let EXE delete itself while running?<\/p>\n<p>>I want to delete exe file while is opened. My friend did that in Visual<br \/>\n>Basic with KILL function and it worked. I tried with DeleteFile function<br \/>\n>but Access denied error occured.<br \/>\n<\/em> from Krasimir Stoyanov found in this groups.<\/p>\n<p> 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?<br \/>\n<!--more--><br \/>\n[code lang=&#8221;delphi&#8221;]procedure DeleteSelf;<br \/>\nvar hModule:THandle;<br \/>\n szModuleName:array[0..MAX_PATH] of char;<br \/>\n   hKrnl32 : THandle;<br \/>\n   pExitProcess, pDeleteFile, pFreeLibrary,pUnmapViewOfFile : pointer;<br \/>\n   ExitCode:UINT;<br \/>\nbegin<br \/>\n hModule:= GetModuleHandle(nil);<\/p>\n<p> GetModuleFileName(hModule, szModuleName, sizeof(szModuleName));<\/p>\n<p>     hKrnl32 := GetModuleHandle ( &#8216;kernel32&#8217; );<br \/>\n     pExitProcess := GetProcAddress ( hKrnl32, &#8216;ExitProcess&#8217; );<br \/>\n     pDeleteFile := GetProcAddress  ( hKrnl32, &#8216;DeleteFileA&#8217; );<br \/>\n     pFreeLibrary := GetProcAddress ( hKrnl32, &#8216;FreeLibrary&#8217; );<br \/>\n     pUnmapViewOfFile := GetProcAddress ( hKrnl32, &#8216;UnmapViewOfFile&#8217; );<\/p>\n<p> ExitCode := system.ExitCode;<\/p>\n<p> if($80000000 and GetVersion())<>0 then<br \/>\n  \/\/ Win95, 98, Me<br \/>\n  asm<\/p>\n<p>   lea     eax, szModuleName<br \/>\n   push    ExitCode<br \/>\n   push    0<br \/>\n   push    eax<br \/>\n   push    pExitProcess<br \/>\n   push    hModule<br \/>\n   push    pDeleteFile<br \/>\n   push    pFreeLibrary<br \/>\n   ret<br \/>\n  end<br \/>\n else<br \/>\n  begin<br \/>\n  CloseHandle(THANDLE(4));<\/p>\n<p>  asm<br \/>\n   lea     eax, szModuleName<br \/>\n   push    ExitCode<br \/>\n   push    0<br \/>\n   push    eax<br \/>\n   push    pExitProcess<br \/>\n   push    hModule<br \/>\n   push    pDeleteFile<br \/>\n   push    pUnmapViewOfFile<br \/>\n   ret<br \/>\n   end<br \/>\n  end<br \/>\nend;<br \/>\n[\/code]<\/p>\n<p>As far as I know it doesn&#8217;t work on XP.<\/p>\n<p>[tags]Delphi, Misc, Files, System[\/tags]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Q: how to let EXE delete itself while running? >I want to delete exe file while is opened. My friend did that in&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[11],"tags":[75,112,107,113],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p71Tml-2Z","_links":{"self":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts\/185"}],"collection":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/comments?post=185"}],"version-history":[{"count":0,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts\/185\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/media?parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/categories?post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/tags?post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}