How to Connect to a Network Drive by Using WNetAddConnection

ID: Q141757


The information in this article applies to:
  • Microsoft Visual Basic Professional and Enterprise Editions, 16-bit only, for Windows, version 4.0


SUMMARY

Windows version 3.1 provides a new API Call, WNetAddConnection, which redirects 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:


   Public Declare Function WnetAddConnection% Lib "user"
      (ByVal lpszNetPath As Any,
      ByVal lpszPassword As Any,
      ByVal lpszLocalName As Any) 

Here are the 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:
  1. Start Visual Basic (VB.EXE). Form1 is created by default.


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


  3. Add the following code to the general declaration section of Form1. Enter the Declare statement as one, single line:
    
       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
     


  4. Add the following code to the procedure Connect_Click:
    
       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
     


  5. Create a Sub within the (Declarations) section of the Code window and add the following code:
    
       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
     


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



REFERENCES

MSDN/VB Starter Kit, (contained with the Professional and Enterprise versions of Visual Basic 4.0), Product Documentation, SDKs, "Windows 3.1 SDK," Programmers Reference Volume 2, and the WIN31API.HLP file (also shipped with the Microsoft Visual Basic version 2.0, 3.0 Professional Version).

Additional query words: 1.00 2.00 4.00 vb4win vb416

Keywords : kbcode kbnetwork
Version : 4.00
Platform : WINDOWS
Issue type :


Last Reviewed: June 16, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.