Visual Basic Network Services
IsNetDrive: Determining the Connection State of a Mapped Drive
Posted:   Friday December 09, 2005
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows 2000
OS restrictions:   Windows 2000 or later
Author:   VBnet - Randy Birch


IsNetDrive: Determining the Connection State of a Mapped Drive
InternetGetConnectedState: Determine Network Connection Type
IsNetworkAlive: Determine Network Connection State
IsDestinationReachable: Determine Network QOC Info
gethostbyaddr: Obtain Host Name from IP Address
IcmpSendEcho: Ping a Machine by IP Address
IcmpSendEcho: Ping a Machine by Host Name
Windows 2000 or greater.

If you are using Windows 2000 or later, IsNetDrive can be used to determine whether a particular drive is mapped to a share and the connection state.  The value passed to IsNetDrive is not the drive letter to check, but rather an integer that indicates which drive letter you want to test, i.e. 0 for A:, 1 for B:, 2 for C:, and so on.

IsNetDrive returns 0 if the drive specified is not a network drive, 1 if the specified drive is a network drive that is properly connected to the network, or 2 if the specified drive is a network drive that is disconnected or in an error state.

In testing, I've determined that the state returned by this API reflects the success or failure of Windows to reconnect to shares when Windows boots up (session logon), or reflects the state of new mapped  or disconnected drives during the session. It can not be used as a test to ensure that a successful connection to a particular drive exists at a specific point in time (is still valid).  If all drives were successfully reconnected at boot then calls to this API return the state of the drives at logon. Drives that were mapped or disconnected by the user during the session are properly reflected as a change in state (e.g. from 1 or 2 to 0, or from 0 to 1). In other words, if you have a successful map and then turn off the server or network card, Windows still shows the connection state as 1 (properly connected).  While this may limit the use of this API as a test to ensure that a connection is valid before reading from or writing to a mapped share, the return value of 1 at least indicates that the share was valid at boot and correspondingly, a value of 2 indicates that Windows was unable to reattach the share and has been unsuccessful since.

The illustration shows that my mapped drives P: and Z: were successfully restored to the server when I logged on (values=1), while drive X was not successfully reconnected (value=2) as that server was off.

 BAS Module Code

 Form Code
To a form add a command button (Command1), and a list box for the output (List1) with the following code:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Declare Function IsNetDrive Lib "shell32" _
  (ByVal iDrive As Long) As Long

Private Sub Form_Load()

   Command1.Caption = "IsNetDrive"
End Sub

Private Sub Command1_Click()

   Dim cnt As Long
   For cnt = 0 To 25
      List1.AddItem Chr$(65 + cnt) & ":" & vbTab & IsNetDrive(cnt)
End Sub
IsNetDrive is one of the internal APIs that Microsoft was required to publish as part of its settlement with the U.S. Department of Justice. In its entry, Microsoft notes that while IsNetDrive is available in Microsoft Windows 2000 and Windows XP, it may be altered or unavailable in subsequent Windows versions. Microsoft's preferred alternatives are a combination of GetDriveType and WNetGetConnection.


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter