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



The information in this article applies to:

  • Microsoft Windows 2000 Server
  • Microsoft Windows 2000 Advanced Server
  • Microsoft Windows 2000 Professional
  • Microsoft Windows 98
  • Microsoft Win32 Application Programming Interface (API)
  • Microsoft Windows NT Server 4.0
  • Microsoft Windows NT Workstation 4.0

This article was previously published under Q243627

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:

173011 HOWTO: Add and Remove Network Connections

183366 INFO: WNetAddConnection2 and Multiple User Credentials


Modification Type:MinorLast Reviewed:12/20/2004
Keywords:kbDSWNET2003Swept kbAPI kbinfo kbnetwork kbWNet KB243627