Til først skal du lige erklære de her api kald, og definere nogle consts og types :
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
Public Const NO_ERROR = 0 Public Const CONNECT_UPDATE_PROFILE = &H1 Public Const RESOURCETYPE_DISK = &H1 Public Const RESOURCETYPE_PRINT = &H2 Public Const RESOURCETYPE_ANY = &H0 Public Const RESOURCE_CONNECTED = &H1 Public Const RESOURCE_REMEMBERED = &H3 Public Const RESOURCE_GLOBALNET = &H2 Public Const RESOURCEDISPLAYTYPE_DOMAIN = &H1 Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0 Public Const RESOURCEDISPLAYTYPE_SERVER = &H2 Public Const RESOURCEDISPLAYTYPE_SHARE = &H3 Public Const RESOURCEUSAGE_CONNECTABLE = &H1 Public Const RESOURCEUSAGE_CONTAINER = &H2
Når der er gjordt kan du oprette en forbindelse ved at bruge :
Dim NetR As NETRESOURCE Dim ErrInfo As Long NetR.dwScope = RESOURCE_GLOBALNET NetR.dwType = RESOURCETYPE_DISK NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE NetR.lpLocalName = \"H:\" NetR.lpRemoteName = \"\\\\Computer\\Share\" ErrInfo = WNetAddConnection2(NetR, \"Password\", \"Brugernavn\", CONNECT_UPDATE_PROFILE) \'Password og brugernavn kan selvfølgelig sættes til \"\" og \"\" hvis de ikke skal bruges
If ErrInfo = NO_ERROR Then MsgBox \"Forbindelsen er skabt\" Else MsgBox \"Fejl: \" & Str(ErrInfo) End If
Og fjerne forbindelsen sådan : Dim ErrInfo As Long Dim strLocalName As String strLocalName = \"H:\" ErrInfo = WNetCancelConnection2(strLocalName, CONNECT_UPDATE_PROFILE, False)
If ErrInfo = NO_ERROR Then MsgBox \"Forbindelsen er afbrudt!\" Else MsgBox \"Fejl: \" & Str(ErrInfo) End If
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.