To delete junk programs in the system

Source: Internet
Author: User
The program is used to delete system spam and there is a memory leak. Why am I still not clear? I posted the code. Let's see what the problem is.
The problem is that, if I create a loop in winmain and put the two functions that Delete the garbage into the loop for execution, the memory occupied by the program will go up, each cycle occupies about 7 MB of memory, and all the variables used by my program are local variables. Theoretically, when the function ends, the memory space occupied by the variables in the program is automatically released, after testing, the memory is not released. The reason is not found. It is depressing !!!!!

# Include "windows. H"

# Include "shlobj. H"
# Pragma comment (Lib, "shell32.lib") // required for the shgetspecialfolderpath Function

Void deldirallfile (char * path); // delete all files and folders in the path directory
Void delfilebytype (char * path, char * type); // delete the files whose path directory and all file types in its subdirectories are included in the type string, change type to customize the type of junk files
Void delsysrubbish (); // used to delete the garbage at a fixed position in the system
Void deldefrubbish (); // used to delete garbage of the custom type
Int winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow)
{
Delsysrubbish ();
Deldefrubbish ();
Return 0;
}

Void deldirallfile (char * path)
{
Handle hfile = NULL;
Char filepath [256] = {0}; // Save the search path
Char tempfile [256] = {0}; // name of the searched File
Strcpy (filepath, PATH );
Strcat (filepath ,"\\*.*");
Win32_find_data file_data = {0 };
Hfile = findfirstfile (filepath, & file_data );
Do
{
If (strcmp (file_data.cfilename, ".") = 0 | strcmp (file_data.cfilename, "..") = 0) // This directory and the parent directory
{
Memset (& file_data, 0, sizeof (file_data ));
Continue; // itself and the parent directory, ignore
}
Else
{
Memset (tempfile, 0,256 );
Strcpy (tempfile, PATH );
Strcat (tempfile ,"\\");
Strcat (tempfile, file_data.cfilename );
}
If (file_data.dwfileattributes = file_attribute_directory) // is a folder
{
Deldirallfile (tempfile); // recursively call to delete all files in the subfolders
Removedirectory (tempfile );
}
Else // is a file
Deletefile (tempfile );
Memset (& file_data, 0, sizeof (file_data ));
} While (findnextfile (hfile, & file_data ));
Closehandle (hfile );
}
Void delfilebytype (char * path, char * type)
{
Handle hfile = NULL;
Char filepath [256] = {0}; // Save the search path
Char filetype [8] = {0}; // save the file type
Char tempfile [256] = {0}; // name of the searched File
Strcpy (filepath, PATH );
Strcat (filepath ,"\\*.*");
Win32_find_data file_data = {0 };
Hfile = findfirstfile (filepath, & file_data );
Do
{
If (strcmp (file_data.cfilename, ".") = 0 | strcmp (file_data.cfilename, "..") = 0) // This directory and the parent directory
{
Memset (& file_data, 0, sizeof (file_data ));
Continue; // itself and the parent directory, ignore
}
Else // obtain the complete file path
{
Memset (tempfile, 0,256 );
Strcpy (tempfile, PATH );
Strcat (tempfile ,"\\");
Strcat (tempfile, file_data.cfilename );
}
If (file_data.dwfileattributes = file_attribute_directory) // is a folder,
{
If (stricmp (file_data.cfilename, "debug") = 0 | stricmp (file_data.cfilename, "release") = 0)
{
Deldirallfile (tempfile); // It is generally the garbage generated by the VC project. delete all the items in it, but it may be deleted by mistake.
Removedirectory (tempfile );
}
Else
Delfilebytype (tempfile, type );
}
Else
{
Memset (filetype, 0, 8 );
Strcpy (filetype, & file_data.cfilename [strlen (file_data.cfilename)-strlen (type)]); // copy the last four digits of the file name to obtain the file type
If (stricmp (type, filetype) = 0) // indicates that the object belongs to the type to be deleted.
Deletefile (tempfile );
}
Memset (& file_data, 0, sizeof (file_data ));
} While (findnextfile (hfile, & file_data ));
Closehandle (hfile );
}
Void delsysrubbish ()
{
Char path [256] = {0 };
// ------------- Temporary file -------------//
Memset (path, 0,256 );
Gettemppath (256, PATH );
Path [strlen (PATH)-1] = '\ 0 ';
Deldirallfile (PATH );

// --------------- Cookie -------------//
Memset (path, 0,256 );
Shgetspecialfolderpath (null, path, csidl_cookies, false );
Deldirallfile (PATH );

// --------------- History ------------//
Memset (path, 0,256 );
Shgetspecialfolderpath (null, path, csidl_history, false );
Deldirallfile (PATH );

// ------------- Ie cache ---------------//
Memset (path, 0,256 );
Shgetspecialfolderpath (null, path, csidl_internet_cache, false );
Deldirallfile (PATH );

// ------------ Recently opened record -----------//
Memset (path, 0,256 );
Shgetspecialfolderpath (null, path, csidl_recent, false );
Deldirallfile (PATH );

Memset (path, 0,256 );
Getwindowsdirectory (path, 256 );
Strcat (path ,"\\");
Strcat (path, "prefetch"); // I am not sure about the silk downloaded from this folder. I think Super Rabbit has been deleted, so I also deleted it.
Deldirallfile (PATH );
}
Void deldefrubbish ()
{
Char disk; // ~ Variables used in the Z Loop
Char diskname [6]; // storage disk path (for example, C :), 3 characters is enough, only 6 Geely points, haha,
Char path [256];
For (Disk = 'a'; disk <= 'Z'; disk ++)
{
Memset (diskname, 0, 6 );
Diskname [0] = disk;
Diskname [1] = ':';
If (getdrivetype (diskname )! = Drive_fixed, no write permission
Continue;

// There is a system volume information folder under each disk, and the space on G is used when the disk is moved. It stores the backup generated by the restoration of the Windows built-in system, which is not used by general users and wastes disks.
Memset (path, 0,256 );
Strcpy (path, diskname );
Strcat (path ,"\\");
Strcat (path, "system volume information ");
Deldirallfile (PATH );

// Search for garbage from the root directory of each drive letter
Delfilebytype (diskname, ". APS ");
Delfilebytype (diskname, ". NCB ");
Delfilebytype (diskname, ". PLG ");
Delfilebytype (diskname, ". Opt ");
Delfilebytype (diskname, ". CLW ");
Delfilebytype (diskname, ". Bak ");
Delfilebytype (diskname, ". PCH ");
Delfilebytype (diskname, ". tmp ");
Delfilebytype (diskname, ". _ MP ");
Delfilebytype (diskname, ". log ");
Delfilebytype (diskname, ". gid ");
Delfilebytype (diskname, ". Chk ");
Delfilebytype (diskname, ". Old ");
}
}

 

Http://www.debugman.com/discussion/1666/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.