Delphi Self-Determination Program

How to automatically uninstall my Delphi program? I tried this code:

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; 

But this will not work, do not delete the file. My program is a console. Thanks!

+6
source share
1 answer

Your code will only work under Windows NT and 2000. Since the system stores a link to the usermode descriptor for a memory mapped file that supports an executable image on disk in these OSs. This descriptor always has a value of 0x4 in these versions of Windows.

The most effective way to delete your own exe is to create a child process in a suspended state, enter the code to wait for the parent process (exe to delete), and then detect when the parent process ends, delete the parent process and finally kill the child process.

You can find more about this topic in these recommended resources.

+13
source

Source: https://habr.com/ru/post/918996/


All Articles