How to achieve a simple executable file self-deletion of author: zyq654321
The executable, an exe file, manages its open handle during the run. At this point some of the operations of the file are prohibited by the system, such as delete operations. However, in some occasions, it may be necessary to have a function of self-deleting, which is to delete itself after the program runs. Based on this idea, there is a very simple way to achieve this basic function.
This method implements delete function based on two points. First, use Windows's COMMAND Program to delete file operations; Let's carefully explain it.
The Command Program in Windows is a system's shell program. In Windows95 / 98 / ME, its file is called Command.com, and in NT / 2000 / XP is cmd.exe. We can get its full path name through the environment variable COMSPEC.
Assume that we are using XP, entered in the command line:
cmd.exe /?
That is, it is obtained by the use of the Command shell; where the meaning of / c is: execute the command specified by the string and then end up, this is exactly what we need. This uses the command shell to delete a file as follows:
cmd.exe / c del mypro.exe
Here you should pay attention to a point, the file name should be a short message name (the file name must not exceed 8 characters, the suffix does not exceed 3 characters). If the actual file is a long file sentence, then we can use the getShortPathName this API function to convert.
Next we have to do this instruction in a new process. A new process is mainly SHELLEXECUTE and CREATEPROCESS.
First use shellexecute as an example. Use the following statement at the end of the program:
Shellexecute (NULL, "Open", "cmd.exe", "/ c del mypro.exe", null, sw_hide;
After compiling, the file is found, and the file is running after the file is running. But after doing multiple experiments later, it is found that sometimes the file is executed and will not be deleted. Through analysis, it is considered that the executable is not closed when the delete operation is executed. That is to say only after the process of executing the file is turned off, the process of executing the deleted operation can be completed. This has a problem, the system is responsible for the scheduling execution of the process and threads, and we can't perform in a certain order to predetermined processes or threads.
Solution to this is that the process of establishing a process of executing the delete operation is a suspend state, thus setting a low priority level, while increasing the process level of executing files, and then formally starting a new process. This can basically guarantee the success of both processes. This new solution is to create a new process with CreateProcess, and then use setPriorityClass to set the appropriate priority. The priority of the main process is high_priority_class, and the priority of the process of performing the delete operation is IDLE_PRIORITY_CLASS. After hundreds of tests, the deletion operation is successful. Below is a function that encapsulates the delete operation, and the function is started to execute the DEL command for the Command shell. Call it at the end of the program, you can simply implement the program's self-deduplication.
#include
#include
#include
Int deleteMyexe ()
{
Tchar tcsexename [MAX_PATH];
TCHAR TCSPARAM [MAX_PATH * 2];
TCHAR TCSCMD [MAX_PATH];
Handle HProcess = NULL;
// Get EXE FileName and Command shell Program
IF (0 == getModuleFileName (null, tcsexename, max_path)
|| 0 == GetENVIRONMENTVARIABLE (_T ("COMSPEC"), TCSCMD, MAX_PATH)
Failret;
// Get Short FileName for Command shell Program
IF (0 == getshstpathname (tcsexename, tcsexename, max_path))
Failret;
// CREATE A CommIAnd Process, Set ITS Priority, The Start IT.
Startupinfo Si;
Process_information pi;
ZeromeMory (& Si, SIZEOF (Si));
Si.cb = sizeof (si);
Si.dwflags = startf_useshowwindow;
Si.WShowWindow = sw_hide;
ZeromeMory (& Pi, SizeOf (PI));
_Stprintf (tcsparam, _t ("% s / c del% s"), tcscmd, tcsexename;
IF (! CREATEPROCESS (NULL,
TCSPARAM,
NULL,
NULL,
False,
CREATE_SUSPENDED,
NULL,
NULL,
& Si,
& pi)
{
Return getLastError ();
}
// Heigthen Priority of The Current Process
SetPriorityClass (GetCurrentProcess (), high_priority_class;
// set file attribute to Normal
SetFileAttributes (tcsexename, file_attribute_normal);
// Depress Priority of Command Process, Then Start IT
SetPriorityClass (pi.hprocess, idle_priority_class);
ResumeThread (pi.hthread);
Return 0;
}