Get icon for .url files on Windows



  • I noticed that SHGetFileInfoW function does not return icons for .url files on Windows... My app successfully obtains icons for regular .lnk shortcuts, but not for .url files. For example, shortcuts for Steam games have .url extension.

    For .url files I get blank-sheet icon. Ideas plz?
    Here is the code I use:

    HICON FileListData::loadHIcon(QString filePath)
    {
    	HICON result = 0;
    	auto filePathWideChars = new wchar_t[filePath.length() + 1];
    	filePath = filePath.replace('/', '\\');
    	filePath.toWCharArray(filePathWideChars);
    	filePathWideChars[filePath.length()] = 0;
    	auto fileInfo = new SHFILEINFO();
    	if (SHGetFileInfoW(filePathWideChars, FILE_ATTRIBUTE_NORMAL, fileInfo, sizeof(SHFILEINFO), SHGFI_ICON | SHGFI_LARGEICON))
    	{
    		result = fileInfo->hIcon;
    	}
    	delete[] filePathWideChars;
    	delete fileInfo;
    	return result;
    }
    
    void FileListData::burnIcon(int index)
    {
    	auto hicon = hicons[index];
    	QIcon icon = QIcon(QtWin::fromHICON(hicon));
    	icons.append(icon);
    }
    
    void FileListData::loadHIcons()
    {
    	hicons = new HICON[files.count()];
    	for (int i = 0; i < files.count(); i++)
    	{
    		auto fileInfo = files[i];
    //		if (fileInfo.isSymLink())
    //			fileInfo = QFileInfo(fileInfo.symLinkTarget());
    		const auto icon = loadHIcon(fileInfo.absoluteFilePath());
    		hicons[i] = icon;
    		if (iconLoadedEventReceiver != nullptr)
    			iconLoadedEventReceiver(i + 1);
    	}
    }
    
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.