Public Property Get NET_API_STATUS() As Long
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.NET_API_STATUS
NET_API_STATUS = mvarNET_API_STATUS
End Property
Public Property Get strLastError() As Variant
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.strLastError
If IsObject(mvarstrLastError) Then
Set strLastError = mvarstrLastError
Else
strLastError = mvarstrLastError
End If
End Property
Public Property Get nLastError() As Long
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.nLastError
nLastError = mvarnLastError
End Property
Public Property Get strPath() As Variant
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.strPath
If IsObject(mvarstrPath) Then
Set strPath = mvarstrPath
Else
strPath = mvarstrPath
End If
End Property
Public Property Get nMax_uses() As Long
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.nMax_uses
nMax_uses = mvarnMax_uses
End Property
Public Property Get nCurrent_uses() As Long
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.nCurrent_uses
nCurrent_uses = mvarnCurrent_uses
End Property
Public Property Get strRemark() As Variant
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.strRemark
If IsObject(mvarstrRemark) Then
Set strRemark = mvarstrRemark
Else
strRemark = mvarstrRemark
End If
End Property
Public Property Get nType() As Long
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.nType
nType = mvarnType
End Property
Public Property Get strType() As Variant
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.strType
Select Case mvarnType
Case STYPE_DISKTREE
strType = "Disk Drive"
Case STYPE_PRINTQ
strType = "Print Queue"
Case STYPE_DEVICE
strType = "Communication device"
Case STYPE_IPC
strType = "Interprocess communication (IPC)"
Case STYPE_SPECIAL
strType = "Special share"
Case Else
strType = "Error: Unknown"
End Select
End Property
Public Property Get strNetName() As Variant
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.strNetName
If IsObject(mvarstrNetName) Then
Set strNetName = mvarstrNetName
Else
strNetName = mvarstrNetName
End If
End Property
Public Property Get strServer() As Variant
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.strServer
If IsObject(mvarstrServer) Then
Set strServer = mvarstrServer
Else
strServer = mvarstrServer
End If
End Property
Public Sub Initialize()
' Reset the everything
'
mvarnLastError = 0
mvarstrLastError = ""
mvarstrServer = ""
mvarstrNetName = ""
mvarnType = 0
mvarstrRemark = ""
mvarnCurrent_uses = 0
mvarnMax_uses = 0
mvarstrPath = ""
End Sub
Public Sub GetInfo(strShareName As Variant)
Dim pNetName() As Byte
Dim pServer() As Byte
Dim ptmpBuffer As Long
Dim tmpBuffer As SHARE_INFO_502
Dim strNetName As String
Dim x As Integer
Call Initialize
' copy the network share name without leading spaces.
'
strNetName = LTrim(strShareName)
' check for leading server in the name.
'
If Left(strNetName, 2) = "\\" Then
' find the end of the server in the name
'
x = InStr(3, strNetName, "\")
' only a server in the name
'
If x = 0 Then
mvarnLastError = ERROR_INVALID_NAME
mvarstrLastError = "Need share name not server name."
Exit Sub
Else
mvarstrServer = Left(strNetName, x - 1)
strNetName = Mid(strNetName, x + 1)
End If
End If
' strip off any remaining leading \
'
If Left(strNetName, 1) = "\" Then
strNetName = Mid(strNetName, 2)
End If
' Find the end of the share name.
'
x = InStr(strNetName, "\")
If x > 0 Then
strNetName = Left(strNetName, x - 1)
End If
' Check for drive letter
'
x = InStr(strNetName, ":")
If x > 0 Then
mvarnLastError = ERROR_INVALID_NAME
mvarstrLastError = "Drive letter specified for share name."
Exit Sub
End If
' Convert the string to a UNI string, happens automatically.
'
pNetName = strNetName & vbNullChar
If Len(mvarstrServer) > 0 Then
' format the server name
'
If Left(mvarstrServer, 2) = "\\" Then
pServer = mvarstrServer & vbNullChar
Else
pServer = "\\" & mvarstrServer & vbNullChar
End If
' Get the network infomation on the share.
'
mvarNET_API_STATUS = NetShareGetInfo _
( _
pServer(0), _
pNetName(0), _
502, _
ptmpBuffer _
)
Else
' Get the network infomation on the share.
' NOTE: the first parameter is the server name, by sending a
' null you are only looking at the current machine.
'
mvarNET_API_STATUS = NetShareGetInfo _
( _
vbEmpty, _
pNetName(0), _
502, _
ptmpBuffer _
)
End If
' Check for errors.
If mvarNET_API_STATUS <> NERR_Success Then
Select Case mvarNET_API_STATUS
Case ERROR_ACCESS_DENIED
mvarstrLastError = "NetShareGetInfo: ERROR_ACCESS_DENIED"
Case ERROR_INVALID_LEVEL
mvarstrLastError = "NetShareGetInfo: ERROR_INVALID_LEVEL"
Case ERROR_INVALID_PARAMETER
mvarstrLastError = "NetShareGetInfo: ERROR_INVALID_PARAMETER"
Case ERROR_MORE_DATA
mvarstrLastError = "NetShareGetInfo: ERROR_MORE_DATA"
Case ERROR_NOT_ENOUGH_MEMORY
mvarstrLastError = "NetShareGetInfo: ERROR_NOT_ENOUGH_MEMORY"
Case ERROR_INVALID_NAME
mvarstrLastError = "NetShareGetInfo: ERROR_INVALID_NAME"
Case NERR_NetNameNotFound
mvarstrLastError = "NetShareGetInfo: NERR_NetNameNotFound"
Case Else
mvarstrLastError = "NetShareGetInfo: Unknown " & mvarNET_API_STATUS
End Select
mvarnLastError = mvarNET_API_STATUS
Exit Sub
End If
' Copy the return buffer to a type definition for processing.
Call CopyMemory(tmpBuffer, ptmpBuffer, LenB(tmpBuffer))
' save the return buffer information.
mvarstrNetName = UtoA(tmpBuffer.shi502_netname)
mvarnType = tmpBuffer.shi502_type
mvarstrRemark = UtoA(tmpBuffer.shi502_remark)
mvarnCurrent_uses = tmpBuffer.shi502_current_uses
mvarnMax_uses = tmpBuffer.shi502_max_uses
mvarstrPath = UtoA(tmpBuffer.shi502_path)
' Free the buffer.
mvarNET_API_STATUS = NetApiBufferFree(ptmpBuffer)
' Check for errors.
If mvarNET_API_STATUS <> ERROR_SUCCESS Then
mvarnLastError = mvarNET_API_STATUS
mvarstrLastError = "NetApiBufferFree: Unknown"
Exit Sub
End If
End Sub
Private Function UtoA(pUNIstring As Long) As String
Dim wrkByte() As Byte
Dim wrkStr As String
' Get space for string each character is two bytes and a null terminator.
ReDim wrkByte(lstrlenW(pUNIstring) * 2 + 2)
' Copy the string to a byte array
Call lstrcpyW(wrkByte(0), pUNIstring)
' Covert the string from a UNI string to a ASCII string.
' this happens automatically when a byte array is copied to a string.
wrkStr = wrkByte
' return everything upto the the null terminator.
UtoA = Left(wrkStr, InStr(wrkStr, Chr(0)) - 1)
End Function
'In a form
Option Explicit
Private Sub Form_Load()
m_lblStatus.Caption = ""
' Good test - admin share
TestShareGetInfo "admin$"
' Good test - share with leading slash
TestShareGetInfo "\admin$"
' Good test - share with trailing slash
TestShareGetInfo "admin$\"
' Good test - share with trailing slash
TestShareGetInfo "\admin$\"
' Good test
TestShareGetInfo "testdata"
' Good test - should not have server name, but we fix that
TestShareGetInfo "\\lee\testdata"
' Good test - should not have server name, but we fix that
TestShareGetInfo "\\lee\admin$"
' *** Good test - remote server
TestShareGetInfo "\\maggie\admin$"
' *** Bad test - no share
TestShareGetInfo "NoShareCalledThis"
' *** Bad test - no remote share
TestShareGetInfo "\\maggie\NoShareCalledThis"
End Sub
Private Sub TestShareGetInfo(strShare As String)
Dim x As New CprgNetShareGetInfo
m_lblStatus.Caption = m_lblStatus.Caption & "Test Share: " & strShare & " = "
x.GetInfo strShare
If x.nLastError = 0 Then
m_lblStatus.Caption = m_lblStatus.Caption _
& vbCrLf & " Server: " & x.strServer & " Path: " & x.strPath & vbCrLf
Else
m_lblStatus.Caption = m_lblStatus.Caption & vbCrLf & " Error: " _
& x.nLastError & " " & x.strLastError & vbCrLf
End If
End Sub