5

Sometimes a network drive that is already mapped to a drive letter because "disconnected". Using the normal Windows functions to access files / folders on that drive fail. As soon as the user manually clicks on that drive it the Windows Explorer dialog, it's magically repaired.

Since my program is a batch program I'd like to start this "magic" from my program (C++) but I haven't found a Windows function for that. There's nothing in the usual WNet... functions...

Michael Hutter
  • 1,064
  • 13
  • 33
user178379
  • 240
  • 2
  • 11
  • 1
    You should probably consider accessing the resource via a UNC path as opposed to the mapped drive. \\machine\share\something\somefile.ext vs. M:\something\somefile.ext – CMB Sep 24 '09 at 21:46
  • My customers choose the path. So it's not up to me to decide... – user178379 Sep 25 '09 at 10:42

8 Answers8

1
NET USE V: /DELETE
NET USE V: "\\server1\videos"

NET USE L: /DELETE
NET USE L: "\\server2\archive"
Mitchell Skurnik
  • 1,419
  • 4
  • 25
  • 37
1

When the path is inserted, you could check to see if it is a network resource and before opening files, use WNetGetConnection() to get the network resource.

You could also try to use WNetRestoreConnectionW(), which seems to have more spurious support, depending on the environment.

Kris Kumler
  • 6,307
  • 3
  • 24
  • 27
0

Try re-connecting to the share via net use:

net use \\server\folder [/user:[domain\]username] [password]

If that doesn't work, you can net use /delete it first, then re-connect.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • That should work. Unless the user had associated a password with the old connection. Since my app doesn't know that, it would effectively delete the password. But it seems that there is no official way of "repairing".... – user178379 Sep 26 '09 at 10:12
0

Isn't this what WNetAddConnection and WNetAddConnection2 are for?

0

I suspect that is really the same thing, though. Explorer probably caches the connection info somewhere in the registry. When the user tries to go to that drive Explorer sees that the mapping is disconnected, reads the connection info from the registry, and re-creates the connection. Maybe you could try running regmon while you create a drive mapping and see if you can figure out where and how the connection information is cached.

Luke
  • 11,211
  • 2
  • 27
  • 38
0

I had trouble with this at a client of mine not long ago. I don't know if it's possible in your situation, but our fix was to tweak the Server's network settings to stop the timeouts and disconnects. See MSKB 297684 for details.

ewall
  • 27,179
  • 15
  • 70
  • 84
0

I agree with the comment from CMB, above. I've been down this path (excuse the pun) in the past and it caused me no end of trouble.

If the path is user configurable, they could use m:\pathonserver or they could use \server\c\pathonserver.

It shouldn't make any difference to your code, opening a file as m:\blahdeblah.dat or \server\c\blahdeblah.dat will be identical.

Using the UNC path is far more reliable, Windows will reconnect to that path automatically whether or not the mapped letter is there.

Blair Mahaffy
  • 255
  • 3
  • 10
0

If you map a drive to a network share, the mapped drive may be disconnected after a regular interval of inactivity, and Windows Explorer may display a red "X" on the icon of the mapped drive. However, if you try to access or browse the mapped drive, it reconnects quickly.

To avoid this behavior use the following command:
net config server /autodisconnect:-1

Explanation of Microsoft on this topic:
https://support.microsoft.com/da-dk/help/297684/mapped-drive-connection-to-network-share-may-be-lost

Michael Hutter
  • 1,064
  • 13
  • 33