INFO: Disconnecting from a NetWare File Server by Using WNetCancelConnection2() API

ID: Q243627


The information in this article applies to:
  • Microsoft Windows 98
  • Microsoft Win32 Application Programming Interface (API)
  • Microsoft Windows 2000
  • Microsoft Windows NT 4.0


SUMMARY

Logging off and detaching from a NetWare file server by using the WNetCancelConnection2 function requires that you pass only the file server name as the resource name.

The connection type should be 0 (zero) and the force parameter should be set to True as shown in the following Visual Basic subroutines, Attach_FileServer and Detach_FileServer:


Option Explicit

Declare Function WNetAddConnection2 Lib "mpr.dll" Alias _
"WNetAddConnection2A" (lpNetResource As NETRESOURCE, _
ByVal lpPassword As String, ByVal lpUserName As String, _
ByVal dwFlags As Long) As Long

Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias _
"WNetCancelConnection2A" (ByVal lpName As String, _
ByVal dwFlags As Long, ByVal fForce As Long) As Long

Type NETRESOURCE
  dwScope As Long
  dwType As Long
  dwDisplayType As Long
  dwUsage As Long
  lpLocalName As String
  lpRemoteName As String
  lpComment As String
  lpProvider As String
End Type

Const NO_ERROR = 0
' The following includes all the constants defined for NETRESOURCE,
' not just the ones used in this example.
Const RESOURCETYPE_DISK = &H1
Const RESOURCE_GLOBALNET = &H2
Const RESOURCEDISPLAYTYPE_SHARE = &H3
Const RESOURCEUSAGE_CONNECTABLE = &H1

Sub Attach_FileServer()
  Dim rc As Integer
  Dim NetR As NETRESOURCE
  Dim MyPass, MyUser As String
    
  NetR.dwScope = RESOURCE_GLOBALNET
  NetR.dwType = RESOURCETYPE_DISK
  NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
  NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
  NetR.lpLocalName = "" ' If undefined, Connect with no device
  NetR.lpRemoteName = "\\FILE_SERVER"
  
  ' If the UserName and Password arguments are NULL, the user context
  ' for the process provides the default user name.
  MyUser = "Guest"
  MyPass = "guest"
      
  rc = WNetAddConnection2(NetR, MyPass, MyUser, 0)
    
  If (rc <> NO_ERROR) Then
    MsgBox "Error " & CStr(rc) & ": Cannot Attach to FileServer"
  Else
    MsgBox "Attached"
  End If
End Sub

Sub Detach_FileServer()
  Dim rc As Integer
  
  ' To disconnect from a NetWare server, we have to use just the 
  ' file server name. 
  rc = WNetCancelConnection2("\\FILE_SERVER", 0, True)
  
  If (rc <> NO_ERROR) Then
    MsgBox "Error " & CStr(rc) & ": Cannot Detach from FileServer"
  Else
    MsgBox "Detached"
  End If
End Sub 


MORE INFORMATION

To determine whether NetWare file servers are attached, right-click Network Neighborhood (or My Network Places), and then click Who am I. This action shows the servers that are currently attached. If you perform this action after the WNetAddConnection2function, the server that was attached to is displayed; after the WNetCancelConnection2 function, the server should not be present in the list.


REFERENCES

For additional information on adding/removing connections, click the article numbers below to view the articles in the Microsoft Knowledge Base:

Q173011 HOWTO: Add and Remove Network Connections
Q183366 INFO: WNetAddConnection2 and Multiple User Credentials

Additional query words: kbnetwork kbSDKPlatform kbWNet kbDSupport kbGrpNet

Keywords : kbnetwork kbAPI kbNTOS400 kbWinOS2000 kbSDKPlatform kbSDKWin32 kbWinOS95 kbWinOS98 kbWNet kbDSupport kbGrpNet
Version : WINDOWS:; winnt:4.0
Platform : WINDOWS winnt
Issue type : kbinfo


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