Just paste this code into any class module and change the name of the module to cNetworkInterface.
' Module Name: cNetworkInterface
' Written By: C&D Programming Corp.
' Create Date: 2/98
' Copyright: Copyright 1998 by C&D Programming Corp. Source
' code may not be reproduced except for use in a
' compiled executable. All rights reserved. If
' you would like to reprint any or all of this
' code please email us at email@example.com
' Maximum length of a network path allowed in several of the
' networking API calls
Private Const MAX_PATH = 260
' Other Error Codes
Private Const WN_SUCCESS = 0&
Private Const ERROR_INVALID_PASSWORD = 86&
Private Const ERROR_NOT_ENOUGH_MEMORY = 8&
Private Const ERROR_INVALID_PARAMETER = 87&
Private Const ERROR_NO_MORE_ITEMS = 259&
Private Const ERROR_NOT_SUPPORTED = 50&
Private Const ERROR_REM_NOT_LIST = 51&
Private Const ERROR_DUP_NAME = 52&
Private Const ERROR_BAD_NETPATH = 53&
Private Const ERROR_NETWORK_BUSY = 54&
Private Const ERROR_DEV_NOT_EXIST = 55 ' dderror
Private Const ERROR_TOO_MANY_CMDS = 56&
Private Const ERROR_ADAP_HDW_ERR = 57&
Private Const ERROR_BAD_NET_RESP = 58&
Private Const ERROR_UNEXP_NET_ERR = 59&
Private Const ERROR_BAD_REM_ADAP = 60&
Private Const ERROR_PRINTQ_FULL = 61&
Private Const ERROR_NO_SPOOL_SPACE = 62&
Private Const ERROR_PRINT_CANCELLED = 63&
Private Const ERROR_NETNAME_DELETED = 64&
Private Const ERROR_NETWORK_ACCESS_DENIED = 65&
Private Const ERROR_BAD_DEV_TYPE = 66&
Private Const ERROR_BAD_NET_NAME = 67&
Private Const ERROR_TOO_MANY_NAMES = 68&
Private Const ERROR_TOO_MANY_SESS = 69&
Private Const ERROR_SHARING_PAUSED = 70&
Private Const ERROR_REQ_NOT_ACCEP = 71&
Private Const ERROR_REDIR_PAUSED = 72&
' Winnet32 Status Codes
Private Const ERROR_BAD_USERNAME = 2202&
Private Const ERROR_NOT_CONNECTED = 2250&
Private Const ERROR_OPEN_FILES = 2401&
Private Const ERROR_DEVICE_IN_USE = 2404&
Private Const ERROR_BAD_DEVICE = 1200&
Private Const ERROR_CONNECTION_UNAVAIL = 1201&
Private Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
Private Const ERROR_NO_NET_OR_BAD_PATH = 1203&
Private Const ERROR_BAD_PROVIDER = 1204&
Private Const ERROR_CANNOT_OPEN_PROFILE = 1205&
Private Const ERROR_BAD_PROFILE = 1206&
Private Const ERROR_NOT_CONTAINER = 1207&
Private Const ERROR_EXTENDED_ERROR = 1208&
Private Const ERROR_INVALID_GROUPNAME = 1209&
Private Const ERROR_INVALID_COMPUTERNAME = 1210&
Private Const ERROR_INVALID_EVENTNAME = 1211&
Private Const ERROR_INVALID_DOMAINNAME = 1212&
Private Const ERROR_INVALID_SERVICENAME = 1213&
Private Const ERROR_INVALID_NETNAME = 1214&
Private Const ERROR_INVALID_SHARENAME = 1215&
Private Const ERROR_INVALID_PASSWORDNAME = 1216&
Private Const ERROR_INVALID_MESSAGENAME = 1217&
Private Const ERROR_INVALID_MESSAGEDEST = 1218&
Private Const ERROR_SESSION_CREDENTIAL_CONFLICT = 1219&
Private Const ERROR_REMOTE_SESSION_LIMIT_EXCEEDED = 1220&
Private Const ERROR_DUP_DOMAINNAME = 1221&
Private Const ERROR_NO_NETWORK = 1222&
Private Const ERROR_CANCELLED = 1223
Private Const ERROR_USER_MAPPED_FILE = 1224
Private Const ERROR_CONNECTION_REFUSED = 1225
Private Const ERROR_GRACEFUL_DISCONNECT = 1226
Private Const ERROR_ADDRESS_ALREADY_ASSOCIATED = 1227
Private Const ERROR_ADDRESS_NOT_ASSOCIATED = 1228
Private Const ERROR_CONNECTION_INVALID = 1229
Private Const ERROR_CONNECTION_ACTIVE = 1230
Private Const ERROR_NETWORK_UNREACHABLE = 1231
Private Const ERROR_HOST_UNREACHABLE = 1232
Private Const ERROR_PROTOCOL_UNREACHABLE = 1233
Private Const ERROR_PORT_UNREACHABLE = 1234
Private Const ERROR_REQUEST_ABORTED = 1235
Private Const ERROR_CONNECTION_ABORTED = 1236
Private Const ERROR_RETRY = 1237
Private Const ERROR_CONNECTION_COUNT_LIMIT = 1238
Private Const ERROR_LOGIN_TIME_RESTRICTION = 1239
Private Const ERROR_LOGIN_WKSTA_RESTRICTION = 1240
Private Const ERROR_INCORRECT_ADDRESS = 1241
Private Const ERROR_ALREADY_REGISTERED = 1242
Private Const ERROR_SERVICE_NOT_FOUND = 1243
Private Const ERROR_NOT_AUTHENTICATED = 1244
Private Const ERROR_NOT_LOGGED_ON = 1245
Private Const ERROR_CONTINUE = 1246
Private Const ERROR_ALREADY_INITIALIZED = 1247
Private Const ERROR_NO_MORE_DEVICES = 1248
Private Const ERROR_NO_SUCH_SITE = 1249
Private Const ERROR_DOMAIN_CONTROLLER_EXISTS = 1250
Private Const ERROR_DS_NOT_INSTALLED = 1251
' Error retrieval function for the network library API
Private Declare Function WNetGetLastError Lib "mpr.dll" _
Alias "WNetGetLastErrorA" _
(lpError As Long, ByVal lpErrorBuf As String, _
ByVal nErrorBufSize As Long, ByVal lpNameBuf As String, _
ByVal nNameBufSize As Long) As Long
' Simple Network Resource connection and disconnection API calls.
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" _
(ByVal lpszNetPath As String, ByVal lpszPassword As String, _
ByVal lpszLocalName As String) As Long
Private Declare Function WNetCancelConnection Lib "mpr.dll" Alias "WNetCancelConnectionA" _
(ByVal lpszName As String, ByVal bForce As Long) As Long
Public Function ConnectResource(sLocalName As String, sRemoteName As String, _
sPassword As String)
Dim lReturnVal As Long
lReturnVal = WNetAddConnection(sRemoteName, sPassword & vbNullChar, _
sLocalName & vbNullChar)
If lReturnVal <> WN_SUCCESS Then
Err.Raise lReturnVal, "cNetworkInterface.ConnectResource", WNetErrorDescription(lReturnVal)
Public Function DisconnectResource(sLocalName As String, Optional bForce As Boolean = False)
Dim lReturnVal As Long
lReturnVal = WNetCancelConnection(sLocalName, bForce)
If lReturnVal <> WN_SUCCESS Then
Err.Raise lReturnVal, "cNetworkInterface.DisconnectResource", WNetErrorDescription(lReturnVal)
' When calling API functions, the strings returned often have a Null
' (or vbNullChar, which is the VB shortcut) character at the end of the
' string. This function removes the Null character.
Private Function RemoveNull(sData As String) As String
Dim iNullPos As Integer
iNullPos = InStr(sData, vbNullChar)
If iNullPos <> 0 Then
RemoveNull = Left(sData, iNullPos - 1)
RemoveNull = sData
Public Function WNetErrorDescription(lError As Long)
Dim sMsg As String
Select Case lError
' 32-Bit Networking Error Codes
sMsg = "An extended error has occurred. " & WNetExtendedError(lError)
sMsg = "The specified username is invalid."
sMsg = "This network connection does not exist."
sMsg = "This network connection has files open or requests pending."
sMsg = "The device is in use by an active process and cannot be disconnected."
sMsg = "The specified device name is invalid."
sMsg = "The device is not currently connected but it is a remembered connection."
sMsg = "An attempt was made to remember a device that had previously been remembered."
sMsg = "No network provider accepted the given network path."
sMsg = "The specified network provider name is invalid."
sMsg = "Unable to open the network connection profile."
sMsg = "The network connection profile is corrupt."
sMsg = "Cannot enumerate a non-container."
sMsg = "The format of the specified group name is invalid."
sMsg = "The format of the specified computer name is invalid."
sMsg = "The format of the specified event name is invalid."
sMsg = "The format of the specified domain name is invalid."
sMsg = "The format of the specified service name is invalid."
sMsg = "The format of the specified network name is invalid."
sMsg = "The format of the specified share name is invalid."
sMsg = "The format of the specified password is invalid."
sMsg = "The format of the specified message name is invalid."
sMsg = "The format of the specified message destination is invalid."
sMsg = "The credentials supplied conflict with an existing set of credentials."
sMsg = "An attempt was made to establish a session to a Lan Manager server, but there are already too many sessions established to that server."
sMsg = "The workgroup or domain name is already in use by another computer on the network."
sMsg = "The network is not present or not started."
sMsg = "The operation was canceled by the user."
sMsg = "The requested operation cannot be performed on a file with a user-mapped section open."
sMsg = "The remote system refused the network connection."
sMsg = "The network connection was gracefully closed."
sMsg = "The network transport endpoint already has an address associated with it."
sMsg = "An address has not yet been associated with the network endpoint."
sMsg = "An operation was attempted on a nonexistent network connection."
sMsg = "An invalid operation was attempted on an active network connection."
sMsg = "The remote network is not reachable by the transport."
sMsg = "The remote system is not reachable by the transport."
sMsg = "The remote system does not support the transport protocol."
sMsg = "No service is operating at the destination network endpoint on the remote system."
sMsg = "The request was aborted."
sMsg = "The network connection was aborted by the local system."
sMsg = "The operation could not be completed. A retry should be performed."
sMsg = "A connection to the server could not be made because the limit on the number of concurrent connections for this account has been reached."
sMsg = "Attempting to log in during an unauthorized time of day for this account."
sMsg = "The account is not authorized to log in from this station."
sMsg = "The network address could not be used for the operation requested."
sMsg = "The service is already registered."
sMsg = "The specified service does not exist."
sMsg = "The operation being requested was not performed because the user has not been authenticated."
sMsg = "The operation being requested was not performed because the user has not logged on to the network. The specified service does not exist."
sMsg = "Continue with work in progress."
sMsg = "An attempt was made to perform an initialization operation when initialization has already been completed."
sMsg = "No more local devices."
sMsg = "The specified site does not exist."
sMsg = "A domain controller with the specified name already exists."
sMsg = "An error occurred while installing the Windows NT directory service. Please view the event log for more information."
' Non 32-Bit Error Codes
Case ERROR_INVALID_PASSWORD ' = 86&
sMsg = "Invalid password to access resource"
Case ERROR_NOT_ENOUGH_MEMORY ' = 8&
sMsg = "Not enough memory to process the request"
Case ERROR_INVALID_PARAMETER ' = 87&
sMsg = "Invalid parameter"
Case ERROR_NO_MORE_ITEMS ' = 259&
sMsg = "No more items available"
Case ERROR_NOT_SUPPORTED ' 50&
sMsg = "The network request is not supported."
Case ERROR_REM_NOT_LIST ' 51&
sMsg = "The remote computer is not available."
Case ERROR_DUP_NAME ' 52&
sMsg = "A duplicate name exists on the network."
Case ERROR_BAD_NETPATH ' 53&
sMsg = "The network path was not found."
Case ERROR_NETWORK_BUSY ' 54&
sMsg = "The network is busy."
Case ERROR_DEV_NOT_EXIST ' 55 ' dderror
sMsg = "The specified network resource or device is no longer available."
Case ERROR_TOO_MANY_CMDS ' 56&
sMsg = "The network BIOS command limit has been reached."
Case ERROR_ADAP_HDW_ERR ' 57&
sMsg = "A network adapter hardware error occurred."
Case ERROR_BAD_NET_RESP ' 58&
sMsg = "The specified server cannot perform the requested operation."
Case ERROR_UNEXP_NET_ERR ' 59&
sMsg = "An unexpected network error occurred."
Case ERROR_BAD_REM_ADAP ' 60&
sMsg = "The remote adapter is not compatible."
Case ERROR_PRINTQ_FULL ' 61&
sMsg = "The printer queue is full."
Case ERROR_NO_SPOOL_SPACE ' 62&
sMsg = "Space to store the file waiting to be printed is not available on the server."
Case ERROR_PRINT_CANCELLED ' 63&
sMsg = "Your file waiting to be printed was deleted."
Case ERROR_NETNAME_DELETED ' 64&
sMsg = "The specified network name is no longer available."
Case ERROR_NETWORK_ACCESS_DENIED ' 65&
sMsg = "Network access is denied."
Case ERROR_BAD_DEV_TYPE ' 66&
sMsg = "The network resource type is not correct."
Case ERROR_BAD_NET_NAME ' 67&
sMsg = "The network name cannot be found."
Case ERROR_TOO_MANY_NAMES ' 68&
sMsg = "The name limit for the local computer network adapter card was exceeded."
Case ERROR_TOO_MANY_SESS ' 69&
sMsg = "The network BIOS session limit was exceeded."
Case ERROR_SHARING_PAUSED ' 70&
sMsg = "The remote server has been paused or is in the process of being started."
Case ERROR_REQ_NOT_ACCEP ' 71&
sMsg = "The network request was not accepted."
Case ERROR_REDIR_PAUSED ' 72&
sMsg = "The specified printer or disk device has been paused."
sMsg = "Error " & lError & " occurred. No description defined for this error code."
WNetErrorDescription = sMsg
Public Function WNetExtendedError(lError As Long) As String
Dim lRet As Long
Dim lErrorBuf As Long
Dim sErrorBuf As String
Dim lNameBuf As Long
Dim sNameBuf As String
' This should work in Windows 95 or NT
sErrorBuf = Space(256)
lErrorBuf = Len(sErrorBuf)
sNameBuf = Space(256)
lNameBuf = Len(sNameBuf)
lRet = WNetGetLastError(lError, sErrorBuf, lErrorBuf, sNameBuf, lNameBuf)
' for now, just return the error text
WNetExtendedError = RemoveNull(sErrorBuf)