Why network shares enumeration is so slow on Windows?
Retrieving a list of network shares via Windows API functions NetShareEnum or WNetEnumResource takes up to 60 (!!!) seconds to complete a reqest. Maybe someone knows why it's SO slow? Maybe i开发者_运维技巧t's possible to use some API / library (like SMB) to enum shares at sane speed?
As for why it is so slow, if a host is unavailable or not responding for whatever reason (requiring login credentials, for instance), Windows will wait for the timeout limit to be exceeded, for every share known in the network (which can be multiple per host). Windows' default timeouts are insanely long.
Reducing the timeout to something like 50ms (which should never be exceeded in a LAN) might help here, ultimately it depends on the amount of hosts tested.
I cannot help concerning alternatives towards the API, sorry.
There is something you might want to try, which is to enumerate the same shares with Linux (you can use a LiveCD). In my test, I've seen that the router also gets asked for possible network shares (my router runs Linux and it was what my ISP gave me, so it might be common).
So, maybe Windows is asking the router for network shares for a long time (which shouldn't happen...) and waiting synchronously for it to respond with shares.
(Who was the genious that designed this API to be synchronous?)
精彩评论