-
Feb 26th, 2019, 03:09 PM
#1
Thread Starter
Fanatic Member
How to reconnect of disconnected network drive?
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?
-
Feb 27th, 2019, 12:23 PM
#2
Re: How to reconnect of disconnected network drive?
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
-
Feb 27th, 2019, 03:13 PM
#3
Thread Starter
Fanatic Member
Re: How to reconnect of disconnected network drive?
Originally Posted by topshot
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.
-
Feb 27th, 2019, 04:54 PM
#4
Re: How to reconnect of disconnected network drive?
Windows API calls. For example
-
Feb 28th, 2019, 11:50 AM
#5
Thread Starter
Fanatic Member
Re: How to reconnect of disconnected network drive?
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 by Tech99; Feb 28th, 2019 at 12:05 PM.
-
Feb 28th, 2019, 12:26 PM
#6
Re: How to reconnect of disconnected network drive?
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
-
Feb 28th, 2019, 02:05 PM
#7
Thread Starter
Fanatic Member
Re: How to reconnect of disconnected network drive?
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/win...oreconnectionw
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 by Tech99; Feb 28th, 2019 at 04:34 PM.
-
Mar 4th, 2019, 06:33 PM
#8
Thread Starter
Fanatic Member
Re: How to reconnect of disconnected network drive?
Originally Posted by Tech99
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.
-
Aug 13th, 2019, 03:31 PM
#9
Re: How to reconnect of disconnected network drive?
Did you ever find a solution to your issue?
-
Aug 14th, 2019, 03:41 AM
#10
Thread Starter
Fanatic Member
Re: How to reconnect of disconnected network drive?
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|