1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Text ;
4+ using System . Runtime . InteropServices ;
5+ using System . IO ;
6+ using Accessibility ;
7+ using System . Runtime . InteropServices . ComTypes ;
8+ using System . Security . Policy ;
9+
10+ namespace Flow . Launcher . Plugin . Program . Programs
11+ {
12+ class ShellLinkHelper
13+ {
14+ [ Flags ( ) ]
15+ public enum SLGP_FLAGS
16+ {
17+ SLGP_SHORTPATH = 0x1 ,
18+ SLGP_UNCPRIORITY = 0x2 ,
19+ SLGP_RAWPATH = 0x4
20+ }
21+
22+ [ StructLayout ( LayoutKind . Sequential , CharSet = CharSet . Auto ) ]
23+ public struct WIN32_FIND_DATAW
24+ {
25+ public uint dwFileAttributes ;
26+ public long ftCreationTime ;
27+ public long ftLastAccessTime ;
28+ public long ftLastWriteTime ;
29+ public uint nFileSizeHigh ;
30+ public uint nFileSizeLow ;
31+ public uint dwReserved0 ;
32+ public uint dwReserved1 ;
33+ [ MarshalAs ( UnmanagedType . ByValTStr , SizeConst = 260 ) ]
34+ public string cFileName ;
35+ [ MarshalAs ( UnmanagedType . ByValTStr , SizeConst = 14 ) ]
36+ public string cAlternateFileName ;
37+ }
38+
39+ [ Flags ( ) ]
40+ public enum SLR_FLAGS
41+ {
42+ SLR_NO_UI = 0x1 ,
43+ SLR_ANY_MATCH = 0x2 ,
44+ SLR_UPDATE = 0x4 ,
45+ SLR_NOUPDATE = 0x8 ,
46+ SLR_NOSEARCH = 0x10 ,
47+ SLR_NOTRACK = 0x20 ,
48+ SLR_NOLINKINFO = 0x40 ,
49+ SLR_INVOKE_MSI = 0x80
50+ }
51+
52+
53+ // Reference : http://www.pinvoke.net/default.aspx/Interfaces.IShellLinkW
54+ /// The IShellLink interface allows Shell links to be created, modified, and resolved
55+ [ ComImport ( ) , InterfaceType ( ComInterfaceType . InterfaceIsIUnknown ) , Guid ( "000214F9-0000-0000-C000-000000000046" ) ]
56+ interface IShellLinkW
57+ {
58+ /// <summary>Retrieves the path and file name of a Shell link object</summary>
59+ void GetPath ( [ Out ( ) , MarshalAs ( UnmanagedType . LPWStr ) ] StringBuilder pszFile , int cchMaxPath , ref WIN32_FIND_DATAW pfd , SLGP_FLAGS fFlags ) ;
60+ /// <summary>Retrieves the list of item identifiers for a Shell link object</summary>
61+ void GetIDList ( out IntPtr ppidl ) ;
62+ /// <summary>Sets the pointer to an item identifier list (PIDL) for a Shell link object.</summary>
63+ void SetIDList ( IntPtr pidl ) ;
64+ /// <summary>Retrieves the description string for a Shell link object</summary>
65+ void GetDescription ( [ Out ( ) , MarshalAs ( UnmanagedType . LPWStr ) ] StringBuilder pszName , int cchMaxName ) ;
66+ /// <summary>Sets the description for a Shell link object. The description can be any application-defined string</summary>
67+ void SetDescription ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string pszName ) ;
68+ /// <summary>Retrieves the name of the working directory for a Shell link object</summary>
69+ void GetWorkingDirectory ( [ Out ( ) , MarshalAs ( UnmanagedType . LPWStr ) ] StringBuilder pszDir , int cchMaxPath ) ;
70+ /// <summary>Sets the name of the working directory for a Shell link object</summary>
71+ void SetWorkingDirectory ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string pszDir ) ;
72+ /// <summary>Retrieves the command-line arguments associated with a Shell link object</summary>
73+ void GetArguments ( [ Out ( ) , MarshalAs ( UnmanagedType . LPWStr ) ] StringBuilder pszArgs , int cchMaxPath ) ;
74+ /// <summary>Sets the command-line arguments for a Shell link object</summary>
75+ void SetArguments ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string pszArgs ) ;
76+ /// <summary>Retrieves the hot key for a Shell link object</summary>
77+ void GetHotkey ( out short pwHotkey ) ;
78+ /// <summary>Sets a hot key for a Shell link object</summary>
79+ void SetHotkey ( short wHotkey ) ;
80+ /// <summary>Retrieves the show command for a Shell link object</summary>
81+ void GetShowCmd ( out int piShowCmd ) ;
82+ /// <summary>Sets the show command for a Shell link object. The show command sets the initial show state of the window.</summary>
83+ void SetShowCmd ( int iShowCmd ) ;
84+ /// <summary>Retrieves the location (path and index) of the icon for a Shell link object</summary>
85+ void GetIconLocation ( [ Out ( ) , MarshalAs ( UnmanagedType . LPWStr ) ] StringBuilder pszIconPath ,
86+ int cchIconPath , out int piIcon ) ;
87+ /// <summary>Sets the location (path and index) of the icon for a Shell link object</summary>
88+ void SetIconLocation ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string pszIconPath , int iIcon ) ;
89+ /// <summary>Sets the relative path to the Shell link object</summary>
90+ void SetRelativePath ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string pszPathRel , int dwReserved ) ;
91+ /// <summary>Attempts to find the target of a Shell link, even if it has been moved or renamed</summary>
92+ void Resolve ( ref Accessibility . _RemotableHandle hwnd , SLR_FLAGS fFlags ) ;
93+ /// <summary>Sets the path and file name of a Shell link object</summary>
94+ void SetPath ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string pszFile ) ;
95+ }
96+
97+ [ ComImport ( ) , Guid ( "00021401-0000-0000-C000-000000000046" ) ]
98+ public class ShellLink
99+ {
100+ }
101+
102+ // To initialize the app description
103+ public String description = String . Empty ;
104+
105+
106+ // Retrieve the target path using Shell Link
107+ public string retrieveTargetPath ( string path )
108+ {
109+ var link = new ShellLink ( ) ;
110+ const int STGM_READ = 0 ;
111+ ( ( IPersistFile ) link ) . Load ( path , STGM_READ ) ;
112+ var hwnd = new _RemotableHandle ( ) ;
113+ ( ( IShellLinkW ) link ) . Resolve ( ref hwnd , 0 ) ;
114+
115+ const int MAX_PATH = 260 ;
116+ StringBuilder buffer = new StringBuilder ( MAX_PATH ) ;
117+
118+ var data = new WIN32_FIND_DATAW ( ) ;
119+ ( ( IShellLinkW ) link ) . GetPath ( buffer , buffer . Capacity , ref data , SLGP_FLAGS . SLGP_SHORTPATH ) ;
120+ var target = buffer . ToString ( ) ;
121+
122+ // To set the app description
123+ if ( ! String . IsNullOrEmpty ( target ) )
124+ {
125+ buffer = new StringBuilder ( MAX_PATH ) ;
126+ ( ( IShellLinkW ) link ) . GetDescription ( buffer , MAX_PATH ) ;
127+ description = buffer . ToString ( ) ;
128+ }
129+ return target ;
130+ }
131+ }
132+ }
0 commit comments