VB3 Connecting to a Network Drive by Using WNetAddConnection

Last reviewed: January 9, 1997
Article ID: Q94679
The information in this article applies to:

- Microsoft Visual Basic programming system for Windows,

  versions 1.0, 2.0, and 3.0

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 "user"
      (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:

  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:

       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:

       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:

    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

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


KBCategory: kbnetwork kbprg kbcode
KBSubcategory: APrgNet
Additional reference words: 1.00 2.00


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 9, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.