Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to reconnect of disconnected network drive?

Tech99

New member
Joined
Apr 8, 2015
Messages
672
Task is to determine if there is certain file in network share, but FileExists testing, does not work when share is disconnected by OS.

Any idea how to reconnect disconnected (marked with red x in explorer window) network share?

In shellExecuteEx api function, there is possibility to automatically reconnect disconnected share, filling ShellExecuteInfoA fMask parameter with
CONNECTNETDRV (0x00000080) value, but are there method to establish disconnected share without launching/starting unneccesary process?
 

topshot

New member
Joined
Dec 17, 2014
Messages
1,191
I don't think this will work if you right-click the drive and Disconnect if that is what you mean by "disconnected by OS". It should if network cable is disconnected or remote PC is down since that is what I was testing for. If that is what you need you should be able to adapt it to your needs.

Code:
    ''' <summary>
    ''' Check that each folder exists. If so, verify FileSystemWatcher is running. If it isn't, try to start it.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub checkNetwork()
        For i = 0 To tempFolders.Length - 1
            If Directory.Exists(tempFolders(i).Trim) Then
                Dim tempFSW As FSW = TesterWatchers(i) 'this is a List of FileSystemWatchers that watch the folders in tempFolders()
                If tempFSW.IsRunning Then
                    'everything should be OK
                Else
                    startWatcher(tempFSW, tempFolders(i))
                End If
            Else
                LogErr("Folder " & tempFolders(i).Trim & " does not exist.", True, LogLevel.ERR)
            End If
        Next

    End Sub
 

Tech99

New member
Joined
Apr 8, 2015
Messages
672
I don't think this will work if you right-click the drive and Disconnect if that is what you mean by "disconnected by OS".

Yes - no can do with that code. :)

Application 'already knows', that network connection is disconnected. Looking for method to reconnect disconnected share.
 

Tech99

New member
Joined
Apr 8, 2015
Messages
672
Not with that code either, as drive is already mapped, but disconnected by OS.

Question still remains, how to reconnect disconnected share.

What explorer.exe does, when there is red x over share and user clicks that icon.

net config server /autodisconnect:-1
\\net use X: \\servername\folder /persistent:yes

Does not help either, shares still disconnect.
 
Last edited:

techgnome

PowerPoster
Joined
May 15, 2002
Messages
32,778
Seems to me I read an article about this some time back ... the OS caches the connection and puts it to sleep to preserve resources. Or some junk like that. You "wake it up" by trying to use the resource, when Windows sees that it is disconnected, it will then attempt to connect to it and use it. Here's the kicker... I'm not aware of any way of programatically to say "wake this connection up and connect it" ... I think the only way to do so is to attempt to use it, catch the exception when it fails, which it will likely do on the first attempt... pause, then try again and see if the connection goes through a second time. if I remember right that's what the recommendation was at the time... but that was some time ago and I try to avoid using mapped network resources so I didn't squirel away that info well, so it's possible I am mis-remembering things.

-tg
 

Tech99

New member
Joined
Apr 8, 2015
Messages
672
techgnome... Connection does not seems to wake up, by trying to use disconnected drive - even when tried via error handling multiple times - tried that, but no success to reconnect share that way.

.. been reading MSDN, found that there are WNetRestoreConnectionW api call.

https://docs.microsoft.com/en-us/windows/desktop/api/winnetwk/nf-winnetwk-wnetrestoreconnectionw

Remarks

The WNetRestoreConnectionW function is not supported on Windows Vista and later.

To call this function, first call the LoadLibrary function to load Mpr.dll. Then call the GetProcAddress function to retrieve the address of the WNetRestoreConnectionW function.

WNetRestoreConnectionW is used by Winlogon to restore all persistent drive mappings during the interactive logon process. The function is also called by the Microsoft Windows Shell to reconnect individual drives at the user's request. This can occur, for example, when a drive fails to reconnect at logon and the user double-clicks the drive under the My Computer virtual folder.

but as the Remark says, 'not supported on Windows Vista and later'. Does this mean, that function is unusable and removed from newer OS's?

Edit: Yes it does...
Run-time error '453': Can't find entry point WNetRestoreConnectionW in mpr.dll

So 'dead end' with WNetRestoreConnectionW api, in Vista and newer OS.

Looked mpr.dll (from W7, W10 and WS2016 boxes) there are;
WNetRestoreAllConnectionsW and WNetRestoreSingleConnectionW api functions, but no MSDN description for these, so these are officially undocumented ones?

Edit: Documented or not, tried WNetRestoreSingleConnectionW, seems to work.

Code:
Option Explicit

'Private Declare Function WNetRestoreConnectionW Lib "mpr.dll" (ByVal hwndParent As Long, ByVal lpDevice As Long) As Long
Private Declare Function WNetRestoreSingleConnectionW Lib "mpr.dll" (ByVal hwndParent As Long, ByVal lpDevice As Long, ByVal bUseUI As Long) As Long
'

Private Sub Command1_Click()
Debug.Print WNetRestoreShare("P:")
End Sub

Public Function WNetRestoreShare(ByVal lpDevice As String) As Long
    'WNetRestoreShare = WNetRestoreConnectionW(ByVal 0&, StrPtr(lpDevice)) 'Can't use in Vista and above -> Run-time error '453': Can't find entry point WNetRestoreConnectionW in mpr.dll
    WNetRestoreShare = WNetRestoreSingleConnectionW(ByVal 0&, StrPtr(lpDevice), Byval 0&)
End Function
 
Last edited:

Tech99

New member
Joined
Apr 8, 2015
Messages
672
Edit: Documented or not, tried WNetRestoreSingleConnectionW, seems to work.

Bollocks... Have to take my earlier comment back, as after extensive testing. WNetRestoreSingleConnectionW does not work, return value is ERROR_ALREADY_ASSIGNED and yet still share is disconnected.
 

Tech99

New member
Joined
Apr 8, 2015
Messages
672
No - i did not, had to move this to the todo list and progress with other tasks.

My todo list, with this so far is;
- test, if deleting share, before using WNetRestoreSingleConnectionW works.
 
Top