@@ -213,35 +213,25 @@ public static HRESULT TryUnpinFolderFromQuickAccess(this IWindowsFolder @this)
213213 using ComPtr < IContextMenu2 > pContextMenu2 = default ;
214214 using ComHeapPtr < ITEMIDLIST > pShellFolderPidl = default ;
215215
216- hr = PInvoke . CoCreateInstance ( CLSID . CLSID_NewMenu , null , CLSCTX . CLSCTX_INPROC_SERVER , IID . IID_IContextMenu , ( void * * ) & pNewMenu ) ;
217- if ( hr . ThrowIfFailedOnDebug ( ) . Failed ) return null ;
218-
219- hr = pNewMenu ->QueryInterface ( IID . IID_IContextMenu2 , ( void * * ) pContextMenu2 . GetAddressOf ( ) ) ;
220- if ( hr . ThrowIfFailedOnDebug ( ) . Failed ) return null ;
221-
222- hr = pNewMenu ->QueryInterface ( IID . IID_IShellExtInit , ( void * * ) pShellExtInit . GetAddressOf ( ) ) ;
223- if ( hr . ThrowIfFailedOnDebug ( ) . Failed ) return null ;
216+ hr = PInvoke . CoCreateInstance ( CLSID . CLSID_NewMenu , null , CLSCTX . CLSCTX_INPROC_SERVER , IID . IID_IContextMenu , ( void * * ) & pNewMenu ) . ThrowOnFailure ( ) ;
217+ hr = pNewMenu ->QueryInterface ( IID . IID_IContextMenu2 , ( void * * ) pContextMenu2 . GetAddressOf ( ) ) . ThrowOnFailure ( ) ;
218+ hr = pNewMenu ->QueryInterface ( IID . IID_IShellExtInit , ( void * * ) pShellExtInit . GetAddressOf ( ) ) . ThrowOnFailure ( ) ;
224219
225220 @this . ShellNewMenu = pNewMenu ;
226221
227- hr = PInvoke . SHGetIDListFromObject ( ( IUnknown * ) @this . ThisPtr , pShellFolderPidl . GetAddressOf ( ) ) ;
228- if ( hr . ThrowIfFailedOnDebug ( ) . Failed ) return null ;
229-
230- hr = pShellExtInit . Get ( ) ->Initialize ( pShellFolderPidl . Get ( ) , null , default ) ;
231- if ( hr . ThrowIfFailedOnDebug ( ) . Failed ) return null ;
222+ hr = PInvoke . SHGetIDListFromObject ( ( IUnknown * ) @this . ThisPtr , pShellFolderPidl . GetAddressOf ( ) ) . ThrowOnFailure ( ) ;
223+ hr = pShellExtInit . Get ( ) ->Initialize ( pShellFolderPidl . Get ( ) , null , default ) . ThrowOnFailure ( ) ;
232224
233225 // Inserts "New (&W)"
234226 HMENU hMenu = PInvoke . CreatePopupMenu ( ) ;
235- hr = pNewMenu ->QueryContextMenu ( hMenu , 0 , 1 , 256 , 0 ) ;
236- if ( hr . ThrowIfFailedOnDebug ( ) . Failed ) return null ;
227+ hr = pNewMenu ->QueryContextMenu ( hMenu , 0 , 1 , 256 , 0 ) . ThrowOnFailure ( ) ;
237228
238229 // Populates the hSubMenu
239230 HMENU hSubMenu = PInvoke . GetSubMenu ( hMenu , 0 ) ;
240- hr = pContextMenu2 . Get ( ) ->HandleMenuMsg ( PInvoke . WM_INITMENUPOPUP , ( WPARAM ) ( nuint ) hSubMenu . Value , 0 ) ;
241- if ( hr . ThrowIfFailedOnDebug ( ) . Failed ) return null ;
231+ hr = pContextMenu2 . Get ( ) ->HandleMenuMsg ( PInvoke . WM_INITMENUPOPUP , ( WPARAM ) ( nuint ) hSubMenu . Value , 0 ) . ThrowOnFailure ( ) ;
242232
243233 uint dwCount = unchecked ( ( uint ) PInvoke . GetMenuItemCount ( hSubMenu ) ) ;
244- if ( dwCount is unchecked ( ( uint ) - 1 ) ) return null ;
234+ if ( dwCount is unchecked ( ( uint ) - 1 ) ) throw new Win32Exception ( Marshal . GetLastWin32Error ( ) ) ;
245235
246236 // Enumerates the menu items
247237 List < WindowsContextMenuItem > items = [ ] ;
@@ -262,6 +252,10 @@ public static HRESULT TryUnpinFolderFromQuickAccess(this IWindowsFolder @this)
262252
263253 items . Add ( new ( mii . wID , new ( mii . dwTypeData ) , rawImageData , ( WindowsContextMenuType ) mii . fType , ( WindowsContextMenuState ) mii . fState ) ) ;
264254 }
255+ else
256+ {
257+ throw new Win32Exception ( Marshal . GetLastWin32Error ( ) ) ;
258+ }
265259
266260 NativeMemory . Free ( mii . dwTypeData ) ;
267261 }
@@ -309,43 +303,30 @@ public static bool InvokeShellNewItem(this IWindowsFolder @this, WindowsContextM
309303 using ComHeapPtr < ITEMIDLIST > pThisAbsolutePidl = default ;
310304 ComHeapPtr < ITEMIDLIST > pThisRelativePidl = default ;
311305
312- hr = PInvoke . CoCreateInstance ( CLSID . CLSID_OpenWithMenu , null , CLSCTX . CLSCTX_INPROC_SERVER , IID . IID_IContextMenu , ( void * * ) pOpenWithContextMenu . GetAddressOf ( ) ) ;
313- if ( hr . ThrowIfFailedOnDebug ( ) . Failed ) return null ;
314-
315- hr = pOpenWithContextMenu . Get ( ) ->QueryInterface ( IID . IID_IShellExtInit , ( void * * ) pShellExtInit . GetAddressOf ( ) ) ;
316- if ( hr . ThrowIfFailedOnDebug ( ) . Failed ) return null ;
317-
318- hr = pOpenWithContextMenu . Get ( ) ->QueryInterface ( IID . IID_IContextMenu2 , ( void * * ) pOpenWithContextMenu2 . GetAddressOf ( ) ) ;
319- if ( hr . ThrowIfFailedOnDebug ( ) . Failed ) return null ;
306+ hr = PInvoke . CoCreateInstance ( CLSID . CLSID_OpenWithMenu , null , CLSCTX . CLSCTX_INPROC_SERVER , IID . IID_IContextMenu , ( void * * ) pOpenWithContextMenu . GetAddressOf ( ) ) . ThrowOnFailure ( ) ;
307+ hr = pOpenWithContextMenu . Get ( ) ->QueryInterface ( IID . IID_IShellExtInit , ( void * * ) pShellExtInit . GetAddressOf ( ) ) . ThrowOnFailure ( ) ;
308+ hr = pOpenWithContextMenu . Get ( ) ->QueryInterface ( IID . IID_IContextMenu2 , ( void * * ) pOpenWithContextMenu2 . GetAddressOf ( ) ) . ThrowOnFailure ( ) ;
320309
321310 // Get the absolute PIDL of the parent folder
322311 @this . ThisPtr ->GetParent ( pParentFolderShellItem . GetAddressOf ( ) ) ;
323- hr = PInvoke . SHGetIDListFromObject ( ( IUnknown * ) pParentFolderShellItem . Get ( ) , pParentAbsolutePidl . GetAddressOf ( ) ) ;
324- if ( hr . ThrowIfFailedOnDebug ( ) . Failed ) return null ;
312+ hr = PInvoke . SHGetIDListFromObject ( ( IUnknown * ) pParentFolderShellItem . Get ( ) , pParentAbsolutePidl . GetAddressOf ( ) ) . ThrowOnFailure ( ) ;
325313
326314 // Get the relative PIDL of the current item
327- hr = PInvoke . SHGetIDListFromObject ( ( IUnknown * ) @this . ThisPtr , pThisAbsolutePidl . GetAddressOf ( ) ) ;
328- if ( hr . ThrowIfFailedOnDebug ( ) . Failed ) return null ;
315+ hr = PInvoke . SHGetIDListFromObject ( ( IUnknown * ) @this . ThisPtr , pThisAbsolutePidl . GetAddressOf ( ) ) . ThrowOnFailure ( ) ;
329316 pThisRelativePidl . Attach ( PInvoke . ILFindLastID ( pThisAbsolutePidl . Get ( ) ) ) ;
317+ hr = PInvoke . SHCreateDataObject ( pParentAbsolutePidl . Get ( ) , 1U , pThisRelativePidl . GetAddressOf ( ) , null , IID . IID_IDataObject , ( void * * ) pDataObject . GetAddressOf ( ) ) . ThrowOnFailure ( ) ;
318+ hr = pShellExtInit . Get ( ) ->Initialize ( null , pDataObject . Get ( ) , HKEY . Null ) . ThrowOnFailure ( ) ;
330319
331- hr = PInvoke . SHCreateDataObject ( pParentAbsolutePidl . Get ( ) , 1U , pThisRelativePidl . GetAddressOf ( ) , null , IID . IID_IDataObject , ( void * * ) pDataObject . GetAddressOf ( ) ) ;
332- if ( hr . ThrowIfFailedOnDebug ( ) . Failed ) return null ;
333-
334- hr = pShellExtInit . Get ( ) ->Initialize ( null , pDataObject . Get ( ) , HKEY . Null ) ;
335- if ( hr . ThrowIfFailedOnDebug ( ) . Failed ) return [ ] ;
336-
337- // Inserts "Open With(&H)" or "Open With(&H)..."
320+ // Inserts "Open With (&H)" or "Open With (&H)..."
338321 HMENU hMenu = PInvoke . CreatePopupMenu ( ) ;
339- hr = pOpenWithContextMenu . Get ( ) ->QueryContextMenu ( hMenu , 0 , 1 , 256 , 0 ) ;
340- if ( hr . ThrowIfFailedOnDebug ( ) . Failed ) return null ;
322+ hr = pOpenWithContextMenu . Get ( ) ->QueryContextMenu ( hMenu , 0 , 1 , 256 , 0 ) . ThrowOnFailure ( ) ;
341323
342324 // Populates the hSubMenu
343325 HMENU hSubMenu = PInvoke . GetSubMenu ( hMenu , 0 ) ;
344- hr = pOpenWithContextMenu2 . Get ( ) ->HandleMenuMsg ( PInvoke . WM_INITMENUPOPUP , ( WPARAM ) ( nuint ) hSubMenu . Value , 0 ) ;
345- if ( hr . ThrowIfFailedOnDebug ( ) . Failed ) return null ;
326+ hr = pOpenWithContextMenu2 . Get ( ) ->HandleMenuMsg ( PInvoke . WM_INITMENUPOPUP , ( WPARAM ) ( nuint ) hSubMenu . Value , 0 ) . ThrowOnFailure ( ) ;
346327
347328 uint dwCount = unchecked ( ( uint ) PInvoke . GetMenuItemCount ( hSubMenu ) ) ;
348- if ( dwCount is unchecked ( ( uint ) - 1 ) ) return null ;
329+ if ( dwCount is unchecked ( ( uint ) - 1 ) ) throw new Win32Exception ( Marshal . GetLastWin32Error ( ) ) ;
349330
350331 // Enumerates the menu items
351332 List < WindowsContextMenuItem > items = [ ] ;
@@ -366,6 +347,10 @@ public static bool InvokeShellNewItem(this IWindowsFolder @this, WindowsContextM
366347
367348 items . Add ( new ( mii . wID , new ( mii . dwTypeData ) , rawImageData , ( WindowsContextMenuType ) mii . fType , ( WindowsContextMenuState ) mii . fState ) ) ;
368349 }
350+ else
351+ {
352+ throw new Win32Exception ( Marshal . GetLastWin32Error ( ) ) ;
353+ }
369354
370355 NativeMemory . Free ( mii . dwTypeData ) ;
371356 }
0 commit comments