Article ID: 141757
Article Last Modified on 1/8/2003
Public Declare Function WnetAddConnection% Lib "user"
(ByVal lpszNetPath As Any,
ByVal lpszPassword As Any,
ByVal lpszLocalName As Any)
Formal Parameter Definition
-----------------------------------------------------------------------
lpszNetPath Points to a null-terminated string specifying the
shared device or remote server.
lpszPassword Points to a null-terminated string specifying the
network password for the given device or server.
lpszLocalName Points to a null-terminated string specifying the
local drive or device to be redirected. All
lpszLocalName strings (such as LPT1) are case
independent. Only the drive names A through Z
and device names LPT1 through LPT3 are used.
Value (Hex Value) Meaning
-----------------------------------------------------------------------
WN_SUCCESS (&H0) Function was successful.
WN_NOT_SUPPORTED (&H1) Function was not supported.
WN_OUT_OF_MEMORY (&HB) System was out of memory.
WN_NET_ERROR (&H2) An error occurred on the network.
WN_BAD_POINTER (&H4) Pointer was invalid.
WN_BAD_NETNAME (&H32) Network resource name was invalid.
WN_BAD_LOCALNAME (&H33) Local device name was invalid.
WN_BAD_PASSWORD (&H6) Password was invalid.
WN_ACCESS_DENIED (&H7) A security violation occurred.
WN_ALREADY_CONNECTED (&H34) Local device was already connected
to a remote resource.
Default Name Caption CtlName ------------------------------------------- Text1 (Not applicable) NetPath Text2 (Not applicable) Password Command1 &Connect Connect Drive1 (Not applicable) Drive1
Private Declare Function WnetAddConnection Lib "user"
(ByVal lpszNetPath as String,
ByVal lpszPassword as String,
ByVal lpszLocalName as String) as Integer
Const WN_Success = &H0
Const WN_Not_Supported = &H1
Const WN_Net_Error = &H2
Const WN_Bad_Pointer = &H4
Const WN_Bad_NetName = &H32
Const WN_Bad_Password = &H6
Const WN_Bad_Localname = &H33
Const WN_Access_Denied = &H7
Const WN_Out_Of_Memory = &HB
Const WN_Already_Connected = &H34
If you're using Visual Basic version 1.0, add the following to the
general declarations also:
Const True = -1 Const False = 0
Private Sub Connect_Click ()
ServerText$ = UCase$(NetPath.Text) + Chr$(0) ' Network resource name
PasswordText$ = Password.Text + Chr$(0) ' Password for the resource
driveletter$ = "N:" + Chr$(0) ' Substitute your own drive letter
' Enter the following two lines as one, single line:
Succeed% =
WnetAddConnection(ServerText$, PasswordText$, driveletter$)
If IsSuccess(Succeed%, msg$) = True Then ' Call Function to parse
' potential error messages.
Drive1.Refresh
NetPath.Text = "" ' Reset the contents following connection
Else
MsgBox msg$
End If
End Sub
Private Function IsSuccess% (ReturnCode%, Msg$)
If ReturnCode% = WN_Success Then
IsSuccess% = True
Else
IsSuccess% = False
Select Case ReturnCode%
Case WN_Success:
Drive1.Refresh
Case WN_Not_Supported:
msg$ = "Function is not supported."
Case Wn_Out_Of_Memory:
msg$ = "Out of Memory."
Case WN_Net_Error:
msg$ = "An error occurred on the network."
Case WN_Bad_Pointer:
msg$ = "The Pointer was Invalid."
Case WN_Bad_NetName:
msg$ = "Invalid Network Resource Name."
Case WN_Bad_Password:
msg$ = "The Password was Invalid."
Case WN_Bad_Localname:
msg$ = "The local device name was invalid."
Case WN_Access_Denied:
msg$ = "A security violation occurred."
Case WN_Already_Connected:
msg$ = "The local device was connected to a remote resource."
Case Else:
msg$ = "Unrecognized Error " + Str$(ReturnCode%) + "."
End Select
End If
End Function
Additional query words: 1.00 2.00 4.00 vb4win vb416
Keywords: kbnetwork KB141757