Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

API function to detect for Disconnected Network Drives

517 views
Skip to first unread message

Dok

unread,
Nov 14, 2005, 12:47:05 AM11/14/05
to
Hi,

I was wondering if anybody new of a function to detect if a mapped network
drive is connectted or not, like how windows displays 'Disconnected Network
Drive'. I have tried the function 'WNetGetConnection', on a disconnected
drive and it returns NO_ERROR, insead of the error I expected
'ERROR_CONNECTION_UNAVAIL'.

Every time my application starts it scans the drives and when it hits a
network drive that is currently disconneted I get a pause which I would like
to avoid.

Thanks,
Adam

Kellie Fitton

unread,
Nov 14, 2005, 1:24:48 AM11/14/05
to

Pavel A.

unread,
Nov 14, 2005, 8:41:42 AM11/14/05
to
"Kellie Fitton" wrote:
> Hi,
>
> You can use the following API's to test a hardDrive:
>
> IsNetDrive()
> GetDriveType()

I've tried all these APIs on my XP SP1 - all they fail to report
disconnected state :(
I've mapped a network drive (for example Q:) and waited until net use reports
it as disconnected. After running the test program I did net use again to
verify that
the drive still is disconnected.

GetDriveTypeW(L"Q:\\") returns 4 = DRIVE_REMOTE
IsNetDrive('Q' - 'A') returns 1 = "network drive that is properly connected"
WNetGetConnectionW returns 0 (success) and correct remote path; extended
error = 0.

If this matters, Q: was mapped to a normal share (non-DFS).

Regards,
--PA

Arkady Frenkel

unread,
Nov 14, 2005, 9:21:11 AM11/14/05
to
Pavel , are you still with SP1 ? that absolutely unsecured OS :)
Arkady

"Pavel A." <pav...@NOwritemeNO.com> wrote in message
news:8CEDFFC5-6EE6-4E78...@microsoft.com...

Kellie Fitton

unread,
Nov 14, 2005, 10:45:39 PM11/14/05
to
Hi Pavel,

What happens when you use the API CreateFile() to obtain a handle to
the
mapped netWork drive (Q:\) ?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/createfile.asp

Kellie.

Eugene Gershnik

unread,
Nov 15, 2005, 2:49:37 PM11/15/05
to
Pavel A. wrote:
> I've tried all these APIs on my XP SP1 - all they fail to report
> disconnected state :(
> I've mapped a network drive (for example Q:) and waited until net use
> reports
> it as disconnected. After running the test program I did net use
> again to
> verify that
> the drive still is disconnected.


Try NetUseGetInfo with level 1, then look at the ui1_status field. In
general net.cmd uses Net API so if you wnat to replicate its behavior this
is the place to look first.


--
Eugene
http://www.gershnik.com


Pavel A.

unread,
Nov 16, 2005, 7:26:07 AM11/16/05
to
"Eugene Gershnik" wrote:
> Try NetUseGetInfo with level 1, then look at the ui1_status field.

Well Eugene... laugh at me but I couldn't make NetUseGetInfo to work at all.
What I tried:

#include <lm.h>

NET_API_STATUS ns = NetUseGetInfo(
NULL, //LMSTR UncServerName: reserved, must be NULL
name, //LMSTR UseName, see below
1, //DWORD Level,
(LPBYTE *)&ui1_struct
);

where name was:
1. "Q:"
2. "Q:\\"
3. L"Q:"
4. L"Q:\\"

In all these cases NetUseGetInfo returned 2250 (This network connection does
not exist.) and ui1_struct pointer = NULL.

Do you have an example on your site?

Regards,
--PA

Eugene Gershnik

unread,
Nov 16, 2005, 10:21:57 AM11/16/05
to
Pavel A. wrote:
> "Eugene Gershnik" wrote:
>> Try NetUseGetInfo with level 1, then look at the ui1_status field.
>
> Well Eugene... laugh at me but I couldn't make NetUseGetInfo to work
> at all. What I tried:

[...]

> where name was:
> 1. "Q:"
> 2. "Q:\\"
> 3. L"Q:"
> 4. L"Q:\\"
>
> In all these cases NetUseGetInfo returned 2250 (This network
> connection does not exist.) and ui1_struct pointer = NULL.

I don't have access to any sample code right now but I would guess that the
name should be the remote name (L"\\server\share") rather than the local
one. To map local names to the remote ones use NetUseEnum.

> Do you have an example on your site?

Unfortunately not. I have a long backlog of things I want to put there but
too little spare time to do something about it :-(


--
Eugene
http://www.gershnik.com


Pavel A.

unread,
Nov 16, 2005, 11:50:56 AM11/16/05
to
"Eugene Gershnik" wrote:
> I don't have access to any sample code right now but I would guess that the
> name should be the remote name (L"\\server\share") rather than the local
> one. To map local names to the remote ones use NetUseEnum.

My wrong... this time Q: was a DFS share.
NetUseGetInfo seems to work only with normal (non-DFS) shares - at least on
my machine (XP SP1 in a domain).
The UseName argument should be like L"Q:".
Then NetUseGetInfo returns correct share name; the status and other data
also look reasonably.
But when Q: is any other type of connection: DFS share or "pseudo-network"
shares created by some software - the call fails and no data is returned.

Command NET USE shows all mappings correctly.
So, this disapproves the statement that NET uses Net... API internally,
or that Net... API give same result as NET comand.
Maybe it was so on earlier Windows versions, but not on XP.

>> Do you have an example on your site?
>
>Unfortunately not. I have a long backlog of things I want to put there but
>too little spare time to do something about it :-(

You write there that the site is under redesign - so i thought I didn't find
the samples...

Regards,
--PA

Eugene Gershnik

unread,
Nov 16, 2005, 2:26:24 PM11/16/05
to
Pavel A. wrote:
> But when Q: is any other type of connection: DFS share or
> "pseudo-network" shares created by some software - the call fails and
> no data is returned.

Only the connections returned by NetUseEnum can be queried by NetUseGetInfo.
For DFS ones you will need to use some DFS specific API. I assume that
NetDfsGetInfo may be what you are looking for.

> Command NET USE shows all mappings correctly.
> So, this disapproves the statement that NET uses Net... API
> internally,
> or that Net... API give same result as NET comand.
> Maybe it was so on earlier Windows versions, but not on XP.

It does use Net API for the normal CIFS connections. As governer of
California likes to say "trust me" ;-) (I am looking at the net.exe source
code now).
The NET USE command first enumerates all resources using WNetEnumResource.
Those that are "Lanman" ones are then further queried by Net API which gives
the status and other things.


--
Eugene
http://www.gershnik.com


Dok

unread,
Nov 16, 2005, 8:21:03 PM11/16/05
to

"Eugene Gershnik" wrote:
> Try NetUseGetInfo with level 1, then look at the ui1_status field. In
> general net.cmd uses Net API so if you wnat to replicate its behavior this
> is the place to look first.


Thakyou, 'NetUseGetInfo' does what I needed.

Quick question, does anybody know why 'GetDriveType' and 'WNetGetConnection'
doesn't say that a network drive is disconnected when it is?

Thanks,
Adam

Arkady Frenkel

unread,
Nov 17, 2005, 2:12:36 AM11/17/05
to
Why you need to look at the source, enough to open it in depends.exe :)
Arkady

"Eugene Gershnik" <gers...@hotmail.com> wrote in message
news:eHnxiOu6...@TK2MSFTNGP14.phx.gbl...

Pavel A.

unread,
Nov 17, 2005, 1:33:14 PM11/17/05
to
Well... May the source be with you...

--PA

0 new messages