CString FileMisc::ResolveShortcut(LPCTSTR szShortcut) { // start by checking its a valid file if (!FileExists(szShortcut)) { return _T(""); } CoInitialize(NULL); HRESULT hResult; IShellLink* psl; CString sTarget(szShortcut); hResult = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); if (SUCCEEDED(hResult)) { LPPERSISTFILE ppf; hResult = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); if (SUCCEEDED(hResult)) { hResult = ppf->Load(ATL::CT2OLE(szShortcut), STGM_READ); if (SUCCEEDED(hResult)) { hResult = psl->Resolve(NULL, SLR_ANY_MATCH | SLR_NO_UI); if (SUCCEEDED(hResult)) { TCHAR szPath[MAX_PATH]; WIN32_FIND_DATA wfd; //fabio_2005 #if _MSC_VER >= 1400 _tcscpy_s(szPath, szShortcut); #else _tcscpy(szPath, szShortcut); #endif hResult = psl->GetPath(szPath, MAX_PATH, (WIN32_FIND_DATA*)&wfd, SLGP_SHORTPATH); if (SUCCEEDED(hResult)) { sTarget = CString(szPath); } } } ppf->Release(); } psl->Release(); } CoUninitialize(); return sTarget; }
HRESULT ShellFunctions::ResolveShortcut(HWND hWnd,LPCSTR pszShortcutFile,LPSTR pszPath) { HRESULT hres; IShellLink* psl; pszPath[0]='\0'; hres=CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(void**)&psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; hres=psl->QueryInterface(IID_IPersistFile,(void**)&ppf); if (SUCCEEDED(hres)) { WCHAR wsz[MAX_PATH]; MultiByteToWideChar(CP_ACP,0,pszShortcutFile,-1,wsz,MAX_PATH); hres=ppf->Load(wsz,STGM_READ); if (SUCCEEDED(hres)) { hres=psl->Resolve(hWnd,SLR_ANY_MATCH); if (pszPath!=NULL && SUCCEEDED(hres)) hres=psl->GetPath(pszPath,MAX_PATH,NULL,0); } ppf->Release(); } psl->Release(); } return hres; }
BOOL ResolveShortcut(TCHAR *shortcut, TCHAR *file) { IShellLink* psl = NULL; HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) &psl); if (SUCCEEDED(hr)) { IPersistFile* ppf = NULL; hr = psl->QueryInterface(IID_IPersistFile, (void **) &ppf); if (SUCCEEDED(hr)) { hr = ppf->Load(shortcut, STGM_READ); if (SUCCEEDED(hr)) { hr = psl->Resolve(NULL, SLR_UPDATE); if (SUCCEEDED(hr)) { WIN32_FIND_DATA wfd; hr = psl->GetPath(file, MAX_PATH, &wfd, SLGP_RAWPATH); } } ppf->Release(); } psl->Release(); } return SUCCEEDED(hr); }
void CEzShortcutDlg::OnDropFiles(HDROP hDropInfo) { TCHAR szPathName[MAX_PATH]; // 드롭된 파일의 갯수 int nFiles=DragQueryFile(hDropInfo, 0xFFFFFFFF, szPathName, MAX_PATH); if( nFiles != 1) { AfxMessageBox(_T("This application is not support multi-file drag & drop")); return; } for(int index=0 ; index < nFiles ; index++) { DragQueryFile(hDropInfo, index, szPathName, MAX_PATH); // 파일의 경로 얻어옴 std::wstring strExt = light::get_file_ext(szPathName); if( strExt == _T("lnk" )) { IShellLink *link; IPersistFile *pfile; BSTR szLinkPath; CString szPrePath; TCHAR szBuffer[MAX_PATH]; CString fname = szPathName; if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&link))) { link->QueryInterface(IID_IPersistFile, (void **)&pfile); szLinkPath = fname.AllocSysString(); pfile->Load(szLinkPath, STGM_READ); SysFreeString(szLinkPath); link->Resolve(NULL, NULL); link->GetPath(szBuffer, MAX_PATH, NULL, 0); // 리스트 박스나 메세지 박스로 해당 경로 값을 불러온다. (szBuffer) _tcsncpy_s(szPathName , szBuffer, _TRUNCATE); pfile->Release(); link->Release(); } } SHORTCUT_INFO ShortcutInfo(GetSafeHwnd()); ShortcutInfo.m_strShortcutName = light::get_file_name_without_ext(szPathName); ShortcutInfo.m_strProgramPath = szPathName; if( true == OnEditShortcutDlg(ShortcutInfo) ) { AddShortCutInfo(ShortcutInfo); RebuildShortcutList(); AutoSave(); } } DragFinish(hDropInfo); CDialog::OnDropFiles(hDropInfo); }
HRESULT ResolveShortcut(const TCHAR* LnkFile, TCHAR* FilePath, TCHAR* LnkDesc, TCHAR* WorkDir) { CoInitialize(NULL); HRESULT hres; IShellLink* psl; WIN32_FIND_DATA wfd; TCHAR strfilepath[MAX_PATH]; TCHAR strlnkdesc[INFOTIPSIZE]; TCHAR strworkdir[MAX_PATH]; USES_CONVERSION; hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; hres = psl->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf); if (SUCCEEDED(hres)) { hres = ppf->Load(LnkFile, STGM_READ); if (SUCCEEDED(hres)) { hres = psl->Resolve(GetDesktopWindow(), 0); if (SUCCEEDED(hres)) { hres = psl->GetPath(strfilepath,MAX_PATH, &wfd, SLGP_UNCPRIORITY ); if (SUCCEEDED(hres)) { _tcscpy(FilePath, strfilepath); hres = psl->GetDescription(strlnkdesc,INFOTIPSIZE); } if (SUCCEEDED(hres)) { _tcscpy(LnkDesc,strlnkdesc); hres = psl->GetWorkingDirectory(strworkdir,MAX_PATH); } if (SUCCEEDED(hres)) { _tcscpy(WorkDir,strworkdir); } } } ppf->Release(); } psl->Release(); } CoUninitialize(); return hres; }
BOOL AFXAPI AfxResolveShortcut(CWnd* pWnd, LPCTSTR lpszFileIn, LPTSTR lpszFileOut, int cchPath) { USES_CONVERSION; AFX_COM com; IShellLink* psl; *lpszFileOut = 0; // assume failure // WINBUG: Win32s versions prior to Win32s 1.3b do not restore the // stack pointer correctly in SHGetFileInfo. All it does is return // failure anyway on Win32s, so here we just avoid calling it. if (afxData.bWin31) return FALSE; SHFILEINFO info; if ((SHGetFileInfo(lpszFileIn, 0, &info, sizeof(info), SHGFI_ATTRIBUTES) == 0) || !(info.dwAttributes & SFGAO_LINK)) { return FALSE; } if (FAILED(com.CreateInstance(CLSID_ShellLink, NULL, IID_IShellLink, (LPVOID*)&psl))) { return FALSE; } IPersistFile *ppf; if (SUCCEEDED(psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf))) { if (SUCCEEDED(ppf->Load(T2COLE(lpszFileIn), STGM_READ))) { /* Resolve the link, this may post UI to find the link */ if (SUCCEEDED(psl->Resolve(pWnd->GetSafeHwnd(), SLR_ANY_MATCH))) { //#ifndef _UNICODE psl->GetPath(lpszFileOut, cchPath, NULL, 0); /*#else char szTemp[_MAX_PATH]; psl->GetPath(szTemp, _MAX_PATH, NULL, 0); _mbstowcsz(lpszFileOut, szTemp, cchPath); #endif*/ return TRUE; } } ppf->Release(); } psl->Release(); return FALSE; }
//Taken from: http://www.cplusplus.com/forum/windows/64088/ bool ResolveShortcut(HWND hwnd, const wchar_t* szShortcutPath, char* szResolvedPath, size_t nSize) { if(szResolvedPath == NULL) return SUCCEEDED(E_INVALIDARG); //Initialize COM stuff CoInitialize(NULL); //Get a pointer to the IShellLink interface. IShellLink* psl = NULL; HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); if(SUCCEEDED(hres)) { //Get a pointer to the IPersistFile interface. IPersistFile* ppf = NULL; hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); if(SUCCEEDED(hres)) { //Load the shortcut. hres = ppf->Load(szShortcutPath, STGM_READ); if(SUCCEEDED(hres)) { //Resolve the link. hres = psl->Resolve(hwnd, 0); if(SUCCEEDED(hres)) { //Get the path to the link target. char szGotPath[MAX_PATH] = {0}; hres = psl->GetPath(szGotPath, _countof(szGotPath), NULL, SLGP_SHORTPATH); if(SUCCEEDED(hres)) { strcpy_s(szResolvedPath, nSize, szGotPath); } } } //Release the pointer to the IPersistFile interface. ppf->Release(); } //Release the pointer to the IShellLink interface. psl->Release(); } //Uninitialize COM stuff CoUninitialize(); return SUCCEEDED(hres); }
HRESULT ResolveLink(HWND hwnd,DWORD fFlags,TCHAR *LinkFile,TCHAR *LinkPath,int nBufferSize) { IShellLink *pShellLink = NULL; IPersistFile *pPersistFile = NULL; SHFILEINFO shfi; WCHAR LinkFileW[MAX_PATH]; TCHAR ResolvedFilePath[MAX_PATH]; HRESULT hr; SHGetFileInfo(LinkFile,NULL,&shfi,sizeof(shfi),SHGFI_ATTRIBUTES); if(!(shfi.dwAttributes & SFGAO_LINK)) { StringCchCopy(LinkPath,nBufferSize,LinkFile); return E_UNEXPECTED; } hr = CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER, IID_IShellLink,(LPVOID*)&pShellLink); if(hr == S_OK) { hr = pShellLink->QueryInterface(IID_IPersistFile,(LPVOID *)&pPersistFile); if(hr == S_OK) { #ifndef UNICODE MultiByteToWideChar(CP_ACP,0,LinkFile,-1,LinkFileW,MAX_PATH); #else StringCchCopy(LinkFileW,SIZEOF_ARRAY(LinkFileW),LinkFile); #endif hr = pPersistFile->Load(LinkFileW,STGM_READ); if(hr == S_OK) { pShellLink->Resolve(hwnd,fFlags); pShellLink->GetPath(ResolvedFilePath,MAX_PATH,NULL,SLGP_UNCPRIORITY); StringCchCopy(LinkPath,nBufferSize,ResolvedFilePath); } pPersistFile->Release(); } pShellLink->Release(); } return hr; }
//获得快捷方式文件所指向的路径 //lpwsLinkName:lnk文件的路径 //lpwsLinkPath:用于存放所指程序路径的缓冲区 //返回:HRESULT HRESULT common_getLnkPath(IN LPWSTR lpwsLinkName,OUT LPWSTR lpwsLinkPath) { HRESULT hResult; IShellLink *pIShellLink; WIN32_FIND_DATA wfd; //初始化 CoInitialize(NULL); //创建实例 hResult = CoCreateInstance((REFIID)CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,(REFIID)IID_IShellLink,(LPVOID *)&pIShellLink); //如果成功 if (SUCCEEDED(hResult)) { IPersistFile *pIPersistFile; //查询相关信息 hResult = pIShellLink->QueryInterface((REFIID)IID_IPersistFile,(LPVOID *)&pIPersistFile); //如果查询成功 if (SUCCEEDED(hResult)) { //加载快捷方式文件 hResult = pIPersistFile->Load(lpwsLinkName, STGM_READ); //如果成功 if (SUCCEEDED(hResult)) { //解析 hResult = pIShellLink->Resolve(NULL,SLR_ANY_MATCH | SLR_NO_UI); if (SUCCEEDED(hResult)) { //获得快捷方式的制定的路径 hResult = pIShellLink->GetPath(lpwsLinkPath,MAX_PATH,&wfd,SLGP_SHORTPATH); } } pIPersistFile->Release(); } pIShellLink->Release(); } return hResult; }
// @pymethod |PyIShellLink|Resolve|Resolves a shell link by searching for the shell link object and updating the // shell link path and its list of identifiers (if necessary) PyObject *PyIShellLink::Resolve(PyObject *self, PyObject *args) { IShellLink *pISL = GetI(self); if ( pISL == NULL ) return NULL; // @pyparm HWND|hwnd||The parent window of a dialog which will pop up if resolution fails. // @pyparm int|fFlags||One of the following constants: // @flagh Value|Description // @flag SLR_INVOKE_MSI|Call the Microsoft Windows Installer. // @flag SLR_NOLINKINFO |Disable distributed link tracking. By default, distributed // link tracking tracks removable media across multiple devices based on the // volume name. It also uses the UNC path to track remote file systems whose // drive letter has changed. Setting SLR_NOLINKINFO disables both types of tracking. // @flag SLR_NO_UI|Do not display a dialog box if the link cannot be resolved. When // SLR_NO_UI is set, the high-order word of fFlags can be set to a time-out value // that specifies the maximum amount of time to be spent resolving the link. The // function returns if the link cannot be resolved within the time-out duration. // If the high-order word is set to zero, the time-out duration will be set to the // default value of 3,000 milliseconds (3 seconds). To specify a value, set the high // word of fFlags to the desired time-out duration, in milliseconds. // @flag SLR_NOUPDATE|Do not update the link information. // @flag SLR_NOSEARCH|Do not execute the search heuristics. // @flag SLR_NOTRACK|Do not use distributed link tracking. // @flag SLR_UPDATE|If the link object has changed, update its path and list of identifiers. If SLR_UPDATE is set, you do not need to call IPersistFile::IsDirty to determine whether or not the link object has changed. HWND hwnd; PyObject *obhwnd; DWORD fFlags; if ( !PyArg_ParseTuple(args, "Ol:Resolve", &obhwnd, &fFlags) ) return NULL; if (!PyWinObject_AsHANDLE(obhwnd, (HANDLE *)&hwnd)) return NULL; HRESULT hr; PY_INTERFACE_PRECALL; hr = pISL->Resolve( hwnd, fFlags ); PY_INTERFACE_POSTCALL; if ( FAILED(hr) ) return OleSetOleError(hr); Py_INCREF(Py_None); return Py_None; }
BOOL ResolveShortcut(TCHAR *shortcut, TCHAR *file) { CoInitialize(NULL); IShellLink* psl = NULL; HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) &psl); if (SUCCEEDED(hr)) { IPersistFile* ppf = NULL; hr = psl->QueryInterface(IID_IPersistFile, (void **) &ppf); if (SUCCEEDED(hr)) { #ifdef _UNICODE hr = ppf->Load(shortcut, STGM_READ); #else WCHAR tmp[MAX_PATH]; MultiByteToWideChar(CP_ACP, 0, shortcut, -1, tmp, MAX_PATH); hr = ppf->Load(tmp, STGM_READ); #endif if (SUCCEEDED(hr)) { hr = psl->Resolve(NULL, SLR_UPDATE); if (SUCCEEDED(hr)) { WIN32_FIND_DATA wfd; hr = psl->GetPath(file, MAX_PATH, &wfd, SLGP_RAWPATH); } } ppf->Release(); } psl->Release(); } if(FAILED(hr)) ErrorExit(NULL,_T("CreateShortcut")); return SUCCEEDED(hr); }
//----------------------------------------------------------------------------- bool WinDragContainer::checkResolveLink (const TCHAR* nativePath, TCHAR* resolved) { const TCHAR* ext = VSTGUI_STRRCHR (nativePath, '.'); if (ext && VSTGUI_STRICMP (ext, TEXT(".lnk")) == 0) { IShellLink* psl; IPersistFile* ppf; WIN32_FIND_DATA wfd; HRESULT hres; // Get a pointer to the IShellLink interface. hres = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl); if (SUCCEEDED (hres)) { // Get a pointer to the IPersistFile interface. hres = psl->QueryInterface (IID_IPersistFile, (void**)&ppf); if (SUCCEEDED (hres)) { // Load the shell link. hres = ppf->Load (nativePath, STGM_READ); if (SUCCEEDED (hres)) { hres = psl->Resolve (0, MAKELONG (SLR_ANY_MATCH | SLR_NO_UI, 500)); if (SUCCEEDED (hres)) { // Get the path to the link target. hres = psl->GetPath (resolved, 2048, &wfd, SLGP_SHORTPATH); } } // Release pointer to IPersistFile interface. ppf->Release (); } // Release pointer to IShellLink interface. psl->Release (); } return SUCCEEDED(hres); } return false; }
STDAPI ResolveIt(HWND hwnd, LPCTSTR lpszLinkFile, LPTSTR lpszPath) { HRESULT hres; IShellLink *psl; WIN32_FIND_DATA fd; *lpszPath = _T('\0'); // assume failure // Get a pointer to the IShellLink interface. hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; // Get a pointer to the IPersistFile interface. hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); if (SUCCEEDED(hres)) { // Load the shortcut. hres = ppf->Load(static_cast<T2W>(lpszLinkFile), STGM_READ); if (SUCCEEDED(hres)) { // Resolve the link. hres = psl->Resolve(hwnd, SLR_ANY_MATCH); if (SUCCEEDED(hres)) { // Get the path to the link target. hres = psl->GetPath(lpszPath, MAX_PATH, &fd, SLGP_SHORTPATH); } } // Release the pointer to the IPersistFile interface. ppf->Release(); } // Release the pointer to the IShellLink interface. psl->Release(); } return hres; }
//---------------------------------------------------------------------------------------- nsresult nsFileSpec::ResolveSymlink(PRBool& wasSymlink) //---------------------------------------------------------------------------------------- { wasSymlink = PR_FALSE; // assume failure if (Exists()) return NS_OK; HRESULT hres; IShellLink* psl; CoInitialize(NULL); // Get a pointer to the IShellLink interface. hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; // Get a pointer to the IPersistFile interface. hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); if (SUCCEEDED(hres)) { WCHAR wsz[MAX_PATH]; // Ensure that the string is Unicode. MultiByteToWideChar(CP_ACP, 0, mPath, -1, wsz, MAX_PATH); // Load the shortcut. hres = ppf->Load(wsz, STGM_READ); if (SUCCEEDED(hres)) { wasSymlink = PR_TRUE; // Resolve the link. hres = psl->Resolve(nsnull, SLR_NO_UI ); if (SUCCEEDED(hres)) { char szGotPath[MAX_PATH]; WIN32_FIND_DATA wfd; // Get the path to the link target. hres = psl->GetPath( szGotPath, MAX_PATH, &wfd, SLGP_UNCPRIORITY ); if (SUCCEEDED(hres)) { // Here we modify the nsFileSpec; mPath = szGotPath; mError = NS_OK; } } } else { // It wasn't a shortcut. Oh well. Leave it like it was. hres = 0; } // Release the pointer to the IPersistFile interface. ppf->Release(); } // Release the pointer to the IShellLink interface. psl->Release(); } CoUninitialize(); if (SUCCEEDED(hres)) return NS_OK; return NS_FILE_FAILURE; }
// Resolve a shortcut // Source: https://msdn.microsoft.com/en-us/library/windows/desktop/bb776891%28v=vs.85%29.aspx // // ------> DO NOT FORGET TO CALL CoInitialize first // HRESULT ResolveIt(HWND hwnd, LPCSTR lpszLinkFile, LPWSTR lpszPath, int iPathBufferSize) { IShellLink* psl; HRESULT hres; WCHAR szGotPath[MAX_PATH]; WCHAR szDescription[MAX_PATH]; WIN32_FIND_DATA wfd; *lpszPath = 0; // Assume failure hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); if(SUCCEEDED(hres)) { IPersistFile* ppf; // Get a pointer to the IPersistFile interface. hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); if (SUCCEEDED(hres)) { WCHAR wsz[MAX_PATH]; // Ensure that the string is Unicode. MultiByteToWideChar(CP_ACP, 0, lpszLinkFile, -1, wsz, MAX_PATH); // Add code here to check return value from MultiByteWideChar // for success. // Load the shortcut. hres = ppf->Load(wsz, STGM_READ); if (SUCCEEDED(hres)) { // Resolve the link. hres = psl->Resolve(hwnd, SLR_NO_UI); if (SUCCEEDED(hres)) { // Get the path to the link target. hres = psl->GetPath((LPSTR)szGotPath, MAX_PATH, (WIN32_FIND_DATA*)&wfd, SLGP_SHORTPATH); if (SUCCEEDED(hres)) { // Get the description of the target. hres = psl->GetDescription((LPSTR)szDescription, MAX_PATH); if (SUCCEEDED(hres)) { hres = StringCbCopy((STRSAFE_LPSTR)lpszPath, iPathBufferSize, (STRSAFE_LPSTR)szGotPath); if (SUCCEEDED(hres)) { // Handle success } else { // Handle the error } } } } } // Release the pointer to the IPersistFile interface. ppf->Release(); } // Release the pointer to the IShellLink interface. psl->Release(); } return hres; }
BOOL CInstall::CreateShellLink(LPCSTR description, LPCSTR program, LPCSTR arguments, LPCSTR icon, int nIconIndex) { HRESULT hres; IShellLink* psl; CHAR szLink[MAXSTR]; strcpy(szLink, m_szPrograms); strcat(szLink, "\\"); strcat(szLink, m_szTargetGroup); strcat(szLink, "\\"); strcat(szLink, description); strcat(szLink, ".LNK"); AddMessage("Adding shell link\n "); AddMessage(szLink); AddMessage("\n"); // Ensure string is UNICODE. WCHAR wsz[MAX_PATH]; MultiByteToWideChar(CP_ACP, 0, szLink, -1, wsz, MAX_PATH); // Save old shell link // Get a pointer to the IShellLink interface. hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; // Query IShellLink for the IPersistFile interface. hres = psl->QueryInterface(IID_IPersistFile, (void **)&ppf); if (SUCCEEDED(hres)) { // Load the shell link. hres = ppf->Load(wsz, STGM_READ); if (SUCCEEDED(hres)) { // Resolve the link. hres = psl->Resolve(HWND_DESKTOP, SLR_ANY_MATCH); if (SUCCEEDED(hres)) { // found it, so save details CHAR szTemp[MAXSTR]; WIN32_FIND_DATA wfd; int i; fprintf(m_fLogOld, "Name=%s\n", szLink); hres = psl->GetPath(szTemp, MAXSTR, (WIN32_FIND_DATA *)&wfd, SLGP_SHORTPATH ); if (SUCCEEDED(hres)) fprintf(m_fLogOld, "Path=%s\n", szTemp); hres = psl->GetDescription(szTemp, MAXSTR); if (SUCCEEDED(hres)) fprintf(m_fLogOld, "Description=%s\n", szTemp); hres = psl->GetArguments(szTemp, MAXSTR); if (SUCCEEDED(hres) && (szTemp[0] != '\0')) fprintf(m_fLogOld, "Arguments=%s\n", szTemp); hres = psl->GetWorkingDirectory(szTemp, MAXSTR); if (SUCCEEDED(hres) && (szTemp[0] != '\0')) fprintf(m_fLogOld, "Directory=%s\n", szTemp); hres = psl->GetIconLocation(szTemp, MAXSTR, &i); if (SUCCEEDED(hres) && (szTemp[0] != '\0')) { fprintf(m_fLogOld, "IconLocation=%s\n", szTemp); fprintf(m_fLogOld, "IconIndex=%d\n", i); } fprintf(m_fLogOld, "\n"); } } // Release pointer to IPersistFile. ppf->Release(); } // Release pointer to IShellLink. psl->Release(); } // Save new shell link // Get a pointer to the IShellLink interface. hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; // Query IShellLink for the IPersistFile interface for // saving the shell link in persistent storage. hres = psl->QueryInterface(IID_IPersistFile, (void **)&ppf); if (SUCCEEDED(hres)) { fprintf(m_fLogNew, "Name=%s\n", szLink); // Set the path to the shell link target. hres = psl->SetPath(program); if (!SUCCEEDED(hres)) AddMessage("SetPath failed!"); fprintf(m_fLogNew, "Path=%s\n", program); // Set the description of the shell link. hres = psl->SetDescription(description); if (!SUCCEEDED(hres)) AddMessage("SetDescription failed!"); fprintf(m_fLogNew, "Description=%s\n", description); if (arguments != (LPCSTR)NULL) { // Set the arguments of the shell link target. hres = psl->SetArguments(arguments); if (!SUCCEEDED(hres)) AddMessage("SetArguments failed!"); fprintf(m_fLogNew, "Arguments=%s\n", arguments); } if (icon != (LPCSTR)NULL) { // Set the arguments of the shell link target. hres = psl->SetIconLocation(icon, nIconIndex); if (!SUCCEEDED(hres)) AddMessage("SetIconLocation failed!"); fprintf(m_fLogNew, "IconLocation=%s\n", icon); fprintf(m_fLogNew, "IconIndex=%d\n", nIconIndex); } // Save the link via the IPersistFile::Save method. hres = ppf->Save(wsz, TRUE); // Release pointer to IPersistFile. ppf->Release(); } // Release pointer to IShellLink. psl->Release(); fprintf(m_fLogNew, "\n"); } return (hres == 0); }
bool FileSystemManager::getShortcutTarget(QString shortcutFileName, QString * targetOut, QString * argsOut, QString * workingDirOut) { assert(targetOut); // return true if _any_ of the attributes can be resolved bool targetResolved = false; bool result = false; IShellLink * psl = NULL; // Accounting for the newer "Advertised Shortcuts" which refer to MSI operations which may pre-empt the // launching of the specified shortcut DWORD targetPathLen = MAX_PATH; TCHAR targetPath[MAX_PATH]; TCHAR productCode[MAX_PATH]; TCHAR featureId[MAX_PATH]; TCHAR componentCode[MAX_PATH]; if (pfnMsiGetShortcutTarget && pfnMsiGetComponentPath) { if (ERROR_SUCCESS == pfnMsiGetShortcutTarget((LPCTSTR) shortcutFileName.utf16(), productCode, featureId, componentCode)) { if (INSTALLSTATE_LOCAL == pfnMsiGetComponentPath(productCode, componentCode, targetPath, &targetPathLen)) { *targetOut = QString::fromUtf16((const ushort *) targetPath); targetResolved = true; result = true; } } } // Get a pointer to the IShellLink interface. TCHAR args[MAX_PATH]; TCHAR workingDir[MAX_PATH]; HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl); if (SUCCEEDED(hres)) { IPersistFile * ppf = NULL; // Get a pointer to the IPersistFile interface. if (SUCCEEDED(psl->QueryInterface(IID_IPersistFile, (LPVOID *) &ppf))) { // Load the shortcut. if (SUCCEEDED(ppf->Load((LPCOLESTR) shortcutFileName.utf16(), STGM_READ))) { // Resolve the link. if (SUCCEEDED(psl->Resolve(winOS->GetWindowsHandle(), SLR_NOUPDATE | SLR_NO_UI))) { // Get the path to the link target. if (!targetResolved && SUCCEEDED(psl->GetPath(targetPath, MAX_PATH, NULL, 0))) { *targetOut = QString::fromUtf16((const ushort *) targetPath); result = true; } if (argsOut && SUCCEEDED(psl->GetArguments(args, MAX_PATH))) { *argsOut = QString::fromUtf16((const ushort *) args); result = true; } if (workingDirOut && SUCCEEDED(psl->GetWorkingDirectory(workingDir, MAX_PATH))) { *workingDirOut = QString::fromUtf16((const ushort *) workingDir); result = true; } } } // Release the pointer to the IPersistFile interface. ppf->Release(); } // Release the pointer to the IShellLink interface. psl->Release(); } return result; }
/* * Class: sun_awt_shell_Win32ShellFolder * Method: getLinkLocation * Signature: (JJ)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_sun_awt_shell_Win32ShellFolder_getLinkLocation (JNIEnv* env, jobject folder, jlong parentIShellFolder, jlong relativePIDL) { HRESULT hres; IShellLink* psl; char szGotPath[MAX_PATH]; WIN32_FIND_DATA wfd; CHAR szBuf[MAX_PATH]; STRRET strret; OLECHAR olePath[MAX_PATH]; // wide-char version of path name LPWSTR wstr; IShellFolder* pParent = (IShellFolder*)parentIShellFolder; if (pParent == NULL) { return NULL; } LPITEMIDLIST pidl = (LPITEMIDLIST)relativePIDL; if (pidl == NULL) { return NULL; } pParent->GetDisplayNameOf(pidl, SHGDN_NORMAL | SHGDN_FORPARSING, &strret); switch (strret.uType) { case STRRET_CSTR : // IShellFolder::ParseDisplayName requires the path name in Unicode. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, strret.cStr, -1, olePath, MAX_PATH); wstr = olePath; break; case STRRET_OFFSET : MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (CHAR *)(pidl + strret.uOffset), -1, olePath, MAX_PATH); wstr = olePath; break; case STRRET_WSTR : wstr = strret.pOleStr; break; } CoInitialize(NULL); hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); if (SUCCEEDED(hres)) { hres = ppf->Load(wstr, STGM_READ); if (SUCCEEDED(hres)) { //hres = psl->Resolve(NULL, SLR_NO_UI); hres = psl->Resolve(NULL, 0); if (SUCCEEDED(hres)) { hres = psl->GetPath(szGotPath, MAX_PATH, (WIN32_FIND_DATA *)&wfd, SLGP_SHORTPATH); if (SUCCEEDED(hres)) { lstrcpy(szBuf, szGotPath); } } } ppf->Release(); } psl->Release(); } CoUninitialize(); if (SUCCEEDED(hres)) { return JNU_NewStringPlatform(env, szBuf); } else { return NULL; } }
HRESULT ShellFunctions::ResolveShortcut(HWND hWnd,LPCWSTR pszShortcutFile,LPWSTR pszPath) { HRESULT hres; pszPath[0]='\0'; if (IsUnicodeSystem()) { IShellLinkW* psl; WIN32_FIND_DATAW wfd; hres=CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLinkW,(void**)&psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; hres=psl->QueryInterface(IID_IPersistFile,(void**)&ppf); if (SUCCEEDED(hres)) { hres=ppf->Load(pszShortcutFile,STGM_READ); if (SUCCEEDED(hres)) { hres=psl->Resolve(hWnd,SLR_ANY_MATCH); if (pszPath!=NULL && SUCCEEDED(hres)) hres=psl->GetPath(pszPath,MAX_PATH,(WIN32_FIND_DATAW*)&wfd,0); } ppf->Release(); } psl->Release(); } } else { IShellLink* psl; WIN32_FIND_DATA wfd; hres=CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,IID_IShellLink,(void**)&psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; hres=psl->QueryInterface(IID_IPersistFile,(void**)&ppf); if (SUCCEEDED(hres)) { hres=ppf->Load(pszShortcutFile,STGM_READ); if (SUCCEEDED(hres)) { hres=psl->Resolve(hWnd,SLR_ANY_MATCH); if (pszPath!=NULL && SUCCEEDED(hres)) { char szPathA[MAX_PATH]; hres=psl->GetPath(szPathA,MAX_PATH,(WIN32_FIND_DATA*)&wfd,0); if (SUCCEEDED(hres)) MultiByteToWideChar(CP_ACP,0,szPathA,-1,pszPath,MAX_PATH); } } ppf->Release(); } psl->Release(); } } return hres; }
bool nglPath::ResolveLink() { if (!GetOSPathName()) return false; char tmpPath[MAX_PATH]; if (access(GetOSPathName(), 0) == -1) { strcpy(tmpPath, GetOSPathName()); strcat(tmpPath,".lnk"); } else { if (GetExtension() != "lnk") { return false; } strcpy(tmpPath, GetOSPathName()); } HRESULT hres; IShellLink *pShLink = NULL; wchar_t wsz[MAX_PATH]; char resolvedPath[MAX_PATH]; CoInitialize(NULL); bool res = false; hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *)&pShLink); if (SUCCEEDED(hres)) { IPersistFile *ppf; // The IShellLink Interface supports the IPersistFile // interface. Get an interface pointer to it. hres = pShLink->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf); if (SUCCEEDED(hres)) { // Convert the given link name string to a wide character string. MultiByteToWideChar(CP_ACP, 0, tmpPath, -1, wsz, MAX_PATH); // Load the file. hres = ppf->Load(wsz, STGM_READ); if (SUCCEEDED(hres)) { // Resolve the link by calling the Resolve() interface function. // This enables us to find the file the link points to even if // it has been moved or renamed. hres = pShLink->Resolve(NULL, SLR_ANY_MATCH | SLR_NO_UI); if (SUCCEEDED(hres)) { WIN32_FIND_DATA winFindData; // Get the path of the file the link points to. hres = pShLink->GetPath(resolvedPath, MAX_PATH, &winFindData, SLGP_UNCPRIORITY); if (SUCCEEDED(hres)) { InternalSetPath(resolvedPath); res = true; } } } ppf->Release(); } pShLink->Release(); } return res; }
/********************************************* This routine resolves the lnk destination: \param LnkName - The name of the ShortCut\n \param SpecialFolder - where to put the shortcut (See #defines above (MSDN)) \param hwnd - handle of the parent window for MessageBoxes the shell may need to display \param LnkPath - Reference to a CString that receives the linkpath if routine is successful else the string will be empty \param LnkDescription - Reference to a CString that receives the Description of the link else the string will be empty \returns a HRESULT *********************************************/ HRESULT CShortcut::ResolveLink(const CString &LnkName, UINT SpecialFolder, HWND hwnd, CString &LnkPath, CString &LnkDescription) { HRESULT hres; IShellLink* psl; wchar_t *szGotPath = LnkPath.GetBuffer(MAX_PATH); wchar_t *szDescription = LnkDescription.GetBuffer(MAX_PATH); CString sLnkFile, sSpecialFolder; CString sLong; WIN32_FIND_DATA wfd; // get the path to the special folder: if (!GetSpecialFolder(SpecialFolder, sSpecialFolder)) return 1; // return ERROR // build a linkfile: sLnkFile = sSpecialFolder + LnkName + L".lnk"; // Get a pointer to the IShellLink interface. hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*) &psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; // Get a pointer to the IPersistFile interface. hres = psl->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf); if (SUCCEEDED(hres)) { hres = ppf->Load(sLnkFile, STGM_READ); if (SUCCEEDED(hres)) { // Resolve the link. hres = psl->Resolve(hwnd, SLR_ANY_MATCH); if (SUCCEEDED(hres)) { // Get the path to the link target. hres = psl->GetPath(szGotPath, MAX_PATH, (WIN32_FIND_DATA *)&wfd, SLGP_SHORTPATH); LnkPath.ReleaseBuffer(); if (!SUCCEEDED(hres)) { LnkDescription.ReleaseBuffer(); return hres; // application-defined function } // Get the description of the target: hres = psl->GetDescription(szDescription, MAX_PATH); LnkDescription.ReleaseBuffer(); if (!SUCCEEDED(hres)) return hres; } } // Release the pointer to the IPersistFile interface. ppf->Release(); } // Release the pointer to the IShellLink interface. psl->Release(); } // whether OS is <= NT4 or not... use this helper: if (ShortToLongPathName(LnkPath, sLong) > LnkPath.GetLength()) LnkPath = sLong; return hres; }
/*! ショートカット(.lnk)の解決 @date 2009.01.08 ryoji CoInitialize/CoUninitializeを削除(WinMainにOleInitialize/OleUninitializeを追加) */ BOOL ResolveShortcutLink( HWND hwnd, LPCTSTR lpszLinkFile, LPTSTR lpszPath ) { BOOL bRes; HRESULT hRes; IShellLink* pIShellLink; IPersistFile* pIPersistFile; WIN32_FIND_DATA wfd; /* 初期化 */ pIShellLink = NULL; pIPersistFile = NULL; *lpszPath = 0; // assume failure bRes = FALSE; // 2009.01.08 ryoji CoInitializeを削除(WinMainにOleInitialize追加) // Get a pointer to the IShellLink interface. // hRes = 0; TCHAR szAbsLongPath[_MAX_PATH]; if( ! ::GetLongFileName( lpszLinkFile, szAbsLongPath ) ){ return FALSE; } // 2010.08.28 DLL インジェクション対策としてEXEのフォルダに移動する CCurrentDirectoryBackupPoint dirBack; ChangeCurrentDirectoryToExeDir(); if( SUCCEEDED( hRes = ::CoCreateInstance( CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *)&pIShellLink ) ) ){ // Get a pointer to the IPersistFile interface. if( SUCCEEDED(hRes = pIShellLink->QueryInterface( IID_IPersistFile, (void**)&pIPersistFile ) ) ){ // Ensure that the string is Unicode. WCHAR wsz[MAX_PATH]; _tcstowcs(wsz, szAbsLongPath, _countof(wsz)); // MultiByteToWideChar( CP_ACP, 0, lpszLinkFile, -1, wsz, MAX_PATH ); // Load the shortcut. if( SUCCEEDED(hRes = pIPersistFile->Load( wsz, STGM_READ ) ) ){ // Resolve the link. if( SUCCEEDED( hRes = pIShellLink->Resolve(hwnd, SLR_ANY_MATCH ) ) ){ // Get the path to the link target. TCHAR szGotPath[MAX_PATH]; szGotPath[0] = _T('\0'); if( SUCCEEDED( hRes = pIShellLink->GetPath(szGotPath, MAX_PATH, &wfd, SLGP_SHORTPATH ) ) ){ // Get the description of the target. TCHAR szDescription[MAX_PATH]; if( SUCCEEDED(hRes = pIShellLink->GetDescription(szDescription, MAX_PATH ) ) ){ if( _T('\0') != szGotPath[0] ){ /* 正常終了 */ _tcscpy_s( lpszPath, _MAX_PATH, szGotPath ); bRes = TRUE; } } } } } } } // Release the pointer to the IPersistFile interface. if( NULL != pIPersistFile ){ pIPersistFile->Release(); pIPersistFile = NULL; } // Release the pointer to the IShellLink interface. if( NULL != pIShellLink ){ pIShellLink->Release(); pIShellLink = NULL; } // 2009.01.08 ryoji CoUninitializeを削除(WinMainにOleUninitialize追加) return bRes; }
BOOL OpenLinkedFile(CFile &fileObject, CString fileName, CString &linkedFilePath, HWND hWnd) { CFileException fileError; CFileStatus fileStatus; // Attempt to open the file as named if (!fileObject.Open(fileName, CFile::modeRead, &fileError)) { TRACE("File could not be opened: %s. Check for shortcut.\n", fileName); // The file, as named, couldn't be opened, so check for a shortcut with the same name fileName += ".lnk"; // Shortcut filename = filename.lnk if (CFile::GetStatus(fileName, fileStatus)) { TRACE("Shortcut detected: %s\n", fileName); // A shortcut for the named file has been found. // The shortcut link must now be resolved to get the target file name. HRESULT hres; IShellLink *pShellLink; WIN32_FIND_DATA wfd; TCHAR targetFilePath[MAX_PATH]; // Call CoCreateInstance to obtain the IShellLink // Interface pointer. This call fails if // CoInitialize is not called, so it is assumed that // CoInitialize has already been called. hres = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *)&pShellLink); if (SUCCEEDED(hres)) { // A pointer to the IShellLink interface has been obtained. // Now, we need a pointer to the IPersistFile interface. IPersistFile *pPersistFile; hres = pShellLink->QueryInterface(IID_IPersistFile, (LPVOID *)&pPersistFile); if (SUCCEEDED(hres)) { // Pointers to both interfaces have been obtained. // Convert the shortcut filename to a wide character string. WCHAR wideFileName [MAX_PATH]; //MultiByteToWideChar (CP_ACP, 0, (LPCSTR)fileName, -1, wideFileName, MAX_PATH); _tcscpy(wideFileName, (LPCTSTR)fileName); // Load the shortcut file from disk. hres = pPersistFile->Load(wideFileName, STGM_READ); if (SUCCEEDED (hres)) { // The shortcut file has been loaded. Resolve the shortcut link. hres = pShellLink->Resolve(hWnd, SLR_NO_UI); if (SUCCEEDED(hres)) { // The shortcut link has been successfully resolved. // Get the path of the target file that the link points to. hres = pShellLink->GetPath(targetFilePath, MAX_PATH, &wfd, SLGP_SHORTPATH); if (SUCCEEDED(hres)) { // The path of the target file has been obtained. // Attempt to open the target (linked) file. linkedFilePath = targetFilePath; if (fileObject.Open(linkedFilePath, CFile::modeRead, &fileError)) { // The named file was a shortcut. The shortcut link was successfully // resolved and the target file was successfully opened. TRACE("Shortcut detected, resolved and opened: %s->%s\n", fileName, linkedFilePath); return TRUE; } } } } // Release the IPersistFile interface pointer. pPersistFile->Release(); } // Release the IShellLink interface pointer. pShellLink->Release(); } } else { // Named file could not be opened and no shortcut file was detected. TRACE("File could not be opened: % s. No shortcut detected.\n", fileName); return FALSE; } } else { // The named file was not a shortcut and was successfully opened. return TRUE; } // The named file was a shortcut, but the shortcut was not successfully resolved or opened. TRACE("Shortcut detected, but was not successfully opened: %s->%s\n", fileName, linkedFilePath); return FALSE; }
// http://msdn.microsoft.com/en-us/library/aa969393.aspx bool my_resolveshortcut(TCHAR *linkfile, int size) { bool ok = false; HRESULT hres; IShellLink* psl; WCHAR szGotPath[MAX_PATH]; WCHAR szDescription[MAX_PATH]; WIN32_FIND_DATA wfd; const TCHAR *ext = _tcsrchr (linkfile, '.'); if (!ext || _tcsicmp (ext, _T(".lnk")) != 0) return false; // Get a pointer to the IShellLink interface. It is assumed that CoInitialize // has already been called. hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; // Get a pointer to the IPersistFile interface. hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); if (SUCCEEDED(hres)) { // Add code here to check return value from MultiByteWideChar // for success. // Load the shortcut. hres = ppf->Load(linkfile, STGM_READ); if (SUCCEEDED(hres)) { // Resolve the link. hres = psl->Resolve(NULL, SLR_NO_UI); if (SUCCEEDED(hres)) { // Get the path to the link target. hres = psl->GetPath(szGotPath, MAX_PATH, (WIN32_FIND_DATA*)&wfd, SLGP_SHORTPATH); if (SUCCEEDED(hres)) { // Get the description of the target. hres = psl->GetDescription(szDescription, MAX_PATH); if (SUCCEEDED(hres)) { if (size > 0) my_canonicalize_path (szGotPath, linkfile, size); ok = SUCCEEDED(hres); } } } } // Release the pointer to the IPersistFile interface. ppf->Release(); } // Release the pointer to the IShellLink interface. psl->Release(); } return ok; }