Created
March 17, 2015 14:54
-
-
Save davecluderay/e44ca3cb56e581c5d469 to your computer and use it in GitHub Desktop.
Resolves shell links
This file contains hidden or 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
public static class ShellLink | |
{ | |
public static string Resolve(string shortcutPath) | |
{ | |
var link = new CShellLink(); | |
((IPersistFile)link).Load(shortcutPath, STGM_READ); | |
var result = new StringBuilder(MAX_PATH); | |
// ReSharper disable once RedundantAssignment | |
var findData = new WIN32_FIND_DATAW(); | |
((IShellLinkW)link).GetPath(result, result.Capacity, out findData, 0); | |
return result.ToString(); | |
} | |
// ReSharper disable InconsistentNaming | |
// ReSharper disable UnusedMember.Local | |
// ReSharper disable FieldCanBeMadeReadOnly.Local | |
// ReSharper disable MemberCanBePrivate.Local | |
[DllImport("shfolder.dll", CharSet = CharSet.Auto)] | |
internal static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath); | |
[Flags] | |
enum SLGP_FLAGS | |
{ | |
SLGP_SHORTPATH = 0x1, | |
SLGP_UNCPRIORITY = 0x2, | |
SLGP_RAWPATH = 0x4 | |
} | |
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] | |
struct WIN32_FIND_DATAW | |
{ | |
public uint dwFileAttributes; | |
public long ftCreationTime; | |
public long ftLastAccessTime; | |
public long ftLastWriteTime; | |
public uint nFileSizeHigh; | |
public uint nFileSizeLow; | |
public uint dwReserved0; | |
public uint dwReserved1; | |
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] | |
public string cFileName; | |
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)] | |
public string cAlternateFileName; | |
} | |
[Flags] | |
enum SLR_FLAGS | |
{ | |
SLR_NO_UI = 0x1, | |
SLR_ANY_MATCH = 0x2, | |
SLR_UPDATE = 0x4, | |
SLR_NOUPDATE = 0x8, | |
SLR_NOSEARCH = 0x10, | |
SLR_NOTRACK = 0x20, | |
SLR_NOLINKINFO = 0x40, | |
SLR_INVOKE_MSI = 0x80 | |
} | |
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")] | |
interface IShellLinkW | |
{ | |
void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out WIN32_FIND_DATAW pfd, SLGP_FLAGS fFlags); | |
void GetIDList(out IntPtr ppidl); | |
void SetIDList(IntPtr pidl); | |
void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName); | |
void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName); | |
void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath); | |
void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir); | |
void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath); | |
void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs); | |
void GetHotkey(out short pwHotkey); | |
void SetHotkey(short wHotkey); | |
void GetShowCmd(out int piShowCmd); | |
void SetShowCmd(int iShowCmd); | |
void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, int cchIconPath, out int piIcon); | |
void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon); | |
void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved); | |
void Resolve(IntPtr hwnd, SLR_FLAGS fFlags); | |
void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile); | |
} | |
[ComImport, Guid("0000010c-0000-0000-c000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] | |
public interface IPersist | |
{ | |
[PreserveSig] | |
void GetClassID(out Guid pClassID); | |
} | |
[ComImport, Guid("0000010b-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] | |
public interface IPersistFile : IPersist | |
{ | |
new void GetClassID(out Guid pClassID); | |
[PreserveSig] | |
int IsDirty(); | |
[PreserveSig] | |
void Load([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName, uint dwMode); | |
[PreserveSig] | |
void Save([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName, [In, MarshalAs(UnmanagedType.Bool)] bool fRemember); | |
[PreserveSig] | |
void SaveCompleted([In, MarshalAs(UnmanagedType.LPWStr)] string pszFileName); | |
[PreserveSig] | |
void GetCurFile([In, MarshalAs(UnmanagedType.LPWStr)] string ppszFileName); | |
} | |
const uint STGM_READ = 0; | |
const int MAX_PATH = 260; | |
[ComImport, Guid("00021401-0000-0000-C000-000000000046")] | |
public class CShellLink | |
{ | |
} | |
// ReSharper restore MemberCanBePrivate.Local | |
// ReSharper restore FieldCanBeMadeReadOnly.Local | |
// ReSharper restore UnusedMember.Local | |
// ReSharper restore InconsistentNaming | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment