7
7
using Vanara . Extensions ;
8
8
using Windows . Win32 ;
9
9
using Windows . Win32 . Foundation ;
10
+ using Windows . Win32 . NetworkManagement . WNet ;
10
11
using Windows . Win32 . System . Com ;
11
12
using Windows . Win32 . UI . Shell ;
12
13
using Windows . Win32 . UI . Shell . Common ;
@@ -179,43 +180,46 @@ public bool Open_NetworkConnectionDialog(nint hWind, bool hideRestoreConnectionC
179
180
180
181
private sealed class NetworkConnectionDialog : CommonDialog
181
182
{
182
- private readonly Vanara . PInvoke . Mpr . NETRESOURCE netRes = new ( ) ;
183
- private Vanara . PInvoke . Mpr . CONNECTDLGSTRUCT dialogOptions ;
183
+ private NETRESOURCEW netRes = new ( ) ;
184
+ private CONNECTDLGSTRUCTW dialogOptions ;
184
185
185
- /// <summary>
186
- /// Initializes a new instance of the <see cref="NetworkConnectionDialog"/> class.
187
- /// </summary>
186
+ /// <summary>Initializes a new instance of the <see cref="NetworkConnectionDialog"/> class.</summary>
188
187
public NetworkConnectionDialog ( )
189
188
{
190
- dialogOptions . cbStructure = ( uint ) Marshal . SizeOf ( typeof ( Vanara . PInvoke . Mpr . CONNECTDLGSTRUCT ) ) ;
191
- netRes . dwType = Vanara . PInvoke . Mpr . NETRESOURCEType . RESOURCETYPE_DISK ;
189
+ dialogOptions . cbStructure = ( uint ) Marshal . SizeOf ( typeof ( CONNECTDLGSTRUCTW ) ) ;
190
+ netRes . dwType = NET_RESOURCE_TYPE . RESOURCETYPE_DISK ;
192
191
}
193
192
194
193
/// <summary>Gets the connected device number. This value is only valid after successfully running the dialog.</summary>
195
194
/// <value>The connected device number. The value is 1 for A:, 2 for B:, 3 for C:, and so on. If the user made a deviceless connection, the value is –1.</value>
196
195
[ Browsable ( false ) ]
197
- public int ConnectedDeviceNumber
198
- => dialogOptions . dwDevNum ;
196
+ public int ConnectedDeviceNumber => ( int ) dialogOptions . dwDevNum ;
199
197
200
198
/// <summary>Gets or sets a value indicating whether to hide the check box allowing the user to restore the connection at logon.</summary>
201
199
/// <value><c>true</c> if hiding restore connection check box; otherwise, <c>false</c>.</value>
202
200
[ DefaultValue ( false ) , Category ( "Appearance" ) , Description ( "Hide the check box allowing the user to restore the connection at logon." ) ]
203
201
public bool HideRestoreConnectionCheckBox
204
202
{
205
- get => dialogOptions . dwFlags . IsFlagSet ( Vanara . PInvoke . Mpr . CONN_DLG . CONNDLG_HIDE_BOX ) ;
206
- set => dialogOptions . dwFlags = dialogOptions . dwFlags . SetFlags ( Vanara . PInvoke . Mpr . CONN_DLG . CONNDLG_HIDE_BOX , value ) ;
203
+ get => dialogOptions . dwFlags . HasFlag ( CONNECTDLGSTRUCT_FLAGS . CONNDLG_HIDE_BOX ) ;
204
+ set
205
+ {
206
+ if ( value )
207
+ dialogOptions . dwFlags |= CONNECTDLGSTRUCT_FLAGS . CONNDLG_HIDE_BOX ;
208
+ else
209
+ dialogOptions . dwFlags &= ~ CONNECTDLGSTRUCT_FLAGS . CONNDLG_HIDE_BOX ;
210
+ }
207
211
}
208
212
209
213
/// <summary>Gets or sets a value indicating whether restore the connection at logon.</summary>
210
214
/// <value><c>true</c> to restore connection at logon; otherwise, <c>false</c>.</value>
211
215
[ DefaultValue ( false ) , Category ( "Behavior" ) , Description ( "Restore the connection at logon." ) ]
212
216
public bool PersistConnectionAtLogon
213
217
{
214
- get => dialogOptions . dwFlags . IsFlagSet ( Vanara . PInvoke . Mpr . CONN_DLG . CONNDLG_PERSIST ) ;
218
+ get => dialogOptions . dwFlags . IsFlagSet ( CONNECTDLGSTRUCT_FLAGS . CONNDLG_PERSIST ) ;
215
219
set
216
220
{
217
- dialogOptions . dwFlags = dialogOptions . dwFlags . SetFlags ( Vanara . PInvoke . Mpr . CONN_DLG . CONNDLG_PERSIST , value ) ;
218
- dialogOptions . dwFlags = dialogOptions . dwFlags . SetFlags ( Vanara . PInvoke . Mpr . CONN_DLG . CONNDLG_NOT_PERSIST , ! value ) ;
221
+ dialogOptions . dwFlags = dialogOptions . dwFlags . SetFlags ( CONNECTDLGSTRUCT_FLAGS . CONNDLG_PERSIST , value ) ;
222
+ dialogOptions . dwFlags = dialogOptions . dwFlags . SetFlags ( CONNECTDLGSTRUCT_FLAGS . CONNDLG_NOT_PERSIST , ! value ) ;
219
223
}
220
224
}
221
225
@@ -230,52 +234,64 @@ public bool PersistConnectionAtLogon
230
234
/// <summary>Gets or sets the name of the remote network.</summary>
231
235
/// <value>The name of the remote network.</value>
232
236
[ DefaultValue ( null ) , Category ( "Behavior" ) , Description ( "The value displayed in the path field." ) ]
233
- public string RemoteNetworkName { get => netRes . lpRemoteName ; set => netRes . lpRemoteName = value ; }
237
+ public string RemoteNetworkName
238
+ {
239
+ get => netRes . lpRemoteName . ToString ( ) ;
240
+ set
241
+ {
242
+ unsafe
243
+ {
244
+ fixed ( char * lpcRemoteName = value )
245
+ netRes . lpRemoteName = lpcRemoteName ;
246
+ }
247
+ }
248
+ }
234
249
235
250
/// <summary>Gets or sets a value indicating whether to enter the most recently used paths into the combination box.</summary>
236
251
/// <value><c>true</c> to use MRU path; otherwise, <c>false</c>.</value>
237
252
/// <exception cref="InvalidOperationException">UseMostRecentPath</exception>
238
253
[ DefaultValue ( false ) , Category ( "Behavior" ) , Description ( "Enter the most recently used paths into the combination box." ) ]
239
254
public bool UseMostRecentPath
240
255
{
241
- get => dialogOptions . dwFlags . IsFlagSet ( Vanara . PInvoke . Mpr . CONN_DLG . CONNDLG_USE_MRU ) ;
256
+ get => dialogOptions . dwFlags . IsFlagSet ( CONNECTDLGSTRUCT_FLAGS . CONNDLG_USE_MRU ) ;
242
257
set
243
258
{
244
259
if ( value && ! string . IsNullOrEmpty ( RemoteNetworkName ) )
245
260
throw new InvalidOperationException ( $ "{ nameof ( UseMostRecentPath ) } cannot be set to true if { nameof ( RemoteNetworkName ) } has a value.") ;
246
261
247
- dialogOptions . dwFlags = dialogOptions . dwFlags . SetFlags ( Vanara . PInvoke . Mpr . CONN_DLG . CONNDLG_USE_MRU , value ) ;
262
+ dialogOptions . dwFlags = dialogOptions . dwFlags . SetFlags ( CONNECTDLGSTRUCT_FLAGS . CONNDLG_USE_MRU , value ) ;
248
263
}
249
264
}
250
265
251
266
/// <inheritdoc/>
252
- public override void Reset ( )
267
+ public unsafe override void Reset ( )
253
268
{
254
- dialogOptions . dwDevNum = - 1 ;
269
+ dialogOptions . dwDevNum = unchecked ( ( uint ) - 1 ) ;
255
270
dialogOptions . dwFlags = 0 ;
256
- dialogOptions . lpConnRes = IntPtr . Zero ;
271
+ dialogOptions . lpConnRes = null ;
257
272
ReadOnlyPath = false ;
258
273
}
259
274
260
275
/// <inheritdoc/>
261
- protected override bool RunDialog ( IntPtr hwndOwner )
276
+ protected unsafe override bool RunDialog ( IntPtr hwndOwner )
262
277
{
263
- using var lpNetResource = Vanara . InteropServices . SafeCoTaskMemHandle . CreateFromStructure ( netRes ) ;
278
+ dialogOptions . hwndOwner = new ( hwndOwner ) ;
264
279
265
- dialogOptions . hwndOwner = hwndOwner ;
266
- dialogOptions . lpConnRes = lpNetResource . DangerousGetHandle ( ) ;
280
+ fixed ( NETRESOURCEW * lpConnRes = & netRes )
281
+ dialogOptions . lpConnRes = lpConnRes ;
267
282
268
- if ( ReadOnlyPath && ! string . IsNullOrEmpty ( netRes . lpRemoteName ) )
269
- dialogOptions . dwFlags |= Vanara . PInvoke . Mpr . CONN_DLG . CONNDLG_RO_PATH ;
283
+ if ( ReadOnlyPath && ! string . IsNullOrEmpty ( netRes . lpRemoteName . ToString ( ) ) )
284
+ dialogOptions . dwFlags |= CONNECTDLGSTRUCT_FLAGS . CONNDLG_RO_PATH ;
270
285
271
- var result = Vanara . PInvoke . Mpr . WNetConnectionDialog1 ( dialogOptions ) ;
286
+ var result = PInvoke . WNetConnectionDialog1W ( ref dialogOptions ) ;
272
287
273
- dialogOptions . lpConnRes = IntPtr . Zero ;
288
+ dialogOptions . lpConnRes = null ;
274
289
275
- if ( result == unchecked ( ( uint ) - 1 ) )
290
+ if ( ( uint ) result == unchecked ( ( uint ) - 1 ) )
276
291
return false ;
277
292
278
- result . ThrowIfFailed ( ) ;
293
+ if ( result == 0 )
294
+ throw new Win32Exception ( "Cannot display dialog" ) ;
279
295
280
296
return true ;
281
297
}
0 commit comments