-
Notifications
You must be signed in to change notification settings - Fork 176
/
Get-Link.ps1
508 lines (430 loc) · 14.4 KB
/
Get-Link.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
param([Parameter(ValueFromPipelineByPropertyName=$true)][Alias("PSPath")][string]$Path)
begin {
Add-Type -TypeDef @"
using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
namespace Huddled.Interop
{
[Flags()] // SHELL_LINK_DATA_FLAGS
public enum ShellLinkFlags : uint {
Default = 0x00000000,
HasIdList = 0x00000001,
HasLinkInfo = 0x00000002,
HasName = 0x00000004,
HasRelpath = 0x00000008,
HasWorkingdir = 0x00000010,
HasArgs = 0x00000020,
HasIconLocation = 0x00000040,
Unicode = 0x00000080,
ForceNoLinkInfo = 0x00000100,
HasExpSz = 0x00000200,
RunInSeparate = 0x00000400,
HasLogo3Id = 0x00000800,
HasDarwinId = 0x00001000,
RunasUser = 0x00002000,
HasExpIconSz = 0x00004000,
NoPidlAlias = 0x00008000,
ForceUncname = 0x00010000,
RunWithShimlayer = 0x00020000,
ForceNoLinktrack = 0x00040000,
EnableTargetMetadata = 0x00080000,
DisableLinkPathTracking = 0x00100000,
DisableKnownfolderRelativeTracking = 0x00200000,
NoKFAlias = 0x00400000,
AllowLinkToLink = 0x00800000,
UnaliasOnSave = 0x01000000,
PreferEnvironmentPath = 0x02000000,
KeepLocalIdListForUncTarget = 0x04000000,
Valid = 0x07fff7ff,
Reserved = (uint)0x80000000
}
// IShellLink.ShowCmd fFlags
[Flags()]
public enum ShowCmd
{
Normal = 1,
Maximized = 3,
Minnoactive = 7
}
// IShellLink.Resolve fFlags SLR_FLAGS
[Flags()]
public enum ResolveFlags
{
NoUI = 0x1,
AnyMatch = 0x2,
Update = 0x4,
NoUpdate = 0x8,
NoSearch = 0x10,
NoTrack = 0x20,
NoLinkInfo = 0x40,
InvokeMsi = 0x80
}
// IShellLink.GetPath fFlags SLGP_FLAGS
[Flags()]
public enum GetPathFlags
{
ShortPath = 0x1,
UncPriority = 0x2,
RawPath = 0x4
}
// Win32 COORD
[StructLayout(LayoutKind.Sequential)]
public struct COORD
{
public short X;
public short Y;
}
// IShellDataLink NT_CONSOLE_PROPS
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct NT_CONSOLE_PROPS
{
public int cbSize; // Size of this extra data block
public uint dwSignature; // signature of this extra data block
public ushort wFillAttribute; // fill attribute for console
public ushort wPopupFillAttribute; // fill attribute for console popups
public COORD dwScreenBufferSize; // screen buffer size for console
public COORD dwWindowSize; // window size for console
public COORD dwWindowOrigin; // window origin for console
public int nFont;
public int nInputBufferSize;
public COORD dwFontSize;
public uint uFontFamily;
public uint uFontWeight;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string FaceName;
public uint uCursorSize;
public bool bFullScreen;
public bool bQuickEdit;
public bool bInsertMode;
public bool bAutoPosition;
public uint uHistoryBufferSize;
public uint uNumberOfHistoryBuffers;
public bool bHistoryNoDup;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public uint[] ColorTable;
}
// The CharSet must match the CharSet of the corresponding PInvoke signature
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct WIN32_FIND_DATA
{
public uint dwFileAttributes;
public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
public System.Runtime.InteropServices.ComTypes.FILETIME 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;
}
[ComImport(),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("0000010B-0000-0000-C000-000000000046")]
public interface IPersistFile
{
#region Methods inherited from IPersist
void GetClassID(
out Guid pClassID);
#endregion
[PreserveSig()]
int IsDirty();
void Load(
[MarshalAs(UnmanagedType.LPWStr)] string pszFileName,
int dwMode);
void Save(
[MarshalAs(UnmanagedType.LPWStr)] string pszFileName,
[MarshalAs(UnmanagedType.Bool)] bool fRemember);
void SaveCompleted(
[MarshalAs(UnmanagedType.LPWStr)] string pszFileName);
void GetCurFile(
out IntPtr ppszFileName);
}
[ComImport(),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("000214F9-0000-0000-C000-000000000046")]
public interface IShellLinkW
{
void GetPath(
[Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile,
int cchMaxPath,
out WIN32_FIND_DATA pfd,
GetPathFlags 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,
ResolveFlags fFlags);
void SetPath(
[MarshalAs(UnmanagedType.LPWStr)] string pszFile);
}
[ComImport(),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("45E2b4AE-B1C3-11D0-B92F-00A0C90312E1")]
public interface IShellLinkDataList
{
void AddDataBlock(IntPtr pDataBlock);
void CopyDataBlock(uint dwSig, out IntPtr ppDataBlock);
void RemoveDataBlock(uint dwSig);
void GetFlags(out uint dwFlags);
void SetFlags(uint dwFlags);
}
public class ShellLink
{
private IShellLinkW _ShellLink;
private NT_CONSOLE_PROPS _ConsoleProperties;
private ushort _ScreenFill;
private ushort _PopUpFill;
public Color[] ConsoleColors = new Color[16];
public ShellLink(string Path) {
_ShellLink = Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("00021401-0000-0000-C000-000000000046"))) as IShellLinkW;
if (File.Exists(Path))
{
IntPtr pConsoleProperties = IntPtr.Zero;
((IPersistFile)_ShellLink).Load(Path, 0);
try
{
// TODO: Fix memory leak here
((IShellLinkDataList)_ShellLink).CopyDataBlock(0xA0000002, out pConsoleProperties);
_ConsoleProperties = (NT_CONSOLE_PROPS)Marshal.PtrToStructure(pConsoleProperties, typeof(NT_CONSOLE_PROPS));
_ScreenFill = _ConsoleProperties.wFillAttribute;
_PopUpFill = _ConsoleProperties.wPopupFillAttribute;
for (int i = 0; i < 16; i++)
{
ConsoleColors[i] = ColorTranslator.FromWin32((int)_ConsoleProperties.ColorTable[i]);
}
}
catch (Exception)
{
}
}
else
{
((IPersistFile)_ShellLink).Save(Path, true);
}
// TODO: Don't fake it, pull these values from the Registry
// Initialize default Console Properties
if (_ConsoleProperties.dwSignature != 0xA0000002)
{
_ConsoleProperties = new NT_CONSOLE_PROPS();
_ConsoleProperties.cbSize = Marshal.SizeOf(_ConsoleProperties);
_ConsoleProperties.dwSignature = 0xA0000002;
_ConsoleProperties.ColorTable = new uint[16];
_ConsoleProperties.dwWindowSize.X = 120;
_ConsoleProperties.dwWindowSize.Y = 30;
_ConsoleProperties.dwScreenBufferSize.X = 120;
_ConsoleProperties.dwScreenBufferSize.Y = 8000;
_ConsoleProperties.bQuickEdit = true;
_ConsoleProperties.bAutoPosition = true;
for (int i = 0; i < 16; i++)
{
_ConsoleProperties.ColorTable[i] = 0xffffffff;
}
}
}
public ShellLinkFlags Flags {
get {
uint flags;
((IShellLinkDataList)_ShellLink).GetFlags( out flags );
return (ShellLinkFlags)flags;
}
set {
((IShellLinkDataList)_ShellLink).SetFlags( (uint)value );
}
}
public void Save(string Path)
{
SetConsoleColors();
((IPersistFile)_ShellLink).Save(Path, true);
}
public void Save()
{
SetConsoleColors();
((IPersistFile)_ShellLink).Save(null, true);
}
public IShellLinkW GetShellLink()
{
return _ShellLink;
}
public string Path
{
get
{
StringBuilder sb = new StringBuilder(260);
WIN32_FIND_DATA pfd = new WIN32_FIND_DATA();
_ShellLink.GetPath(sb, 259, out pfd, GetPathFlags.RawPath);
return sb.ToString();
}
set
{
_ShellLink.SetPath(value);
}
}
public string Description
{
get
{
StringBuilder sb = new StringBuilder(2048);
_ShellLink.GetDescription(sb, 2048);
return sb.ToString();
}
set { _ShellLink.SetDescription(value); }
}
public string WorkingDirectory
{
get
{
StringBuilder sb = new StringBuilder(260);
_ShellLink.GetWorkingDirectory(sb, 260);
return sb.ToString();
}
set { _ShellLink.SetWorkingDirectory(value); }
}
public ShowCmd ShowCmd
{
get
{
int nShowCmd;
_ShellLink.GetShowCmd(out nShowCmd);
return (ShowCmd)Enum.ToObject(typeof(ShowCmd), nShowCmd);
}
set
{
_ShellLink.SetShowCmd((int)value);
}
}
public bool QuickEditMode
{
get { return _ConsoleProperties.bQuickEdit; }
set { _ConsoleProperties.bQuickEdit = value; }
}
public bool InsertMode
{
get { return _ConsoleProperties.bInsertMode; }
set { _ConsoleProperties.bInsertMode = value; }
}
public bool AutoPosition
{
get { return _ConsoleProperties.bAutoPosition; }
set { _ConsoleProperties.bAutoPosition = value; }
}
public void SetScreenBufferSize(short x, short y)
{
_ConsoleProperties.dwScreenBufferSize.X = x;
_ConsoleProperties.dwScreenBufferSize.Y = y;
}
public void SetWindowSize(short x, short y)
{
_ConsoleProperties.dwWindowSize.X = x;
_ConsoleProperties.dwWindowSize.Y = y;
}
public uint CommandHistoryBufferSize
{
get { return _ConsoleProperties.uHistoryBufferSize; }
set { _ConsoleProperties.uHistoryBufferSize = value; }
}
public uint CommandHistoryBufferCount
{
get { return _ConsoleProperties.uNumberOfHistoryBuffers; }
set { _ConsoleProperties.uNumberOfHistoryBuffers = value; }
}
public byte ScreenBackgroundColor
{
set
{
_ScreenFill &= 0x000f;
_ScreenFill += (ushort)(value << 4);
}
}
public byte ScreenTextColor
{
set
{
_ScreenFill &= 0x00f0;
_ScreenFill += value;
}
}
public byte PopUpBackgroundColor
{
set
{
_PopUpFill &= 0x000f;
_PopUpFill += (ushort)(value << 4);
}
}
public byte PopUpTextColor
{
set
{
_PopUpFill &= 0x00f0;
_PopUpFill += value;
}
}
// This does more than console colors
private void SetConsoleColors()
{
for (int i = 0; i < 16; i++)
{
_ConsoleProperties.ColorTable[i] = (uint)ColorTranslator.ToWin32(ConsoleColors[i]);
}
_ConsoleProperties.wFillAttribute = _ScreenFill;
_ConsoleProperties.wPopupFillAttribute = _PopUpFill; ;
IntPtr pConsoleProperties = Marshal.AllocCoTaskMem(_ConsoleProperties.cbSize);
Marshal.StructureToPtr(_ConsoleProperties, pConsoleProperties, true);
((IShellLinkDataList)_ShellLink).RemoveDataBlock(0xA0000002);
((IShellLinkDataList)_ShellLink).AddDataBlock(pConsoleProperties);
}
}
}
"@ -ReferencedAssemblies System.Drawing
function global:Get-Link {
param([Parameter(ValueFromPipelineByPropertyName=$true)][Alias("PSPath")][string]$Path)
process {
New-Object Huddled.Interop.ShellLink (Convert-Path $Path)
}
}
}
process {
Get-Link $Path
}