[hide]#include <windows.h>
#include <tlhelp32.h>
#include <stdlib.h>
#include <stdio.h>
#include <ShlObj.h> //其中含有获取快捷方式路径的函数
#include <sys/stat.h> //获取文件相关操作的一些函数
//获取快捷方式的目标路径
BOOL GetShellPath(CHAR *src, CHAR *ShellPath){
BOOL bRet = FALSE;
CoInitialize(NULL); //初始化com接口
IShellLink *psl = NULL;
//创建COM接口,IShellLink对象创建
HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
if (SUCCEEDED(hr)){
IPersistFile *ppf;
hr = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
if (SUCCEEDED(hr)){
WCHAR wsz[MAX_PATH] = { 0 };
MultiByteToWideChar(CP_ACP, 0, src, -1, wsz, MAX_PATH);
hr = ppf->Load(wsz, STGM_READ);//加载文件
if (SUCCEEDED(hr)){
WIN32_FIND_DATA wfd;
psl->GetPath(ShellPath, MAX_PATH, (WIN32_FIND_DATA*)&wfd, SLGP_SHORTPATH); //获取目标路径
bRet = TRUE;
}
ppf->Release();
}
psl->Release();
}
CoUninitialize();
return bRet;
}
void GetProcessFileName(CHAR *szShellPath, CHAR *szTargetExeFile){
CHAR szExtension[MAX_PATH]; //文件后缀
CHAR szFileName[MAX_PATH]; //文件名称
_splitpath(szShellPath, NULL, NULL, szFileName, szExtension);
lstrcat(szTargetExeFile, szFileName);
lstrcat(szTargetExeFile, szExtension);
//这样就获取到了进程中的要删除快捷方式对应的进程名字
printf("\n\n\n%s\n\n\n", szTargetExeFile);
}
DWORD GetProcessID(LPSTR szProcessName){
DWORD dwProcessId = 0; // 用于记录进程的ID号
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(pe32);
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE){
printf("CreateToolhelp32Snapshot调用失败:%d\n", GetLastError());
return -1;
}
else{
BOOL bMore = Process32First(hProcessSnap, &pe32);
while (bMore){
if (lstrcmp(pe32.szExeFile, szProcessName) == 0){
dwProcessId = pe32.th32ProcessID;
}
bMore = Process32Next(hProcessSnap, &pe32);
}
CloseHandle(hProcessSnap);
}
return dwProcessId;
}
DWORD KillProcess(LPSTR szProcessName){
if (GetProcessID(szProcessName) != 0){ //判断进程是否存在
HANDLE hProcessToKill = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetProcessID(szProcessName));
if (hProcessToKill){
TerminateProcess(hProcessToKill, 0);
}
CloseHandle(hProcessToKill);
}
return 0;
}
DWORD DeleteTargetFile(LPSTR szDeleteFile){
DWORD dwChoose = MessageBox(NULL,
TEXT("请问是否要完全清除数据,用专业恢复数据软件也没有办法恢复!"),
TEXT("警告"),
MB_OKCANCEL | MB_ICONEXCLAMATION);
if (dwChoose == IDOK){
CHAR *buffer[1280] = { "这个文件恢复不了成原先的状态",
"数据无价,早知今日,何必当初",
"西瓜,苹果,香蕉,菠萝" };
HANDLE hFileWrite = CreateFile(szDeleteFile,
GENERIC_WRITE,
0,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFileWrite == INVALID_HANDLE_VALUE){
printf("文件写入句柄创建错误:%d\n", GetLastError());
return -1;
}
DWORD dwWriteDataSize;
for (int i = 0; i < 3; i++){
printf("进行第%d次覆写\n", i + 1);
if (!WriteFile(hFileWrite, buffer[i], lstrlen(buffer[i]), &dwWriteDataSize, NULL)){
printf("写入失败:%d\n", GetLastError());
return -1;
}
else{
printf("写入%d字节\n", dwWriteDataSize);
}
}
CloseHandle(hFileWrite); //关闭写入文件的句柄
DeleteFile(szDeleteFile); //删除文件
MessageBox(NULL, "彻底删除文件成功", "提示", MB_OK);
}
else{
if (MessageBox(NULL,
"这个数据是可以日后依靠专业的数据恢复软件进行恢复的,但是会直接从您的电脑上删除,不放入回收站,是否继续?",
"提示",
MB_OKCANCEL | MB_ICONQUESTION) == IDOK){
DeleteFile(szDeleteFile);
MessageBox(NULL, "删除成功", "提示", MB_OK);
}
}
return 0;
}
int main(int argc, char *argv[]){
CHAR* szDeleteFile = (CHAR*)malloc(sizeof(CHAR)*MAX_PATH);
DWORD dwChoose = MessageBox(NULL, "请问您删除的是快捷方式或者.exe文件么", "提示", MB_OKCANCEL | MB_ICONQUESTION);
if (dwChoose == IDOK){ //如果是快捷方式或者.exe文件
if (MessageBox(NULL, "请问您要删除的是快捷方式么?", "提示", MB_OKCANCEL | MB_ICONQUESTION) == IDOK){
printf("请输入您要删除的快捷方式的绝对路径:");
scanf("%s", szDeleteFile);
CHAR* szShellPath = (CHAR*)malloc(sizeof(CHAR)*MAX_PATH);
if (GetShellPath(szDeleteFile, szShellPath)){ //获取快捷方式对应的目标路径
CHAR* szTargetExeFile = (CHAR*)malloc(sizeof(CHAR)*MAX_PATH);
lstrcpy(szTargetExeFile, "");
GetProcessFileName(szShellPath, szTargetExeFile);
KillProcess(szTargetExeFile); //终止要删除的文件的进程
DeleteTargetFile(szDeleteFile); //删除快捷方式
//释放内存
free(szTargetExeFile);
free(szShellPath);
free(szDeleteFile);
}
}
else{
CHAR* szTargetExeFile = (CHAR*)malloc(sizeof(CHAR)*MAX_PATH);
lstrcpy(szTargetExeFile, "");
printf("请输入您要删除.exe文件的绝对路径:");
scanf("%s", szDeleteFile);
GetProcessFileName(szDeleteFile, szTargetExeFile);
KillProcess(szTargetExeFile); //终止进程
DeleteTargetFile(szDeleteFile);
free(szDeleteFile);
free(szTargetExeFile);
}
}
else{
printf("请输入您要删除文件的绝对路径:");
scanf("%s", szDeleteFile);
DeleteTargetFile(szDeleteFile);
}
return 0;
}
[/hide]