views:

277

answers:

7

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...

A: 

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.

jeffamaphone
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"....
A: 

Isn't this what WNetAddConnection and WNetAddConnection2 are for?

No, they're for adding new connections (instead of repairing an existing one).
A: 

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
A: 

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
+1  A: 
NET USE V: /DELETE
NET USE V: "\\server1\videos"

NET USE L: /DELETE
NET USE L: "\\server2\archive"
Mitchell Skurnik
A: 

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
A: 

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