VB3 Connecting to a Network Drive by Using WNetAddConnectionID: Q94679
|
Windows version 3.1 provides a new API Call, WNetAddConnection, which will
redirect a local device to a shared resource or network server.
WNetAddConnection requires the name of the local device, the name of the
network resource, and the password necessary to use that resource.
This article explains in detail the arguments and potential error messages
for the Windows version 3.1 WNetAddConnection function call.
To use WNetAddConnection within a Visual Basic application, declare the
WNetAddConnection function in the General Declarations Section of your code
window. (In Visual Basic version 1.0 you can also put the declaration in
the Global Module.) Declare the function as follows entering the entire
Declare statement on one, single line:
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
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
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
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
"Microsoft Windows Software Development Kit: Reference Volume 2," version 3.1 and the WIN31WH.HLP file that shipped with the Microsoft Visual Basic version 2.0 Professional Version for Windows.
Additional query words: 2.00
Keywords : kbcode kbnetwork
Version : 1.00 2.00 3.00
Platform : WINDOWS
Issue type :
Last Reviewed: June 11, 1999