获取快捷方式的目标文件,并删除无效快捷方式和目标文件不在系统盘的快捷方式

来源:互联网 发布:长安app软件下载 编辑:程序博客网 时间:2024/05/22 06:42
// 获取快捷方式的目标文件,并删除无效快捷方式和目标文件不在系统盘的快捷方式BOOL GetExePath(LPCTSTR lnkPath, LPTSTR exePath, LPTSTR Arguments){DWORD dwAttribute = ::GetFileAttributes(lnkPath);if(dwAttribute & FILE_ATTRIBUTE_READONLY){dwAttribute &= ~FILE_ATTRIBUTE_READONLY;SetFileAttributes(lnkPath,dwAttribute);}HRESULT hr = CoInitialize(NULL);if(SUCCEEDED(hr)){IShellLink *pisl;hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pisl);if(SUCCEEDED(hr)){IPersistFile * ppf ;HRESULT hr = pisl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);if(SUCCEEDED(hr)){hr = ppf->Load(lnkPath, STGM_READWRITE);if(S_OK != hr)// 快捷方式无效(快捷方式文件中的内容无效),删除快捷方式{DeleteFile(lnkPath);pisl->Release();ppf->Release();CoUninitialize();return FALSE;}WIN32_FIND_DATA wfd;hr = pisl->GetPath(exePath, MAX_PATH, &wfd, 0);if(S_OK != hr)// 快捷方式无效(快捷方式文件中无内容,即为空文件),删除快捷方式{DeleteFile(lnkPath);pisl->Release();ppf->Release();CoUninitialize();return FALSE;}if(!PathFileExists(exePath))// 快捷方式指向的目标文件不存在,删除快捷方式{DeleteFile(lnkPath);pisl->Release();ppf->Release();CoUninitialize();return FALSE;}CString strSystemDrive;if(!GetSystemDrive(strSystemDrive)){pisl->Release();ppf->Release();CoUninitialize();return FALSE;}CString strExePath = exePath;if(strExePath.Left(1).MakeUpper() != strSystemDrive.Left(1).MakeUpper())// 快捷方式指向的目标文件不在系统盘,删除快捷方式{DeleteFile(lnkPath);pisl->Release();ppf->Release();CoUninitialize();return FALSE;}pisl->GetArguments(Arguments, MAX_PATH);pisl->Release();ppf->Release();}}CoUninitialize();}return TRUE;}

0 0
原创粉丝点击