How to easily delete executable files
The executable file is the handle opened by the system when the EXE file is running. In this case, some operations on the file are forbidden by the system, such as the delete operation. However, in some cases, the program may need to have the function of self-deletion, that is, to delete itself after the program runs successfully. Based on this idea, there can be a very simple way to implement this basic function.
This method implements the delete function based on two points. One is to use Windows command program to delete files; the other is to start a new process to perform this delete operation. The following is a detailed description.
Command Program in Windows is a system shell program. in Windows95/98/me, its file name is command.com, while in NT/2000/XP, It is cmd.exe. We can obtain the full path name through the Environment Variable comspec.
Assume that we are currently using XP, and enter:
Cmd.exe /?
The command shell usage is obtained./C indicates that the command specified by the string is executed and the command is terminated. This is exactly what we need. The command to delete an object using command shell is as follows:
Cmd.exe/C del mypro.exe
Note that the file name should be a short file name (the file name cannot exceed 8 characters, and the suffix cannot exceed 3 characters ). If the actual file is a long file sentence, we can use the getmediapathname API function in the program for conversion.
The next step is how to successfully execute this command in a new process. The commands for starting a new process mainly include ShellExecute and CreateProcess.
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 compilation, the file is run successfully and deleted after the file is run. However, after multiple experiments, it is found that sometimes files are not deleted after execution. The analysis shows that the executable file is not closed when the delete operation is executed. That is to say, only the process that executes the delete operation can complete the operation after the process that executes the file is closed. In this case, the system is responsible for the scheduling and execution of processes and threads. We cannot manually define the process or thread to execute in a certain order.
My solution is to set the process for executing the delete operation as a pending state, so as to set a low priority level for the process and improve the process level of the execution file, then start the new process. In this way, the two processes can be executed successively. In this way, the new solution is to use CreateProcess to create a new process with the create_suspend flag, and then use setpriorityclass to set the corresponding priority. The priority of the main process is high_priority_class, the priority of the Process executing the delete operation is idle_priority_class. After hundreds of tests, the deletion operation is successful.
The following is a function that encapsulates the delete operation. The function starts a process to execute the del command of the command shell. You can call it at the end of the program to implement the auto-deletion function of the program.
# Include <windows. h> # include <shellapi. h> # include <stdio. h> int deletemyexe () {tchar tcsexename [max_path]; tchar tcsparam [max_path * 2]; tchar tcscmd [max_path]; handle hprocess = NULL; // get EXE filename and command shell programif (0 = getmodulefilename (null, tcsexename, max_path) | 0 = getenvironmentvariable (_ T ("comspec"), tcscmd, max_path) failret; // get short filename for command shell programif (0 = getaskpathname (tcsexename, tcsexename, max_path) failret; // create a command process, set its priority, then 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_suincluded, null, null, & Si, & PI) {return getlasterror ();} // heigthen priority of the current processsetpriorityclass (getcurrentprocess (), expiration); // set file attribute to normal setfileattributes (tcsexename, file_attribute_normal); // depress priority of command process, then start itsetpriorityclass (Pi. hprocess, idle_priority_class); resumethread (Pi. hthread); Return 0 ;}