Microsoft KB Archive/94679

= VB3 Connecting to a Network Drive by Using WNetAddConnection =

Article ID: 94679

Article Last Modified on 11/18/2003

-

APPLIES TO


 * Microsoft Visual Basic 1.0 Standard Edition
 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q94679



SUMMARY
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.



MORE INFORMATION
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 &quot;user&quot; (ByVal lpszNetPath As Any,     ByVal lpszPassword As Any,      ByVal lpszLocalName As Any)

Here are definitions for the formal parameters:

  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.

Below are the possible return values as defined on page 990 of the Microsoft Windows version 3.1 Programmer's Reference:

  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.

Below is an example of how to redirect a local device to a network resource:

 Start Visual Basic (VB.EXE). Form1 is created by default.  Create the following controls with the indicated properties on Form1:   Default Name   Caption             CtlName ---  Text1          (Not applicable)    NetPath Text2         (Not applicable)    Password Command1      &Connect            Connect Drive1        (Not applicable)    Drive1

  Add the following code to the general declaration section of Form1. Enter the Declare statement as one, single line: Declare Function WnetAddConnection Lib &quot;user&quot; (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

  Add the following code to the procedure Connect_Click: Sub Connect_Click

ServerText$ = UCase$(NetPath.Text) + Chr$(0) ' Network resource name PasswordText$ = Password.Text + Chr$(0) ' Password for the resource driveletter$ = &quot;N:&quot; + 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 = &quot;&quot; ' Reset the contents following connection Else MsgBox msg$ End If

End Sub

  Create a Sub within the (Declarations) section of the Code window and add the following code: 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$ = &quot;Function is not supported.&quot; Case Wn_Out_Of_Memory: msg$ = &quot;Out of Memory.&quot; Case WN_Net_Error: msg$ = &quot;An error occurred on the network.&quot; Case WN_Bad_Pointer: msg$ = &quot;The Pointer was Invalid.&quot; Case WN_Bad_NetName: msg$ = &quot;Invalid Network Resource Name.&quot; Case WN_Bad_Password: msg$ = &quot;The Password was Invalid.&quot; Case WN_Bad_Localname: msg$ = &quot;The local device name was invalid.&quot; Case WN_Access_Denied: msg$ = &quot;A security violation occurred.&quot; Case WN_Already_Connected: msg$ = &quot;The local device was connected to a remote resource.&quot; Case Else: msg$ = &quot;Unrecognized Error &quot; + Str$(ReturnCode%) + &quot;.&quot;

End Select End If

End Function

</li> Run the program. Type in the name of a network resource in the edit box and press the Connect button. The drive box will be updated with the new resource if the call was successful.</li></ol>

<div class="references_section">