Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid 12 seconds delay when disonnecting from share in Windows 7?

I'm making some utils that uses NetUseAdd and NetUseDel functions to connect/disconnect to share. In Windows 7 I noticed that between calling NetUseDel and real disconnnection passes nearly 12 seconds. I made some investigations and found that net use \server /del also disonnects only after 12 seconds. Here's little script and Wireshark output, corresponding to run of script:

net use \\server "" /user:""
net use \\server /delete

http://i.stack.imgur.com/5CyCw.png

Setting last tree connect smb command as reference, we can see, that tree disconnect delayed for 12 seconds.

Does anyone know how to cut such a timeout?

like image 689
kizill Avatar asked Nov 05 '22 18:11

kizill


1 Answers

The connection can stay alive sometimes even more than 12 seconds. The trick is to force a bad login after deletion.

If you force a bad login then the share will be inaccessible immediately. We can use the local Guest account for that (it will usually give the error "Logon failure: account currently disabled." and even if it is enabled it will not have access).

Instead of just:

net use \\server /delete

We will execute:

net use \\server /delete
net use \\server "" /user:"Guest"
net use \\server /delete 2>nul

The third line is executed just in case the Guest login succedes (it usses 2>nul for error stream redirection to the nul device, to avoid displaying any error messages).

This is the batch version that uses the "net use" command, but the same solution can be applied when using NetUseAdd and NetUseDel from netapi32.dll, or WNetAddConnection and WNetCancelConnection from mpr.dll.

like image 79
Cosmin Rus Avatar answered Nov 15 '22 05:11

Cosmin Rus