BOOL CheckShortcut(LPCTSTR lpFileLink)
{
IShellLink *pLink = NULL;
IPersistFile *ppf = NULL;
BOOL bFind = FALSE;
HRESULT hr = CoInitialize(NULL); //Init COM
BOOL bCleanupCOM = FALSE;
bCleanupCOM = SUCCEEDED(hr);
// if(SUCCEEDED(hr)){
hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pLink); //create a COM object
if(SUCCEEDED(hr)) {
hr = pLink->QueryInterface(IID_IPersistFile, (void**)&ppf);
if(SUCCEEDED(hr)) {
char lnk[MAX_PATH] = {0};
strcat(lnk, lpFileLink);
WCHAR wpath[MAX_PATH] = {0};
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lnk, -1, wpath, MAX_PATH);
hr = ppf->Load(wpath, STGM_READ);
if(SUCCEEDED(hr)) {
hr = pLink->Resolve(NULL, 0);
if(SUCCEEDED(hr)) {
char path[MAX_PATH];
WIN32_FIND_DATA wfd;
hr = pLink->GetPath(path, MAX_PATH,(WIN32_FIND_DATA*)&wfd, SLGP_SHORTPATH);
if(SUCCEEDED(hr)) {
if (strstr(path,_T("nobol.exe")) != NULL){
bFind = TRUE;
}
}
}
}
ppf->Release();
}
pLink->Release();
}
if( bCleanupCOM ) {
CoUninitialize();
}
// }
return bFind;
}
void CMainFrame.:ChageShortcutName()
{
TCHAR lpDesktop[MAX_PATH];
SHGetSpecialFolderPath(0, lpDesktop, CSIDL_DESKTOPDIRECTORY, 0); //get path of desktop
TCHAR lpLinkPath[MAX_PATH];
strcpy(lpLinkPath, lpDesktop);
strcat(lpLinkPath, _T("\\"));
strcat(lpLinkPath, _T("*.lnk"));
TCHAR lpChange[MAX_PATH];
strcpy(lpChange, lpDesktop);
strcat(lpChange, _T("\\"));
CString strTitle;
strTitle.LoadString(AFX_IDS_APP_TITLE);
strcat(lpChange, strTitle);
strcat(lpChange, _T(".lnk")); //the name need to be changed
BOOL bChanged = FALSE;
WIN32_FIND_DATA wfd;
HANDLE hFind = FindFirstFile(lpLinkPath, &wfd); //find shortcut in desktop
if(hFind != INVALID_HANDLE_VALUE) {
do {
TCHAR szFile[MAX_PATH] = {0};
wsprintf(szFile, "%s\\%s", lpDesktop, wfd.cFileName);
if (CheckShortcut(szFile) != FALSE) { //find the target shortcut which need to change
if (bChanged == FALSE) {
CFile::Rename(szFile, lpChange); //rename shortcut
bChanged = TRUE;
}
else {
CFile::Remove(szFile); //delete other shortcut with the same target link
}
}
}while(FindNextFile(hFind, &wfd));
}
FindClose(hFind);
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/677997/viewspace-667299/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/677997/viewspace-667299/