-
Notifications
You must be signed in to change notification settings - Fork 403
/
Copy pathWin32ShellContextMenu.cs
314 lines (262 loc) · 8.72 KB
/
Win32ShellContextMenu.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
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace BizHawk.Common
{
public class Win32ShellContextMenu
{
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe")]
public interface IShellItem
{
IntPtr BindToHandler(IntPtr pbc,
[MarshalAs(UnmanagedType.LPStruct)] Guid bhid,
[MarshalAs(UnmanagedType.LPStruct)] Guid riid);
[PreserveSig]
int GetParent(out IShellItem ppsi);
IntPtr GetDisplayName(uint sigdnName);
uint GetAttributes(uint sfgaoMask);
int Compare(IShellItem psi, uint hint);
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214F2-0000-0000-C000-000000000046")]
public interface IEnumIDList
{
[PreserveSig]
int Next(uint celt, out IntPtr rgelt, out uint pceltFetched);
[PreserveSig]
int Skip(uint celt);
[PreserveSig]
int Reset();
IEnumIDList Clone();
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214E6-0000-0000-C000-000000000046")]
public interface IShellFolder
{
void ParseDisplayName(
[In] IntPtr hwnd,
[In] IntPtr pbc,
[In, MarshalAs(UnmanagedType.LPWStr)] string pszDisplayName,
[Out] out uint pchEaten,
[Out] out IntPtr ppidl,
[In, Out] ref uint pdwAttributes);
[PreserveSig]
int EnumObjects(
[In] IntPtr hwnd,
[In] SHCONTF grfFlags,
[Out] out IEnumIDList ppenumIDList);
void BindToObject(IntPtr pidl, IntPtr pbc,
[MarshalAs(UnmanagedType.LPStruct)] Guid riid,
out IntPtr ppv);
void BindToStorage(IntPtr pidl, IntPtr pbc,
[MarshalAs(UnmanagedType.LPStruct)] Guid riid,
out IntPtr ppv);
[PreserveSig]
short CompareIDs(uint lParam, IntPtr pidl1, IntPtr pidl2);
IntPtr CreateViewObject(IntPtr hwndOwner,
[MarshalAs(UnmanagedType.LPStruct)] Guid riid);
void GetAttributesOf(uint cidl,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] IntPtr[] apidl,
ref uint rgfInOut);
void GetUIObjectOf(IntPtr hwndOwner, uint cidl,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] IntPtr[] apidl,
[MarshalAs(UnmanagedType.LPStruct)] Guid riid,
uint rgfReserved,
out IntPtr ppv);
void GetDisplayNameOf(IntPtr pidl, int uFlags, out STRRET pName);
void SetNameOf(IntPtr hwnd, IntPtr pidl, string pszName, SHCONTF uFlags, out IntPtr ppidlOut);
public enum SHCONTF
{
FOLDERS = 0x0020,
NONFOLDERS = 0x0040,
INCLUDEHIDDEN = 0x0080,
INIT_ON_FIRST_NEXT = 0x0100,
NETPRINTERSRCH = 0x0200,
SHAREABLE = 0x0400,
STORAGE = 0x0800
}
[StructLayout(LayoutKind.Explicit, Size = 264)]
public struct STRRET
{
[FieldOffset(0)]
public uint uType;
[FieldOffset(4)]
public IntPtr pOleStr;
[FieldOffset(4)]
public IntPtr pStr;
[FieldOffset(4)]
public uint uOffset;
[FieldOffset(4)]
public IntPtr cStr;
}
}
[Flags]
public enum TPM
{
TPM_LEFTBUTTON = 0x0000,
TPM_RIGHTBUTTON = 0x0002,
TPM_LEFTALIGN = 0x0000,
TPM_CENTERALIGN = 0x000,
TPM_RIGHTALIGN = 0x000,
TPM_TOPALIGN = 0x0000,
TPM_VCENTERALIGN = 0x0010,
TPM_BOTTOMALIGN = 0x0020,
TPM_HORIZONTAL = 0x0000,
TPM_VERTICAL = 0x0040,
TPM_NONOTIFY = 0x0080,
TPM_RETURNCMD = 0x0100,
TPM_RECURSE = 0x0001,
TPM_HORPOSANIMATION = 0x0400,
TPM_HORNEGANIMATION = 0x0800,
TPM_VERPOSANIMATION = 0x1000,
TPM_VERNEGANIMATION = 0x2000,
TPM_NOANIMATION = 0x4000,
TPM_LAYOUTRTL = 0x8000,
}
[Flags]
public enum CMF : uint
{
NORMAL = 0x00000000,
DEFAULTONLY = 0x00000001,
VERBSONLY = 0x00000002,
EXPLORE = 0x00000004,
NOVERBS = 0x00000008,
CANRENAME = 0x00000010,
NODEFAULT = 0x00000020,
INCLUDESTATIC = 0x00000040,
EXTENDEDVERBS = 0x00000100,
RESERVED = 0xffff0000,
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct CMINVOKECOMMANDINFO
{
public int cbSize;
public int fMask;
public IntPtr hwnd;
public string lpVerb;
public string lpParameters;
public string lpDirectory;
public int nShow;
public int dwHotKey;
public IntPtr hIcon;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct CMINVOKECOMMANDINFO_ByIndex
{
public int cbSize;
public int fMask;
public IntPtr hwnd;
public int iVerb;
public string lpParameters;
public string lpDirectory;
public int nShow;
public int dwHotKey;
public IntPtr hIcon;
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214e4-0000-0000-c000-000000000046")]
public interface IContextMenu
{
[PreserveSig]
int QueryContextMenu(IntPtr hMenu, uint indexMenu, int idCmdFirst, int idCmdLast, CMF uFlags);
void InvokeCommand(ref CMINVOKECOMMANDINFO pici);
[PreserveSig]
int GetCommandString(int idcmd, uint uflags, int reserved,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder commandstring,
int cch);
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214f4-0000-0000-c000-000000000046")]
public interface IContextMenu2 : IContextMenu
{
[PreserveSig]
new int QueryContextMenu(IntPtr hMenu, uint indexMenu, int idCmdFirst, int idCmdLast, CMF uFlags);
void InvokeCommand(ref CMINVOKECOMMANDINFO_ByIndex pici);
[PreserveSig]
new int GetCommandString(int idcmd, uint uflags, int reserved,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder commandstring,
int cch);
[PreserveSig]
int HandleMenuMsg(int uMsg, IntPtr wParam, IntPtr lParam);
}
[DllImport("shell32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
public static extern IShellItem SHCreateItemFromParsingName(
[In] string pszPath,
[In] IntPtr pbc,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid riid);
[DllImport("shell32.dll", PreserveSig = false)]
public static extern IntPtr SHGetIDListFromObject([In, MarshalAs(UnmanagedType.IUnknown)] object punk);
[DllImport("shell32.dll", EntryPoint = "#16")]
public static extern IntPtr ILFindLastID(IntPtr pidl);
[DllImport("user32.dll")]
public static extern int TrackPopupMenuEx(IntPtr hmenu, TPM fuFlags, int x, int y, IntPtr hwnd, IntPtr lptpm);
private IContextMenu ComInterface { get; }
private IContextMenu2 ComInterface2 { get; }
private static readonly Guid SFObject = new("3981e224-f559-11d3-8e3a-00c04f6837d5");
private Win32ShellContextMenu(string path)
{
var uri = new Uri(path);
// this should be the only scheme used in practice
if (uri.Scheme != "file")
{
throw new NotSupportedException("Non-file Uri schemes are unsupported");
}
var shellItem = SHCreateItemFromParsingName(uri.LocalPath, IntPtr.Zero, typeof(IShellItem).GUID);
var pidls = new IntPtr[1];
pidls[0] = ILFindLastID(SHGetIDListFromObject(shellItem));
shellItem.GetParent(out var parent);
var result = parent.BindToHandler(IntPtr.Zero, SFObject, typeof(IShellFolder).GUID);
var shellFolder = (IShellFolder)Marshal.GetObjectForIUnknown(result);
shellFolder.GetUIObjectOf(IntPtr.Zero, 1, pidls, typeof(IContextMenu).GUID, 0, out result);
ComInterface = (IContextMenu)Marshal.GetObjectForIUnknown(result);
ComInterface2 = (IContextMenu2)ComInterface;
}
private ref struct TempMenu
{
[DllImport("user32.dll")]
private static extern IntPtr CreatePopupMenu();
[DllImport("user32.dll", SetLastError = true)]
private static extern bool DestroyMenu(IntPtr hMenu);
public IntPtr Handle { get; private set; }
public TempMenu()
{
Handle = CreatePopupMenu();
if (Handle == IntPtr.Zero)
{
throw new InvalidOperationException($"{nameof(CreatePopupMenu)} returned NULL!");
}
}
public void Dispose()
{
if (Handle != IntPtr.Zero)
{
_ = DestroyMenu(Handle);
Handle = IntPtr.Zero;
}
}
}
public static void ShowContextMenu(string path, IntPtr parentWindow, int x, int y)
{
var ctxMenu = new Win32ShellContextMenu(path);
using var menu = new TempMenu();
const int CmdFirst = 0x8000;
ctxMenu.ComInterface.QueryContextMenu(menu.Handle, 0, CmdFirst, int.MaxValue, CMF.EXPLORE);
var command = TrackPopupMenuEx(menu.Handle, TPM.TPM_RETURNCMD, x, y, parentWindow, IntPtr.Zero);
if (command > 0)
{
const int SW_SHOWNORMAL = 1;
CMINVOKECOMMANDINFO_ByIndex invoke = default;
invoke.cbSize = Marshal.SizeOf(invoke);
invoke.iVerb = command - CmdFirst;
invoke.nShow = SW_SHOWNORMAL;
ctxMenu.ComInterface2.InvokeCommand(ref invoke);
}
}
}
}