Results 1 to 2 of 2

Thread: WNetCancelConnection2 doesn't work

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2000
    Posts
    4
    I am having difficulty cancelling printer connections using the Win API network functions on an NT server. For example, if the LPT1 is connected to \\server\MYPRINTER using WNetAddConnection all is well. But using WNetCancelConnection2 to cancel the connection doesn't work.

    I think this is a known bug but I can't find a workaround.

    Thanks,

    John

  2. #2
    Guest
    Instead of posting the disconnect, I will post the whole code.

    Code:
    Option Explicit
    
    Private Declare Function WNetAddConnection Lib "mpr.dll" Alias
    "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As
    String, ByVal lpszLocalName As String) As Long
    Private Declare Function WNetCancelConnection Lib "mpr.dll" Alias
    "WNetCancelConnectionA" (ByVal lpszName As String, ByVal bForce As Long)
    As Long
    
    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
    
    '-- Error number and message
    Public ErrorNum         As Long
    Public ErrorMsg         As String
    
    Public rc               As Long
    
    Private Const ERROR_NO_CONNECTION = 8
    Private Const ERROR_NO_DISCONNECT = 9
    
    
    Private 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
    
    '----------------------------------------------
    'WNetAddConnection2
    
    'Allows the caller to redirect (connect) a local
    'device to a network resource. It is similar to
    'WNetAddConnection, except that it takes a pointer
    'to a NETRESOURCE structure to describe the network
    'resource to connect to. It also takes the addition
    'parameters lpUserID and dwFlags.
    
    'lpNetResource
    'Specifies the network resource to connect to.
    'The following fields must be set when making a
    'connection, the others are ignored.
    
    '  lpRemoteName: Specifies the network resource
    '                to connect to. This is limited
    '                to MAX_PATH.
    
    '  lpLocalName: This specifies the name of a local
    '               device to be redirected, such as "F:"
    '               or "LPT1". The string is treated in a
    '               case insensitive manner, and may be
    '               the empty string (or NULL) in which
    '               case a connection to the network resource
    '               is made without making a redirection.
    
    '  lpProvider: Specifies the NP to connect to. If NULL
    '              or empty string, Windows will try each
    '              NP in turn. The caller should set
    '              lpProvider only if it knows for sure
    '              which network it wants. Otherwise, it
    '              is preferable to let Windows determine
    '              which NP the network name maps to.
    '              If this is non NULL, Windows will try
    '              the named NP and no other.
    
    '  dwType: Specifies the type of resource to connect to.
    '          It must be RESOURCETYPE_DISK or RESOURCETYPE_PRINT
    '          if lpLocalName is not the empty string. It may
    '          also be RESOURCETYPE_ANY if lpLocalName is the
    '          empty string.
    
    'lpPassword
    'Specifies the password to be used in making the
    'connection, normally the password associated with
    'lpUserID. A NULL value or string may be passed in
    'to indicate to the function to use the current
    'default password.
    '
    'lpUserID
    'This specifies the identity of the user needed to
    'make the connection. If NULL, a default will be
    'applied. This is used when the user wishes to connect
    'to a resource, but has a different user name or
    'account assigned to him for that resource. This
    'identification represents a security context, and
    'is NP specific.
    '
    'dwFlags
    'This is a bit mask which may have any of the
    'following bits set:
    '
    '  CONNECT_UPDATE_PROFILE: If the connection should
    '                          be made persistent. If set,
    '                          Windows automatically restores
    '                          this connection when the user
    '                          logs on to the network. A connection
    '                          is only made persistent if the
    '                          connection was successful.
    Private 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
    Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias
    "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long,
    ByVal fForce As Long) As Long
    Private Declare Function WNetConnectionDialog Lib "mpr.dll" (ByVal hWnd As
    Long, ByVal dwType As Long) As Long
    Private Declare Function WNetDisconnectDialog Lib "mpr.dll" (ByVal hWnd As
    Long, ByVal dwType As Long) As Long
    
    'Public Const RESOURCE_CONNECTED = &H1
    'Public Const RESOURCE_REMEMBERED = &H3
    'Public Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
    'Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
    'Public Const RESOURCEDISPLAYTYPE_SERVER = &H2
    'Public Const RESOURCEUSAGE_CONTAINER = &H2
    
    Const NO_ERROR = 0
    Const CONNECT_UPDATE_PROFILE = &H1
    Const RESOURCETYPE_DISK = &H1
    Const RESOURCETYPE_PRINT = &H2
    Const RESOURCETYPE_ANY = &H0
    Const RESOURCE_GLOBALNET = &H2
    Const RESOURCEDISPLAYTYPE_SHARE = &H3
    Const RESOURCEUSAGE_CONNECTABLE = &H1
    
    Public Sub Connect(sDrive As String, sService As String, Optional
    sPassword As String = "")
    
       On Error GoTo Err_Connect
       Me.ErrorNum = 0
       Me.ErrorMsg = ""
       rc = WNetAddConnection(sService & Chr(0), sPassword & Chr(0), sDrive &
    Chr(0))
       If rc <> 0 Then GoTo Err_Connect
    
       Exit Sub
    
    Err_Connect:
       Me.ErrorNum = rc
       Me.ErrorMsg = WnetError(rc)
    
    End Sub
    
    Public Sub DisConnect(sDrive As String)
    
       On Error GoTo Err_DisConnect
       Me.ErrorNum = 0
       Me.ErrorMsg = ""
       rc = WNetCancelConnection(sDrive + Chr(0), 0)
       If rc <> 0 Then GoTo Err_DisConnect
    
       Exit Sub
    Err_DisConnect:
       Me.ErrorNum = rc
       Me.ErrorMsg = WnetError(rc)
    
    End Sub
    
    Private Function WnetError(Errcode As Long) As String
    
       Select Case Errcode
          Case WN_Not_Supported:
             WnetError = "Function is not supported."
          Case WN_Out_Of_Memory:
             WnetError = "Out of Memory."
          Case WN_Net_Error:
             WnetError = "An error occurred on the network."
          Case WN_Bad_Pointer:
             WnetError = "The Pointer was Invalid."
          Case WN_Bad_NetName:
             WnetError = "Invalid Network Resource Name."
          Case WN_Bad_Password:
             WnetError = "The Password was Invalid."
          Case WN_Bad_Localname:
             WnetError = "The local device name was invalid."
          Case WN_Access_Denied:
             WnetError = "A security violation occurred."
          Case WN_Already_Connected:
             WnetError = "The local device was connected to a remote
    resource."
          Case Else:
             WnetError = "Unrecognized Error " + Str(Errcode) + "."
       End Select
    
    End Function
    
    Public Function ConnectNetworkDialog() As Long
       ' *** Show the dialog to map a drive
    
       'If the function succeeds, the return value is
       'NO_ERROR (0). If the user cancels out of the
       'dialog box, it is &HFFFFFFFF.
    
       ConnectNetworkDialog = WNetConnectionDialog(0&, RESOURCETYPE_DISK)
    
    End Function
    
    Public Function DisconnectNetworkDialog() As Long
       ' *** Show the dialog to disconnect mapped a drive
    
       'If the function succeeds, the return value is
       'NO_ERROR (0). If the user cancels out of the
       'dialog box, it is &HFFFFFFFF.
    
       DisconnectNetworkDialog = WNetDisconnectDialog(0&, RESOURCETYPE_DISK)
    
    End Function
    
    Public Function ConnectPrintDialog() As Long
       ' *** Show the dialog to map a network printer, Windows
    
       'If the function succeeds, the return value is
       'NO_ERROR (0). If the user cancels out of the
       'dialog box, it is &HFFFFFFFF.
    
       ConnectPrintDialog = WNetConnectionDialog(0&, RESOURCETYPE_PRINT)
    
    End Function
    
    Public Function DisconnectPrintDialog() As Long
       ' *** Show the dialog to disconnect network printer
    
       'If the function succeeds, the return value is
       'NO_ERROR (0). If the user cancels out of the
       'dialog box, it is &HFFFFFFFF.
    
       DisconnectPrintDialog = WNetDisconnectDialog(0&, RESOURCETYPE_PRINT)
    
    End Function
    
    Public Function ConnectUserPassword(sDrive As String, sService As String,
    Optional sUser As String = "", Optional sPassword As String = "") As
    Boolean
       ' *** Connect to a network drive
    
       'attempts to connect to the passed network
       'connection to the specified drive.
       'ErrInfo=NO_ERROR if successful.
    
       Dim NETR       As NETRESOURCE
       Dim errInfo    As Long
    
       With NETR
          .dwScope = RESOURCE_GLOBALNET
          .dwType = RESOURCETYPE_DISK
          .dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
          .dwUsage = RESOURCEUSAGE_CONNECTABLE
          .lpRemoteName = sDrive
          .lpLocalName = sService
       End With
    
       errInfo = WNetAddConnection2(NETR, sPassword, sUser,
    CONNECT_UPDATE_PROFILE)
    
       ConnectUserPassword = errInfo = NO_ERROR
    
    End Function
    Not sure if it'll work, but it's a try.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width