@@ -65,13 +65,17 @@ public unsafe bool Open_FileOpenDialog(nint hWnd, bool pickFoldersOnly, string[]
65
65
pDialog . Get ( ) ->SetDefaultFolder ( pDefaultFolderShellItem . Get ( ) ) ;
66
66
67
67
// Show the dialog
68
- pDialog . Get ( ) ->Show ( new HWND ( hWnd ) ) ;
68
+ hr = pDialog . Get ( ) ->Show ( new HWND ( hWnd ) ) ;
69
+ if ( hr . Value == unchecked ( ( int ) 0x800704C7 ) ) // HRESULT_FROM_WIN32(ERROR_CANCELLED)
70
+ return false ;
71
+
72
+ hr . ThrowOnFailure ( ) ;
69
73
70
74
// Get the file that user chose
71
75
using ComPtr < IShellItem > pResultShellItem = default ;
72
76
pDialog . Get ( ) ->GetResult ( pResultShellItem . GetAddressOf ( ) ) ;
73
- if ( pResultShellItem . Get ( ) == null )
74
- throw new COMException ( "FileSaveDialog returned invalid shell item." ) ;
77
+ if ( pResultShellItem . Get ( ) is null )
78
+ throw new COMException ( "FileOpenDialog returned invalid shell item." ) ;
75
79
pResultShellItem . Get ( ) ->GetDisplayName ( SIGDN . SIGDN_FILESYSPATH , out var lpFilePath ) ;
76
80
filePath = lpFilePath . ToString ( ) ;
77
81
@@ -135,12 +139,16 @@ public unsafe bool Open_FileSaveDialog(nint hWnd, bool pickFoldersOnly, string[]
135
139
pDialog . Get ( ) ->SetDefaultFolder ( pDefaultFolderShellItem . Get ( ) ) ;
136
140
137
141
// Show the dialog
138
- pDialog . Get ( ) ->Show ( new HWND ( hWnd ) ) ;
142
+ hr = pDialog . Get ( ) ->Show ( new HWND ( hWnd ) ) ;
143
+ if ( hr . Value == unchecked ( ( int ) 0x800704C7 ) ) // HRESULT_FROM_WIN32(ERROR_CANCELLED)
144
+ return false ;
145
+
146
+ hr . ThrowOnFailure ( ) ;
139
147
140
148
// Get the file that user chose
141
149
using ComPtr < IShellItem > pResultShellItem = default ;
142
150
pDialog . Get ( ) ->GetResult ( pResultShellItem . GetAddressOf ( ) ) ;
143
- if ( pResultShellItem . Get ( ) == null )
151
+ if ( pResultShellItem . Get ( ) is null )
144
152
throw new COMException ( "FileSaveDialog returned invalid shell item." ) ;
145
153
pResultShellItem . Get ( ) ->GetDisplayName ( SIGDN . SIGDN_FILESYSPATH , out var lpFilePath ) ;
146
154
filePath = lpFilePath . ToString ( ) ;
0 commit comments