There are many ways to delete a program, but the final idea is to close the process itself and start a new process to delete itself. The following method is to use the command line program provided by windows to delete the file.
Command Behavior: cmd.exe/c del filename
--------------------------------------------------------------------------------
Sample Code: C Language
# Include <windows. h>
# Include <shellapi. h>
# Include <shlobj. h>
BOOL SelfDel ()
{
SHELLEXECUTEINFO sei;
TCHAR szModule [MAX_PATH], szComspec [MAX_PATH], szParams [MAX_PATH];
// Get its own file name. Get the full path file name of cmd
If (GetModuleFileName (0, szModule, MAX_PATH )! = 0 )&&
(Getdomainpathname (szModule, szModule, MAX_PATH )! = 0 )&&
(GetEnvironmentVariable ("COMSPEC", szComspec, MAX_PATH )! = 0 ))
{
// Set command parameters.
Lstrcpy (szParams, "/c del ");
Lstrcat (szParams, szModule );
Lstrcat (szParams, "> nul ");
// Set the structure member.
Sei. cbSize = sizeof (sei );
Sei. hwnd = 0;
Sei. lpVerb = "Open ";
Sei. lpFile = szComspec;
Sei. lpParameters = szParams;
Sei. lpDirectory = 0; sei. nShow = SW_HIDE;
Sei. fMask = SEE_MASK_NOCLOSEPROCESS;
// Create a cmd process.
If (ShellExecuteEx (& sei ))
{
// Set the invocation level of the cmd process to idle so that the program has enough time to exit from the memory.
SetPriorityClass (sei. hProcess, IDLE_PRIORITY_CLASS );
// Set the priority of the process to high
SetPriorityClass (GetCurrentProcess (), REALTIME_PRIORITY_CLASS );
SetThreadPriority (GetCurrentThread (), THREAD_PRIORITY_TIME_CRITICAL );
// Notify Windows resource browser that the program file has been deleted.
Shchangenoworkflow (shcn_delete, SHCNF_PATH, szModule, 0 );
Return TRUE;
}
}
Return FALSE;
}
---------------------------------------------------------------------------------
Code example: Assembly Form
. 386
. Model flat, stdcall
Option casemap: none
Include windows. inc
Include kernel32.inc
Includelib kernel32.lib
Include urlmon. inc
Includelib urlmon. lib
Include shell32.inc
Includelib shell32.lib
. Data?
SzModule db MAX_PATH dup (?)
SComspec db MAX_PATH dup (?); Auto-delete
SzParams db MAX_PATH dup (?)
HCurProcess dd?
HCurThread dd?
. Data
Sei SHELLEXECUTEINFO <0>
. Const
SzComspec db COMSPEC, 0
Verb1 db open, 0
SzCom1 db/c del, 0
SzCom2 db> nul, 0
. Code
Start:
Invoke GetModuleFileName, 0, addr szModule, MAX_PATH
Invoke GetEnvironmentVariable, addr szComspec, addr sComspec, MAX_PATH
Invoke lstrcpy, addr szParams, addr szCom1
Invoke lstrcat, addr szParams, addr szModule
Invoke lstrcat, addr szParams, addr szCom2
Mov sei. cbSize, sizeof sei; 1
Mov sei. hwnd, 0; 2
Mov sei. lpVerb, offset Verb1; 3
Lea eax, szParams
Mov sei. lpParameters, eax; 4
Lea eax, sComspec
Mov sei. lpFile, eax; 5
Mov sei. lpDirectory, 0
Mov sei. nShow, SW_HIDE
Mov sei. fMask, SEE_MASK_NOCLOSEPROCESS
Invoke ShellExecuteEx, addr sei
. If eax
Invoke SetPriorityClass, sei. hProcess, IDLE_PRIORITY_CLASS
Invoke GetCurrentProcess
Mov hCurProcess, eax
Invoke SetPriorityClass, hCurProcess, REALTIME_PRIORITY_CLASS
Invoke GetCurrentThread
Mov hCurThread, eax
Invoke SetThreadPriority, hCurThread, THREAD_PRIORITY_TIME_CRITICAL
Invoke shchangenoworkflow, shcn_delete, SHCNF_PATH, addr szModule, 0
& N