Click here to Skip to main content
15,443,778 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In Windows, you can grab a link from the address bar of a browser (Chrome, Firefox, Edge) and drop it on the desktop or in another folder and Windows will create a .URL file for it. This file contains the link and a reference to the cached icon for the page. The name of the file is the title of the page.

I need to replicate this capability in my own program. I'm using C++ and MFC. I have code using COleDropTarget that gets the url itself. I use RegisterClipboardFormat(_TEXT("UniformResourceLocator")) to catch this.

I could write code to go out to the link and parse the file for the <title>...
to get the page title, but I don't know how to get the path to the cached icon associated with the page. I would prefer not to download and create a new icon since there is already one in the icon cache.

I am thinking that there is some way the browsers make all this information available to explorer so it can create the .URL file, but I can't find any documentation on how this might happen. If anyone knows, please tell me. Ideally, a snippet of code that I can use to capture this info (url, page title, path to cached icon file and icon index if any) would be great. If there's a way to send a message to a browser to tell it to create a .URL file, that would also work.

Thank you so much!

What I have tried:

I have tried a class derived from COleDropTarget implementing OnDragOver, OnDragEnter, OnDragLeave and OnDrop. I have registered clipboard formats "UniformResourceLocator" and "UniformResourceLocatorW". I also tried clipboard format CF_TEXT, which seems to work, but others I tried did not catch a dropped url.
Posted
Updated 6-Oct-21 13:35pm

You can only use what you have, so best try some Clipboard Viewer Software what you have. When it is only text, than you need to deal with it: check, verify and use it.
 
Share this answer
 
Comments
Member 14155986 5-Oct-21 3:39am    
I tried the viewer you suggest. It does not show anything when I drag a link from a browser. It does not show the contents of UniformResourceLocator, for example.
Perhaps this helps:

Any way to grab a logo icon from website URL, programmatically? - Stack Overflow[^]

For example, you can load the image in two ways:
1. If there's an icon at /favicon.ico, load that.
2. Extract the location from <link rel="shortcut icon" href="URL goes here" /> from the content of the page with a rel of either icon or shortcut icon.
 
Share this answer
 
v2
Comments
Member 14155986 7-Oct-21 5:05am     CRLF
Thank you for this. If I can't find out what I am looking for, I can use this method to get an icon for the web page and also the page title. This will still not give me the path to the already cached icon, and I don't think it it worth searching the cache to match the icon file. To be clear, I can load a web page in my browser, then disconnect the computer from the internet, then drag from the address bar onto the desktop, and Windows will create a .URL file there that looks like this: [InternetShortcut] URL=https://www.amazon.com/ IDList= HotKey=0 IconFile=C:\Users\User\AppData\Local\Mozilla\Firefox\Profiles\am00k2ao.default-release\shortcutCache\K00ILysCaEq8+bEqV_3nuw==.ico IconIndex=0 The name of the file is the title of the web page with ".URL" appended. This tells me that there is some communication between the browser and Windows that tells it not only the URL but the path to the cached icon file and the page title. I have only seen documentation on how to drop the URL onto my program, I would like to know how to drop these other two pieces of information on my program.
Member 14155986 7-Oct-21 5:16am    
Another issue: The browser may have cookies and/or login info. If I try to read some pages, I do not get the same page because I do not have those cookies. Is it feasible to exactly mimic Firefox, Edge and Chrome to parse their cookies and retrieve exactly the page the user is pointing to?
I figured out what to do by reading the Firefox source code. The solution is to support clipboard contents of type FileGroupDescriptor and FileContents. The first one gives the name of the file, which is the page title with .URL appended to it, and the second one gives an IStream from which you can read the .URL file itself. The contents of the file include the url and the reference to the cached icon. Exactly what I needed!

Does anyone know what would have been the easy place for me to find this documented?
 
Share this answer
 
Comments
Member 14155986 9-Oct-21 7:34am     CRLF
Ah, well this works for Firefox (not surprising) but I have a problem with Chrome and Edge. These two use the wide format, so I register clipboard format "FileGroupDescriptorW" which gives me the file descriptor, but I cannot seem to read the file. IsDataAvailable(CF_FILECONTENTS) returns true, but GetData fails for types TYMED_ISTORAGE, TYMED_ISTREAM, TYMED_HGLOBAL and TYMED_FILE. Also QueryGetData fails for each of these types. How do I retrieve the file contents from Chrome or edge?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900