打发时间时写的,菜的不能再菜的方法,高手莫笑哦
#include "windows.h"
#include "stdio.h"
#include "KillFile.h"
//#pragma comment(linker, "/ENTRY:KillFile")
//#pragma comment(linker, "/subsystem:windows")
#pragma comment(linker, "/BASE:0x13150000")
/*
函数名 :KillFileName
函数作用:消除指定文件的文件名
参数1 :要操作的文件具体路径
参数2 :覆盖的次数
参数3 :接收更改后的文件名
参数4 :开始时的文件名
返回值 :TRUE 表示成功,否则失败
备注 :把文件名改为类似 a b c 达到隐藏文件名的目的
*/
BOOL KillFileName(char *pFileName, int Repeat ,char *NewFileName,char FileChar = 'a')
{
char *pPath = new char[100];
ZeroMemory(pPath,100);
char *pOldFileName = new char[100];
ZeroMemory(pOldFileName , 100);
char *pNewFileName = new char[100];
ZeroMemory(pNewFileName , 100);
pNewFileName[0] = '\\';
// char FileChar = 'a';
int NameLength = strlen(pFileName);
for(int i = NameLength - 1 ; i >= 0 ;i--)
{
if( '\\' == pFileName[i] )
{
pFileName[i] = 0;
strcpy(pPath,pFileName);
pFileName[i] = '\\';
strcpy(pOldFileName,&pFileName[i]);
break;
}
}
// ZeroMemory(NewFileName,strlen(NewFileName));
char *tempPath = new char[100];
ZeroMemory(tempPath,100);
strcpy(tempPath,pPath);
char *tempNewPath = new char[100];
char *tempOldPath = new char[100];
ZeroMemory(tempOldPath,100);
ZeroMemory(tempNewPath,100);
pNewFileName[1] = FileChar;
for(i = 0; i < Repeat ; i++)
{
strcpy(tempNewPath,tempPath);
strcpy(tempOldPath,tempPath);
strcat(tempOldPath,pOldFileName);
strcat(tempNewPath,pNewFileName);
MoveFile(tempOldPath,tempNewPath);
ZeroMemory(pOldFileName,100);
strcpy(pOldFileName,pNewFileName);
strcpy(NewFileName,tempPath);
strcat(NewFileName,pNewFileName);
if('z' == ++(pNewFileName[1]))
{
pNewFileName[1] = 'a';
}
ZeroMemory(tempNewPath,100);
ZeroMemory(tempOldPath,100);
}
// printf("pPath = %s, pOldFileName = %s \n",pPath,pOldFileName);
delete []tempNewPath;
delete []tempOldPath;
delete []pPath;
delete []pOldFileName;
delete []pNewFileName;
delete []tempPath;
return TRUE;
}
/*
函数名 :KillFile
函数作用:粉碎指定文件
参数1 :待粉碎的文件具体路径
参数2 :覆盖文件数据的次数
返回值 :TRUE 表示成功,否则失败
备注 :现为dll的导出函数
*/
extern "C" __declspec(dllexport)BOOL KillFile(char *pFileName,int Repeat)
{
//文件的载入工作在这里开始
int RepeatNum = Repeat;
HANDLE hFile = CreateFile(pFileName,GENERIC_WRITE | GENERIC_READ,NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(INVALID_HANDLE_VALUE == hFile)
{
MessageBox(NULL,"Has an error on call the CreateFile function","error",0);
return FALSE;
}
HANDLE hMap = CreateFileMapping(hFile,NULL,PAGE_READWRITE | SEC_COMMIT,0,0,NULL);
DWORD LoadSize = 0;
char* pBuffer = (char *)MapViewOfFile(hMap,FILE_MAP_ALL_ACCESS,0,0,0);
if(NULL == pBuffer)
{
MessageBox(NULL,"Has an error on call the MapViewOfFile function","Error",0);
return FALSE;
}
LoadSize = GetFileSize(hFile,NULL); //取得文件的长度
//粉碎工作在下面进行
for(int i = 0 ;i < RepeatNum ; i++)
{
//写一遍0xff 然后再写一遍0x00
memset((LPVOID)(pBuffer),0xff,LoadSize);
ZeroMemory((LPVOID)(pBuffer),LoadSize);
FlushViewOfFile((LPVOID)(pBuffer),LoadSize);
}
//粉碎文件数据后的收尾工作
if(FALSE == UnmapViewOfFile((LPVOID)pBuffer))
{
MessageBox(NULL,"Has an error on call the UnMapViewOfFile function","Error",0);
return FALSE;
}
CloseHandle(hMap);
CloseHandle(hFile);
/* //这里对文件名进行处理
char *pOldFileName = new char [50];
char *pNewFileName = new char [50];
ZeroMemory(pOldFileName,50);
ZeroMemory(pNewFileName,50);
for(i = 0 ; i < 3 ; i++)
{
}
delete []pOldFileName;
delete []pNewdFileName;
*/
char *NewFileName = new char[100];
ZeroMemory(NewFileName,100);
if( FALSE == KillFileName(pFileName,Repeat,NewFileName) )
{
//do something when clear file name failure here
}
DeleteFile(NewFileName);
// printf("\nNewFileName = %s \n",NewFileName);
delete []NewFileName;
return TRUE;
}