bool ShellIO::GetLinkInfo(CString fileName, LinkInfo *linkInfo, int flags) { IShellLink *psl; IPersistFile *ppf; HRESULT hRes; bool ret = false; hRes = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl); if(SUCCEEDED(hRes)) { hRes = psl->QueryInterface(IID_IPersistFile, (void**)&ppf); if(SUCCEEDED(hRes)) { hRes = ppf->Load(fileName.GetBuffer(), NULL); if(SUCCEEDED(hRes)) { wchar_t buf[1000]; if((flags & LI_DESCRIPTION) == LI_DESCRIPTION) { memset(buf, 0, sizeof(buf)); hRes = psl->GetDescription(buf, 1000); if(SUCCEEDED(hRes) && (wcslen(buf) > 0)) { linkInfo->description = buf; ret = true; } else { linkInfo->description.Empty(); } } if((flags & LI_PATH) == LI_PATH) { LPITEMIDLIST pidl; hRes = psl->GetIDList(&pidl); if(SUCCEEDED(hRes) && pidl) { linkInfo->path = PIDLToString(pidl, NULL, SHGDN_FORPARSING); ILFree(pidl); ret = !linkInfo->path.IsEmpty(); } else { linkInfo->path.Empty(); } } if((flags & LI_ARGUMENTS) == LI_ARGUMENTS) { memset(buf, 0, sizeof(buf)); hRes = psl->GetArguments(buf, 1000); if(SUCCEEDED(hRes) && (wcslen(buf) > 0)) { linkInfo->arguments = buf; ret = true; } else { linkInfo->arguments.Empty(); } } if((flags & LI_WORKDIRECTORY) == LI_WORKDIRECTORY) { memset(buf, 0, sizeof(buf)); hRes = psl->GetWorkingDirectory(buf, 1000); if(SUCCEEDED(hRes) && (wcslen(buf) > 0)) { linkInfo->workDirectory = buf; ret = true; } else { linkInfo->workDirectory.Empty(); } } if((flags & LI_ICONLOCATION) == LI_ICONLOCATION) { int iIcon; memset(buf, 0, sizeof(buf)); hRes = psl->GetIconLocation(buf, 1000, &iIcon); if(SUCCEEDED(hRes) && (wcslen(buf) > 0)) { linkInfo->iconLocation = buf; linkInfo->iconIndex = iIcon; ret = true; } else { linkInfo->iconLocation.Empty(); linkInfo->iconIndex = 0; } } } ppf->Release(); } psl->Release(); } return ret; }
// @pymethod str|PyIShellLink|GetIconLocation|Retrieves the location (path and index) of the icon for a shell link object. PyObject *PyIShellLink::GetIconLocation(PyObject *self, PyObject *args) { IShellLink *pISL = GetI(self); if ( pISL == NULL ) return NULL; // @pyparm int|cchMaxPath|_MAX_PATH|Number of characters to allocate for the result string. int cchIconPath = _MAX_PATH; if ( !PyArg_ParseTuple(args, "|i:GetIconLocation", &cchIconPath) ) return NULL; TCHAR *pszIconPath = (TCHAR *)malloc(cchIconPath * sizeof(TCHAR) ); if (pszIconPath==NULL) { PyErr_SetString(PyExc_MemoryError, "allocating string buffer"); return NULL; } HRESULT hr; int iIcon; PY_INTERFACE_PRECALL; hr = pISL->GetIconLocation( pszIconPath, cchIconPath, &iIcon ); PY_INTERFACE_POSTCALL; PyObject *ret; if ( FAILED(hr) ) ret = OleSetOleError(hr); else ret = Py_BuildValue("Ni", PyWinObject_FromTCHAR(pszIconPath), iIcon); free(pszIconPath); return ret; }
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 GetShortcutInfo(LPCTSTR path, CString& targetPath, HICON *largeIcon, HICON *smallIcon, CString *comment) { // Get the attributes of the specified shortcut HRESULT hRes; IShellLink* psl; CString iconFileName; int iconInd; SHFILEINFO inf; 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)) { #ifndef UNICODE WCHAR wsz[BUFF_SIZE]; MultiByteToWideChar(CP_ACP, 0, path, -1, wsz, BUFF_SIZE); hRes = ppf->Load(wsz, STGM_READ); #else hRes = ppf->Load(path, STGM_READ); #endif } iconInd = 0; hRes = psl->GetPath(targetPath.GetBuffer(BUFF_SIZE), BUFF_SIZE, NULL, SLGP_RAWPATH); targetPath.ReleaseBuffer(); if (!targetPath.IsEmpty()) ExpEnvVars(targetPath); if (largeIcon && smallIcon) { hRes = psl->GetIconLocation(iconFileName.GetBuffer(BUFF_SIZE), BUFF_SIZE, &iconInd); iconFileName.ReleaseBuffer(); if (!iconFileName.IsEmpty()) ExpEnvVars(iconFileName); if (iconFileName.IsEmpty() && targetPath.IsEmpty()) { SHGetFileInfo(path, 0, &inf, sizeof(inf), SHGFI_ICONLOCATION); iconFileName = inf.szDisplayName; iconInd = inf.iIcon; } if (iconFileName.IsEmpty()) { GetFileIcons(targetPath, largeIcon, smallIcon); } else { if (iconInd == -1) iconInd = 0; hRes = ExtractIconEx(iconFileName, iconInd, largeIcon, smallIcon, 1); if (hRes == -1) GetFileIcons(path, largeIcon, smallIcon); } } if (comment) { *comment = GetFileNameComp(path, eFcName); CString descr; hRes = psl->GetDescription(descr.GetBuffer(BUFF_SIZE), BUFF_SIZE); descr.ReleaseBuffer(); if (!descr.IsEmpty()) { if (isdigit(comment->GetAt(0)) && isdigit(comment->GetAt(1))) *comment = EMPTY_CSTR; else *comment += CRLF; *comment += descr; } } ppf->Release(); psl->Release(); } return TRUE; }