Source code for Issue Number 27

Copyright 1997-2000 by C&D Programming Corp. All Rights Reserved. Source code may not be reproduced except for use in a compiled executable. All rights reserved. If you would like to reprint any or all of this code please email us at info@codeoftheweek.com

Code of the Week Home


Source Code

Just paste this code into any class module and change the name of the module to cNetworkInterface.

'----------------------------------------------------------------------
'
'   Module Name:    cNetworkInterface
'   Written By:     C&D Programming Corp.
'   Create Date:    2/98
'   Copyright:      Copyright 1998 by C&D Programming Corp.  Source
'                   code may not be reproduced except for use in a
'                   compiled executable.  All rights reserved.  If
'                   you would like to reprint any or all of this
'                   code please email us at info@codeoftheweek.com
'----------------------------------------------------------------------
Option Explicit

Public Enum NetInfoResourceType
    niResTypeAny = &H0
    niResTypeDisk = &H1
    niResTypePrint = &H2
    niResTypeUnknown = &HFFFF
End Enum

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

Private Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" _
            (ByVal lpName As String, ByVal lpUserName As String, _
            lpnLength As Long) As Long

'
'   Only niResTypeDisk or niResTypePrint are valid in this call.
'
Public Sub ConnectDialog(niResType As NetInfoResourceType, _
                        Optional hParentWnd As Long = 0)
    WNetConnectionDialog hParentWnd, niResType
End Sub

Public Sub ConnectDiskDialog(Optional hParentWnd As Long)
    ConnectDialog niResTypeDisk, hParentWnd
End Sub

Public Sub ConnectPrinterDialog(Optional hParentWnd As Long)
    ConnectDialog niResTypePrint, hParentWnd
End Sub

'
'   Only niResTypeDisk or niResTypePrint are valid in this call.
'
Public Sub DisconnectDialog(niResType As NetInfoResourceType, _
                        Optional hParentWnd As Long = 0)
    WNetDisconnectDialog hParentWnd, niResType
End Sub

Public Sub DisconnectDiskDialog(Optional hParentWnd As Long)
    DisconnectDialog niResTypeDisk, hParentWnd
End Sub

Public Sub DisconnectPrinterDialog(Optional hParentWnd As Long)
    DisconnectDialog niResTypePrint, hParentWnd
End Sub

Public Property Get LoginName() As String
    Dim lRet As Long
    Dim lLoginName As Long
    Dim sLoginName As String

    ' This should work in Windows 95 or NT
    sLoginName = Space(128)
    lLoginName = Len(sLoginName)
    lRet = WNetGetUser(ByVal 0&, sLoginName, lLoginName)
    If lRet = 0 Then
        ' we need to remove the null at the end of the string
        sLoginName = Left(sLoginName, InStr(sLoginName, Chr(0)) - 1)
    Else
        sLoginName = "" ' can not determine login name
    End If
    LoginName = sLoginName
End Property