77using Vanara . Extensions ;
88using Windows . Win32 ;
99using Windows . Win32 . Foundation ;
10+ using Windows . Win32 . NetworkManagement . WNet ;
1011using Windows . Win32 . System . Com ;
1112using Windows . Win32 . UI . Shell ;
1213using Windows . Win32 . UI . Shell . Common ;
@@ -179,43 +180,46 @@ public bool Open_NetworkConnectionDialog(nint hWind, bool hideRestoreConnectionC
179180
180181 private sealed class NetworkConnectionDialog : CommonDialog
181182 {
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 ;
184185
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>
188187 public NetworkConnectionDialog ( )
189188 {
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 ;
192191 }
193192
194193 /// <summary>Gets the connected device number. This value is only valid after successfully running the dialog.</summary>
195194 /// <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>
196195 [ Browsable ( false ) ]
197- public int ConnectedDeviceNumber
198- => dialogOptions . dwDevNum ;
196+ public int ConnectedDeviceNumber => ( int ) dialogOptions . dwDevNum ;
199197
200198 /// <summary>Gets or sets a value indicating whether to hide the check box allowing the user to restore the connection at logon.</summary>
201199 /// <value><c>true</c> if hiding restore connection check box; otherwise, <c>false</c>.</value>
202200 [ DefaultValue ( false ) , Category ( "Appearance" ) , Description ( "Hide the check box allowing the user to restore the connection at logon." ) ]
203201 public bool HideRestoreConnectionCheckBox
204202 {
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+ }
207211 }
208212
209213 /// <summary>Gets or sets a value indicating whether restore the connection at logon.</summary>
210214 /// <value><c>true</c> to restore connection at logon; otherwise, <c>false</c>.</value>
211215 [ DefaultValue ( false ) , Category ( "Behavior" ) , Description ( "Restore the connection at logon." ) ]
212216 public bool PersistConnectionAtLogon
213217 {
214- get => dialogOptions . dwFlags . IsFlagSet ( Vanara . PInvoke . Mpr . CONN_DLG . CONNDLG_PERSIST ) ;
218+ get => dialogOptions . dwFlags . IsFlagSet ( CONNECTDLGSTRUCT_FLAGS . CONNDLG_PERSIST ) ;
215219 set
216220 {
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 ) ;
219223 }
220224 }
221225
@@ -230,52 +234,64 @@ public bool PersistConnectionAtLogon
230234 /// <summary>Gets or sets the name of the remote network.</summary>
231235 /// <value>The name of the remote network.</value>
232236 [ 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+ }
234249
235250 /// <summary>Gets or sets a value indicating whether to enter the most recently used paths into the combination box.</summary>
236251 /// <value><c>true</c> to use MRU path; otherwise, <c>false</c>.</value>
237252 /// <exception cref="InvalidOperationException">UseMostRecentPath</exception>
238253 [ DefaultValue ( false ) , Category ( "Behavior" ) , Description ( "Enter the most recently used paths into the combination box." ) ]
239254 public bool UseMostRecentPath
240255 {
241- get => dialogOptions . dwFlags . IsFlagSet ( Vanara . PInvoke . Mpr . CONN_DLG . CONNDLG_USE_MRU ) ;
256+ get => dialogOptions . dwFlags . IsFlagSet ( CONNECTDLGSTRUCT_FLAGS . CONNDLG_USE_MRU ) ;
242257 set
243258 {
244259 if ( value && ! string . IsNullOrEmpty ( RemoteNetworkName ) )
245260 throw new InvalidOperationException ( $ "{ nameof ( UseMostRecentPath ) } cannot be set to true if { nameof ( RemoteNetworkName ) } has a value.") ;
246261
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 ) ;
248263 }
249264 }
250265
251266 /// <inheritdoc/>
252- public override void Reset ( )
267+ public unsafe override void Reset ( )
253268 {
254- dialogOptions . dwDevNum = - 1 ;
269+ dialogOptions . dwDevNum = unchecked ( ( uint ) - 1 ) ;
255270 dialogOptions . dwFlags = 0 ;
256- dialogOptions . lpConnRes = IntPtr . Zero ;
271+ dialogOptions . lpConnRes = null ;
257272 ReadOnlyPath = false ;
258273 }
259274
260275 /// <inheritdoc/>
261- protected override bool RunDialog ( IntPtr hwndOwner )
276+ protected unsafe override bool RunDialog ( IntPtr hwndOwner )
262277 {
263- using var lpNetResource = Vanara . InteropServices . SafeCoTaskMemHandle . CreateFromStructure ( netRes ) ;
278+ dialogOptions . hwndOwner = new ( hwndOwner ) ;
264279
265- dialogOptions . hwndOwner = hwndOwner ;
266- dialogOptions . lpConnRes = lpNetResource . DangerousGetHandle ( ) ;
280+ fixed ( NETRESOURCEW * lpConnRes = & netRes )
281+ dialogOptions . lpConnRes = lpConnRes ;
267282
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 ;
270285
271- var result = Vanara . PInvoke . Mpr . WNetConnectionDialog1 ( dialogOptions ) ;
286+ var result = PInvoke . WNetConnectionDialog1W ( ref dialogOptions ) ;
272287
273- dialogOptions . lpConnRes = IntPtr . Zero ;
288+ dialogOptions . lpConnRes = null ;
274289
275- if ( result == unchecked ( ( uint ) - 1 ) )
290+ if ( ( uint ) result == unchecked ( ( uint ) - 1 ) )
276291 return false ;
277292
278- result . ThrowIfFailed ( ) ;
293+ if ( result == 0 )
294+ throw new Win32Exception ( "Cannot display dialog" ) ;
279295
280296 return true ;
281297 }
0 commit comments