- # Include <Windows. h>
- # Include <stdlib. h>
- # Include <tchar. h>
- # Include <iostream. h>
- BOOL DeleteFolder (LPCTSTR lpszPath)
- {
- SHFILEOPSTRUCT FileOp;
- ZeroMemory (void *) & FileOp, sizeof (SHFILEOPSTRUCT ));
- FileOp. fFlags = FOF_NOCONFIRMATION;
- FileOp. hNameMappings = NULL;
- FileOp. hwnd = NULL;
- FileOp. lpszProgressTitle = NULL;
- FileOp. pFrom = lpszPath;
- Fileop. PTO = NULL;
- Fileop. wfunc = fo_delete;
- Return shfileoperation (& fileop) = 0;
- }
- Int main ()
- {
- If (_ argc = 1)
- {
- // Original EXE: spawn clone EXE to delete this exe
- // Copy this EXEcutable image into the user's temp directory
- Char del;
- Cout <"delete self or not? (Y/n)/n ";
- Cin> del;
- If (del = 'n ')
- Return 0;
- TCHAR szPathOrig [_ MAX_PATH], szPathClone [_ MAX_PATH];
- Getmodulefilename (null, szpathorig, _ max_path );
- Gettemppath (_ max_path, szpathclone );
- Gettempfilename (szpathclone, _ text ("Del"), 0, szpathclone );
- Copyfile (szpathorig, szpathclone, false );
- // *** Note ***:
- // Open the clone EXE using file_flag_delete_on_close
- Handle hfile = createfile (szpathclone, 0, file_assist_read, null, open_existing, file_flag_delete_on_close, null );
- // Spawn the clone EXE passing it our EXE's Process Handle
- // And the full path name to the original EXE file.
- Tchar szcmdline [512];
- Handle hprocessorig = OpenProcess (synchronize, true, getcurrentprocessid ());
- Wsprintf (szcmdline, _ text ("% S % d/" % S/""), szpathclone, hprocessorig, szpathorig );
- Startupinfo Si;
- Zeromemory (& Si, sizeof (SI ));
- Si. cb = sizeof (SI );
- Process_information PI;
- CreateProcess (null, szcmdline, null, null, true, 0, null, null, & Si, & PI );
- Closehandle (hprocessorig );
- Closehandle (hfile );
- // This original process can now terminate.
- }
- Else
- {
- // Clone EXE: When original EXE terminates, delete it
- Handle hprocessorig = (handle) _ ttoi (_ targv [1]);
- WaitForSingleObject (hProcessOrig, INFINITE );
- CloseHandle (hProcessOrig );
- Char str [256];
- Memset (str, 0,256 );
- Strcpy (str ,__ targv [2]);
- Int len = strlen (str );
- While (true)
- {
- If (str [len] = '//')
- {
- Str [len] = '/0 ';
- Break;
- }
- Str [len] = '/0 ';
- Len --;
- }
- DeleteFolder (str );
- // DeleteFile (_ targv [2]);
- // Delete the clone one
- SHELLEXECUTEINFO sei;
- TCHAR szModule [MAX_PATH], szComspec [MAX_PATH], szParams [MAX_PATH];
- // Obtain the file path name
- If (GetModuleFileName (0, szModule, MAX_PATH )! = 0 )&&
- (Getdomainpathname (szModule, szModule, MAX_PATH )! = 0 )&&
- (GetEnvironmentVariable ("COMSPEC", szComspec, MAX_PATH )! = 0 ))
- {// Set command line parameters.
- Lstrcpy (szParams, "/c del"); FindWindow (NULL, NULL );
- Lstrcat (szParams, szModule );
- Lstrcat (szParams, "> nul ");
- // Initialize the SHELLEXECUTEINFO structure member
- Sei. cbSize = sizeof (sei); // set the type size.
- // Process handle in the Command window, which is set when the ShellExecuteEx function is executed.
- Sei. hwnd = 0;
- Sei. lpVerb = "Open"; // The execution action is "Open execution ".
- Sei. lpFile = szComspec; // full path name of the execution program file.
- Sei. lpParameters = szParams; // execution parameters.
- Sei. lpDirectory = 0;
- // Display mode. Hide the mode to prevent the command window from appearing.
- Sei. nShow = SW_HIDE;
- // Set SellExecuteEx to exit after the function is complete.
- Sei. fmask = see_mask_nocloseprocess;
- // Create an execution command window process.
- If (shellexecuteex (& SEI ))
- {// Set the execution level of the command line process to idle, which gives the program enough time to exit from the memory.
- Setpriorityclass (SEI. hprocess, idle_priority_class );
- // Set the execution level of the program process to real-time execution. This program immediately obtains the CPU execution right and quickly exits.
- Setpriorityclass (getcurrentprocess (), realtime_priority_class );
- Setthreadpriority (getcurrentthread (), thread_priority_time_critical );
- }
- }
- }
- // Insert code here to remove the subdirectory too (if desired ).
- // The system will delete the clone EXE automatically
- // Because it was opened with FILE_FLAG_DELETE_ON_CLOSE
- Return (0 );
- }
The anti-installation prototype was rewritten by referring to two articles on the Internet. Thank you for sharing your experience.
There are at least two difficulties for the anti-Installer: one is to delete yourself, and the other is to delete the folder where you are located.
On the "webnumen technology life blog", I found a method for the program to delete itself, but it still cannot be implemented to let the program delete its own folder, even if you run the compiled program in vc6.0, you can delete the Debug folder where you are located. However, if you run a generated program directly, you cannot delete your own folder. At most, you can delete all the files and folders in the same directory as yourself.
Therefore, I found the "C + excellent exploration column" and solved the second difficulty by referring to his code.
The idea is to copy your own attachment to the temporary directory, start the attachment, pass your ID and path as parameters to the attachment, and let yourself exit normally. The appendix to be started is to delete the folder where the original program is located based on the parameters passed to the original program. Delete others and then kill yourself. For details about how to kill yourself, refer to "webnumen technology life blog"
Webnumen technology life blog
Http://webnumen.blog.hexun.com/19268347_d.html
C + refined Column
Http://blog.csdn.net/dpfordor/archive/2008/01/10/2032954.aspx