There are many ways to do self-deletion, the most common is to implement the self-deletion through batch processing.
Batch processing enables the ability to delete itself after running.
Lets the program create batches, which are then removed by the batch process and the batch itself.
: Loop del "D:\Program file\ ..." ifexist goto loopdel%0
The functions used to create and run batch files are also different, and here's my personal example.
1#include <Windows.h>2#include <stdio.h>3 4 intMain ()5 {6 Charszfile[_max_path]={0}; 7 CharSztem[max_path];8 CharSzdelcat[max_path];9GetModuleFileName (Null,szfile,max_path);//gets the absolute path of the programTen Onestrcpy (Szdelcat,": loop\r\n"); Asprintf (Sztem,"del \ "%s\" \ r \ n", szfile); - strcat (szdelcat,sztem); -sprintf (Sztem,"if exist \ "%s\" goto loop\r\n", szfile); the strcat (szdelcat,sztem); -sprintf (Sztem,"del%%0\r\n"); - strcat (szdelcat,sztem); - + Charszbat[max_path]="Del.bat";//Batch file name -HANDLE hbatfile = CreateFile (Szbat, Generic_write,0,0, Create_always,0,0); + A DWORD Dwnum; atWriteFile (Hbatfile, Szdelcat, Lstrlen (szdelcat) +1, &Dwnum, NULL); - CloseHandle (Hbatfile); -ShellExecute (0,0, Szbat,0,0, sw_hide); -ExitProcess (0); - return 0; -}
When using strcat to connect two strings, it is important to note that if the target string is not assigned, the first copied string should use the strcpy function to the specified location.
Self-deletion of programs under Windows