Results 1 to 10 of 10

Thread: How to reconnect of disconnected network drive?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    679

    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?

  2. #2
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,191

    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

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    679

    Re: How to reconnect of disconnected network drive?

    Quote Originally Posted by topshot View Post
    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.

  4. #4
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,191

    Re: How to reconnect of disconnected network drive?

    Windows API calls. For example

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    679

    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.

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    679

    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.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    679

    Re: How to reconnect of disconnected network drive?

    Quote Originally Posted by Tech99 View Post
    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.

  9. #9
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,191

    Re: How to reconnect of disconnected network drive?

    Did you ever find a solution to your issue?

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    679

    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
  •  



Click Here to Expand Forum to Full Width