RT:
创建:
void CreateShortcut(LPCTSTR lpszExe, LPCTSTR lpszLnk) { ::CoInitialize(NULL); IShellLink *psl = NULL; IPersistFile *ppf = NULL; HRESULT hr = ::CoCreateInstance( CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); if (!SUCCEEDED(hr)) { return; } psl->SetPath(lpszExe); //psl->SetArguments(); //psl->SetDescription(); //psl->SetHotkey(); //psl->SetIconLocation(); //psl->SetShowCmd(); TCHAR szWorkPath[MAX_PATH] = {0}; ::lstrcpy(szWorkPath, lpszExe); LPTSTR lp = szWorkPath; while (*lp) { lp++; } while (_T('\\')!=(*lp)) { lp--; } *lp = 0; psl->SetWorkingDirectory(szWorkPath); hr = psl->QueryInterface( IID_IPersistFile, (LPVOID*)&ppf); if (!SUCCEEDED(hr)) { return; } USES_CONVERSION; ppf->Save(T2COLE(lpszLnk), TRUE); if (ppf) { ppf->Release(); } if (psl) { psl->Release(); } ::CoUninitialize(); }
解析:
void GetLinkPath(CString szLink, CString& szPath) { CoInitialize(NULL); szPath = _T(""); IShellLink* psl = NULL; TCHAR szGotPath[MAX_PATH] = {0}; WIN32_FIND_DATA wfd = {0}; HRESULT hr = ::CoCreateInstance( CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); if (!SUCCEEDED(hr)) { return; } IPersistFile* ppf = NULL; hr = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); if (!SUCCEEDED(hr)) { return; } hr = ppf->Load(szLink.GetBuffer(), STGM_READ); szLink.ReleaseBuffer(); if (!SUCCEEDED(hr)) { return; } hr = psl->Resolve(0, 0); if (!SUCCEEDED(hr)) { return; } hr = psl-> GetPath(szGotPath, MAX_PATH, (WIN32_FIND_DATA*)&wfd, SLGP_SHORTPATH); if (SUCCEEDED(hr)) { szPath = szGotPath; } ppf->Release(); psl->Release(); CoUninitialize(); }
调用:
CreateShortcut( _T("C:\\WINDOWS\\system32\\notepad.exe"), _T("C:\\Documents and Settings\\guoyou.huang\\桌面\\hgy的记事本.lnk") ); CString szPath; GetLinkPath(_T("C:\\Documents and Settings\\guoyou.huang\\桌面\\我的记事本.lnk"), szPath);
随手写的,标记下,