forked from Azure/azure-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShellLink.cs
455 lines (392 loc) · 14.4 KB
/
ShellLink.cs
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
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------
namespace Microsoft.WindowsAzure.Setup
{
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
// IShellLink.ShowCmd fFlags
[Flags]
public enum ShowCmd
{
SW_SHOWNORMAL = 1,
SW_SHOWMAXIMIZED = 3,
SW_SHOWMINNOACTIVE = 7
}
// IShellLink.Resolve fFlags
[Flags]
public 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
}
// IShellLink.GetPath fFlags
[Flags]
public enum SLGP_FLAGS
{
SLGP_SHORTPATH = 0x1,
SLGP_UNCPRIORITY = 0x2,
SLGP_RAWPATH = 0x4
}
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = "Matching COM Names")]
[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_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);
}
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = "Matching COM Names")]
[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 int dwFlags);
void SetFlags(
uint dwFlags);
}
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = "Matching COM Names")]
[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);
}
// Win32 COORD
[StructLayout(LayoutKind.Sequential)]
public struct COORD
{
public short X;
public short Y;
}
// IShellDataLink NT_CONSOLE_PROPS
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = "Matching Windows Struct Names")]
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter", Justification = "Matching Windows Struct Names")]
[StructLayout(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;
}
// WIN32_FIND_DATA
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = "Matching Windows Struct Names")]
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter", Justification = "Matching Windows Struct Names")]
public struct WIN32_FIND_DATAW
{
public int dwFileAttributes;
public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
public int nFileSizeHigh;
public int nFileSizeLow;
public int dwReserved0;
public int dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAXPATH)]
public string cFileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public string cAlternateFileName;
private const int MAXPATH = 260;
}
public class ShellLink
{
private IShellLinkW shellLink;
private NT_CONSOLE_PROPS consoleProperties;
public ShellLink(string path)
{
this.shellLink = Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("00021401-0000-0000-C000-000000000046"))) as IShellLinkW;
if (File.Exists(path))
{
IntPtr consoleProperties = IntPtr.Zero;
((IPersistFile)this.shellLink).Load(path, 0);
try
{
((IShellLinkDataList)this.shellLink).CopyDataBlock(0xA0000002, out consoleProperties);
this.consoleProperties = (NT_CONSOLE_PROPS)Marshal.PtrToStructure(consoleProperties, typeof(NT_CONSOLE_PROPS));
}
catch (Exception)
{
}
}
else
{
((IPersistFile)this.shellLink).Save(path, true);
}
// Initialize default Console Properties (TODO: Fix this bug too)
if (this.consoleProperties.dwSignature != 0xA0000002)
{
this.consoleProperties = new NT_CONSOLE_PROPS();
this.consoleProperties.cbSize = Marshal.SizeOf(this.consoleProperties);
this.consoleProperties.dwSignature = 0xA0000002;
this.consoleProperties.ColorTable = new uint[16];
for (int i = 0; i < 16; i++)
{
this.consoleProperties.ColorTable[i] = 0xffffffff;
}
}
}
public IShellLinkW IShellLink
{
get { return this.shellLink; }
}
public string Path
{
get
{
StringBuilder sb = new StringBuilder(260);
WIN32_FIND_DATAW pfd = new WIN32_FIND_DATAW();
this.IShellLink.GetPath(sb, 260, out pfd, SLGP_FLAGS.SLGP_RAWPATH);
return sb.ToString();
}
set
{
this.IShellLink.SetPath(value);
}
}
public string Description
{
get
{
StringBuilder sb = new StringBuilder(2048);
this.IShellLink.GetDescription(sb, 2048);
return sb.ToString();
}
set { this.IShellLink.SetDescription(value); }
}
public string WorkingDirectory
{
get
{
StringBuilder sb = new StringBuilder(260);
this.IShellLink.GetWorkingDirectory(sb, 260);
return sb.ToString();
}
set { this.IShellLink.SetWorkingDirectory(value); }
}
public ShowCmd ShowCmd
{
get
{
int showCmd;
this.IShellLink.GetShowCmd(out showCmd);
return (ShowCmd)Enum.ToObject(typeof(ShowCmd), showCmd);
}
set
{
this.IShellLink.SetShowCmd((int)value);
}
}
public NT_CONSOLE_PROPS ConsoleProperties
{
get { return this.consoleProperties; }
set { this.consoleProperties = value; }
}
public bool QuickEditMode
{
get { return this.consoleProperties.bQuickEdit; }
set { this.consoleProperties.bQuickEdit = value; }
}
public bool InsertMode
{
get { return this.consoleProperties.bInsertMode; }
set { this.consoleProperties.bInsertMode = value; }
}
public bool AutoPosition
{
get { return this.consoleProperties.bAutoPosition; }
set { this.consoleProperties.bAutoPosition = value; }
}
public uint CommandHistoryBufferSize
{
get { return this.consoleProperties.uHistoryBufferSize; }
set { this.consoleProperties.uHistoryBufferSize = value; }
}
public uint CommandHistoryBufferCount
{
get { return this.consoleProperties.uNumberOfHistoryBuffers; }
set { this.consoleProperties.uNumberOfHistoryBuffers = value; }
}
public byte ScreenBackgroundColor
{
set
{
this.consoleProperties.wFillAttribute &= 0x000f;
this.consoleProperties.wFillAttribute += (ushort)(value << 4);
}
}
public byte ScreenTextColor
{
set
{
this.consoleProperties.wFillAttribute &= 0x00f0;
this.consoleProperties.wFillAttribute += value;
}
}
public byte PopUpBackgroundColor
{
set
{
this.consoleProperties.wPopupFillAttribute &= 0x000f;
this.consoleProperties.wPopupFillAttribute += (ushort)(value << 4);
}
}
public byte PopUpTextColor
{
set
{
this.consoleProperties.wPopupFillAttribute &= 0x00f0;
this.consoleProperties.wPopupFillAttribute += value;
}
}
public void Save(string path)
{
this.SetConsoleProperties();
((IPersistFile)this.shellLink).Save(path, true);
}
public void Save()
{
this.SetConsoleProperties();
((IPersistFile)this.shellLink).Save(null, true);
}
public void SetScreenBufferSize(short x, short y)
{
COORD c = new COORD();
c.X = x;
c.Y = y;
this.consoleProperties.dwScreenBufferSize = c;
}
public void SetWindowSize(short x, short y)
{
COORD c = new COORD();
c.X = x;
c.Y = y;
this.consoleProperties.dwWindowSize = c;
}
public void SetFont()
{
this.consoleProperties.FaceName = "Lucida Console";
this.consoleProperties.uFontFamily = 54;
this.consoleProperties.uFontWeight = 400;
this.consoleProperties.uCursorSize = 25;
}
// This does more than console colors
private void SetConsoleProperties()
{
IntPtr consoleProperties = Marshal.AllocCoTaskMem(this.consoleProperties.cbSize);
Marshal.StructureToPtr(this.consoleProperties, consoleProperties, true);
((IShellLinkDataList)this.shellLink).RemoveDataBlock(0xA0000002);
((IShellLinkDataList)this.shellLink).AddDataBlock(consoleProperties);
}
}
}