Advertisement
7_2009-2012 Internet/ HTML #238889

Code to Connect another computer on LAN

This article demonstrates how to programmatically create and remove network connections by using Windows API functions. The following example will add a connection to a network share and will disconnect from the same share.

AI

สรุปโดย AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

ซอร์สโค้ด
original-source
Option Explicit
   Private Sub Command1_Click()
   Dim NetR As NETRESOURCE
   Dim ErrInfo As Long
   Dim MyPass As String, MyUser As String
   NetR.dwScope = RESOURCE_GLOBALNET
   NetR.dwType = RESOURCETYPE_DISK
   NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
   NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
   NetR.lpLocalName = "X:" ' If undefined, Connect with no device
   NetR.lpRemoteName = "\\ServerName\ShareName"  ' Your valid share
   'NetR.lpComment = "Optional Comment"
   'NetR.lpProvider =  ' Leave this undefined
   ' If the MyPass and MyUser arguments are null (use vbNullString), the
   ' user context for the process provides the default user name.
   ErrInfo = WNetAddConnection2(NetR, MyPass, MyUser, _
   CONNECT_UPDATE_PROFILE)
   If ErrInfo = NO_ERROR Then
    MsgBox "Net Connection Successful!", vbInformation, _
    "Share Connected"
   Else
    MsgBox "ERROR: " & ErrInfo & " - Net Connection Failed!", _
    vbExclamation, "Share not Connected"
   End If
   End Sub
   Private Sub Command2_Click()
   Dim ErrInfo As Long
   Dim strLocalName As String
   ' You may specify either the lpRemoteName or lpLocalName
   'strLocalName = "\\ServerName\ShareName"
   strLocalName = "X:"
   ErrInfo = WNetCancelConnection2(strLocalName, _
   CONNECT_UPDATE_PROFILE, False)
   If ErrInfo = NO_ERROR Then
    MsgBox "Net Disconnection Successful!", vbInformation, _
    "Share Disconnected"
   Else
    MsgBox "ERROR: " & ErrInfo & " - Net Disconnection Failed!", _
    vbExclamation, "Share not Disconnected"
   End If
   End Sub
ความคิดเห็นดั้งเดิม (3)
กู้คืนจาก Wayback Machine