File size: 9,005 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Microsoft.PowerShell
{
    internal static class ComInterfaces
    {
        [DllImport("kernel32.dll", SetLastError = false, EntryPoint = "GetStartupInfoW")]
        internal static extern void GetStartupInfo(out StartUpInfo lpStartupInfo);

        /// <remarks>
        /// IntPtr is being used for the string fields to make the marshaller faster and
        /// simpler. With IntPtr, all fields are blittable, and since we don't use the
        /// string fields at all, nothing is lost.
        /// </remarks>
        [StructLayout(LayoutKind.Sequential)]
        internal readonly struct StartUpInfo
        {
            public readonly uint cb;
            private readonly IntPtr lpReserved;
            public readonly IntPtr lpDesktop;
            public readonly IntPtr lpTitle;
            public readonly uint dwX;
            public readonly uint dwY;
            public readonly uint dwXSize;
            public readonly uint dwYSize;
            public readonly uint dwXCountChars;
            public readonly uint dwYCountChars;
            public readonly uint dwFillAttribute;
            public readonly uint dwFlags;
            public readonly ushort wShowWindow;
            private readonly ushort cbReserved2;
            private readonly IntPtr lpReserved2;
            public readonly IntPtr hStdInput;
            public readonly IntPtr hStdOutput;
            public readonly IntPtr hStdError;
        }

        [ComImport]
        [Guid("00021401-0000-0000-C000-000000000046")]
        [ClassInterface(ClassInterfaceType.None)]
        internal class CShellLink { }

        [ComImport]
        [Guid("000214F9-0000-0000-C000-000000000046")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        internal interface IShellLinkW
        {
            void GetPath(
                [Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile,
                int cchMaxPath,
                IntPtr pfd,
                uint fFlags);

            void GetIDList(out IntPtr ppidl);

            void SetIDList(IntPtr pidl);

            void GetDescription(
                [Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile,
                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 wHotKey);

            void SetHotKey(short wHotKey);

            void GetShowCmd(out uint iShowCmd);

            void SetShowCmd(uint iShowCmd);

            void GetIconLocation(
                [Out(), MarshalAs(UnmanagedType.LPWStr)] out StringBuilder pszIconPath,
                int cchIconPath,
                out int iIcon);

            void SetIconLocation(
                [MarshalAs(UnmanagedType.LPWStr)] string pszIconPath,
                int iIcon);

            void SetRelativePath(
                [MarshalAs(UnmanagedType.LPWStr)] string pszPathRel,
                uint dwReserved);

            void Resolve(IntPtr hwnd, uint fFlags);

            void SetPath(
                [MarshalAs(UnmanagedType.LPWStr)] string pszFile);
        }

        /// <summary>
        /// A property store.
        /// </summary>
        [ComImport]
        [Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        internal interface IPropertyStore
        {
            /// <summary>
            /// Gets the number of properties contained in the property store.
            /// </summary>
            /// <param name="propertyCount"></param>
            /// <returns></returns>
            [PreserveSig]
            HResult GetCount([Out] out uint propertyCount);

            /// <summary>
            /// Get a property key located at a specific index.
            /// </summary>
            /// <param name="propertyIndex"></param>
            /// <param name="key"></param>
            /// <returns></returns>
            [PreserveSig]
            HResult GetAt([In] uint propertyIndex, out PropertyKey key);

            /// <summary>
            /// Gets the value of a property from the store.
            /// </summary>
            /// <param name="key"></param>
            /// <param name="pv"></param>
            /// <returns></returns>
            [PreserveSig]
            HResult GetValue([In] in PropertyKey key, [Out] PropVariant pv);

            /// <summary>
            /// Sets the value of a property in the store.
            /// </summary>
            /// <param name="key"></param>
            /// <param name="pv"></param>
            /// <returns></returns>
            [PreserveSig]
            HResult SetValue([In] in PropertyKey key, [In] PropVariant pv);

            /// <summary>
            /// Commits the changes.
            /// </summary>
            /// <returns></returns>
            [PreserveSig]
            HResult Commit();
        }

        [ComImport]
        [Guid("6332DEBF-87B5-4670-90C0-5E57B408A49E")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        internal interface ICustomDestinationList
        {
            void SetAppID(
                [MarshalAs(UnmanagedType.LPWStr)] string pszAppID);

            [PreserveSig]
            HResult BeginList(
                out uint cMaxSlots,
                ref Guid riid,
                [Out(), MarshalAs(UnmanagedType.Interface)] out object ppvObject);

            [PreserveSig]
            HResult AppendCategory(
                [MarshalAs(UnmanagedType.LPWStr)] string pszCategory,
                [MarshalAs(UnmanagedType.Interface)] IObjectArray poa);

            void AppendKnownCategory(
                [MarshalAs(UnmanagedType.I4)] KnownDestinationCategory category);

            [PreserveSig]
            HResult AddUserTasks(
                [MarshalAs(UnmanagedType.Interface)] IObjectArray poa);

            void CommitList();

            void GetRemovedDestinations(
                ref Guid riid,
                [Out(), MarshalAs(UnmanagedType.Interface)] out object ppvObject);

            void DeleteList(
                [MarshalAs(UnmanagedType.LPWStr)] string pszAppID);

            void AbortList();
        }

        internal enum KnownDestinationCategory
        {
            Frequent = 1,
            Recent
        }

        [ComImport]
        [Guid("92CA9DCD-5622-4BBA-A805-5E9F541BD8C9")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        internal interface IObjectArray
        {
            void GetCount(out uint cObjects);

            void GetAt(
                uint iIndex,
                ref Guid riid,
                [Out(), MarshalAs(UnmanagedType.Interface)] out object ppvObject);
        }

        [ComImport]
        [Guid("5632B1A4-E38A-400A-928A-D4CD63230295")]
        [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
        internal interface IObjectCollection
        {
            // IObjectArray
            void GetCount(out uint cObjects);

            void GetAt(
                uint iIndex,
                ref Guid riid,
                [Out(), MarshalAs(UnmanagedType.Interface)] out object ppvObject);

            // IObjectCollection
            void AddObject(
                [MarshalAs(UnmanagedType.Interface)] object pvObject);

            void AddFromArray(
                [MarshalAs(UnmanagedType.Interface)] IObjectArray poaSource);

            void RemoveObject(uint uiIndex);

            void Clear();
        }

        [ComImport]
        [Guid("45e2b4ae-b1c3-11d0-b92f-00a0c90312e1"),
        InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        internal interface IShellLinkDataListW
        {
            [PreserveSig]
            int AddDataBlock(IntPtr pDataBlock);

            [PreserveSig]
            int CopyDataBlock(uint dwSig, out IntPtr ppDataBlock);

            [PreserveSig]
            int RemoveDataBlock(uint dwSig);

            void GetFlags(out uint pdwFlags);

            void SetFlags(uint dwFlags);
        }

        [DllImport("ole32.Dll")]
        internal static extern HResult CoCreateInstance(ref Guid clsid,
           [MarshalAs(UnmanagedType.IUnknown)] object inner,
           uint context,
           ref Guid uuid,
           [MarshalAs(UnmanagedType.IUnknown)] out object rReturnedComObject);
    }
}