Created
February 21, 2017 07:41
-
-
Save Winand/997ed38269e899eb561991a0c663fa49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import locale | |
import struct | |
def __readLink(path): | |
# http://stackoverflow.com/a/28952464/1119602 | |
with open(path, 'rb') as stream: | |
content = stream.read() | |
# skip first 20 bytes (HeaderSize and LinkCLSID) | |
# read the LinkFlags structure (4 bytes) | |
lflags = struct.unpack('I', content[0x14:0x18])[0] | |
position = 0x18 | |
# if the HasLinkTargetIDList bit is set then skip the stored IDList | |
# structure and header | |
if (lflags & 0x01) == 1: | |
position = struct.unpack('H', content[0x4C:0x4E])[0] + 0x4E | |
last_pos = position | |
position += 0x04 | |
# get how long the file information is (LinkInfoSize) | |
length = struct.unpack('I', content[last_pos:position])[0] | |
# skip 12 bytes (LinkInfoHeaderSize, LinkInfoFlags and VolumeIDOffset) | |
position += 0x0C | |
# go to the LocalBasePath position | |
lbpos = struct.unpack('I', content[position:position+0x04])[0] | |
position = last_pos + lbpos | |
# read the string at the given position of the determined length | |
size = (length + last_pos) - position - 0x02 | |
content = content[position:position+size].split(b'\x00', 1) | |
return content[-1].decode('utf-16' if len(content) > 1 | |
else locale.getdefaultlocale()[1]) |
@hannesdelbeke yes, it's very basic implementation which does not work with some .lnk files.
So I switched to ShellLink.GetPath COM method. You just create IShellLink() object and call get_path(path_to_link)
.
see also IShellLink docs
Thanks for the update!
if i'm not mistaken, the python implementation for that uses win32com, which is not in the standard python library.
Thanks for the update! if i'm not mistaken, the python implementation for that uses win32com, which is not in the standard python library.
No no, com_interfaces is a package i've made recently. It uses only ctypes
. I've added IShellLink
as an example. You can implement other interfaces in a similar way (using Microsoft docs, etc.)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
better performance for multiple shortcuts, running everything in 1 powershell cmd.
get all links, recursively in a folder
nearly instant instead of wait a few seconds when running on all shortcuts in windows start menu