@@ -129,10 +129,12 @@ public void Dispose()
129
129
public class ContextMenu : Win32ContextMenu , IDisposable
130
130
{
131
131
private Shell32 . IContextMenu cMenu ;
132
+ private User32 . SafeHMENU hMenu ;
132
133
133
- public ContextMenu ( Shell32 . IContextMenu cMenu )
134
+ public ContextMenu ( Shell32 . IContextMenu cMenu , User32 . SafeHMENU hMenu )
134
135
{
135
136
this . cMenu = cMenu ;
137
+ this . hMenu = hMenu ;
136
138
this . Items = new List < Win32ContextMenuItem > ( ) ;
137
139
}
138
140
@@ -203,11 +205,10 @@ public static ContextMenu GetContextMenuForFiles(ShellItem[] shellItems, Shell32
203
205
return null ;
204
206
using var sf = shellItems . First ( ) . Parent ; // HP: the items are all in the same folder
205
207
Shell32 . IContextMenu menu = sf . GetChildrenUIObjects < Shell32 . IContextMenu > ( null , shellItems ) ;
206
- var contextMenu = new ContextMenu ( menu ) ;
207
208
var hMenu = User32 . CreatePopupMenu ( ) ;
208
209
menu . QueryContextMenu ( hMenu , 0 , 1 , 0x7FFF , flags ) ;
210
+ var contextMenu = new ContextMenu ( menu , hMenu ) ;
209
211
ContextMenu . EnumMenuItems ( menu , hMenu , contextMenu . Items , itemFilter ) ;
210
- User32 . DestroyMenu ( hMenu ) ;
211
212
return contextMenu ;
212
213
}
213
214
@@ -264,9 +265,6 @@ private static void EnumMenuItems(
264
265
try
265
266
{
266
267
( cMenu as Shell32 . IContextMenu2 ) ? . HandleMenuMsg ( ( uint ) User32 . WindowMessage . WM_INITMENUPOPUP , ( IntPtr ) mii . hSubMenu , new IntPtr ( ii ) ) ;
267
- // Skip this items, clicking on them probably won't work
268
- container . Dispose ( ) ;
269
- continue ;
270
268
}
271
269
catch ( NotImplementedException )
272
270
{
@@ -366,6 +364,11 @@ protected virtual void Dispose(bool disposing)
366
364
}
367
365
368
366
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
367
+ if ( hMenu != null )
368
+ {
369
+ User32 . DestroyMenu ( hMenu ) ;
370
+ hMenu = null ;
371
+ }
369
372
if ( cMenu != null )
370
373
{
371
374
Marshal . ReleaseComObject ( cMenu ) ;
0 commit comments