NetShareGetInfo

Iniciado por David Vans, 4 Agosto 2006, 16:37 PM

0 Miembros y 1 Visitante están viendo este tema.

David Vans

Saludos , necesito saber si alguien me puede ayudar a conseguir los recursoso compartidos que tiene el pc desde codigo en vb 6. Habia pensado ejecutar un.

Net view \\127.0.0.1 > C:\recursosocompartidos.txt

y ir separando las lineas y quedarme solo con los recursos pero me parece una chapuza, he oido hblar de NetShareGetInfo pero no encuntro mucha informacion

Gracias

al3

Proba con esto:

'This example was submitted by Lee Carpenter
'
'It needs a class module and a form, with a label (m_lblStatus) on the form

'In the class module (CprgNetShareGetInfo)

Option Explicit

'local variable(s) to hold property value(s)
Private mvarstrServer As Variant 'local copy
Private mvarstrNetName As Variant 'local copy
Private mvarnType As Long 'local copy
Private mvarstrRemark As Variant 'local copy
Private mvarnCurrent_uses As Long 'local copy
Private mvarnMax_uses As Long 'local copy
Private mvarstrPath As Variant 'local copy
Private mvarnLastError As Long 'local copy
Private mvarstrLastError As Variant 'local copy
Private mvarNET_API_STATUS As Long 'local copy

'local variable(s) to hold internal value(s)

' Private constants, types and  declares to call
'

Const STYPE_DISKTREE            As Long = 0
Const STYPE_PRINTQ              As Long = 1
Const STYPE_DEVICE              As Long = 2
Const STYPE_IPC                 As Long = 3
Const STYPE_SPECIAL             As Long = &H80000000

Const ERROR_SUCCESS             As Long = 0&
Const NERR_Success              As Long = 0&
Const ERROR_ACCESS_DENIED       As Long = 5&
Const ERROR_INVALID_LEVEL       As Long = 124&
Const ERROR_INVALID_PARAMETER   As Long = 87&
Const ERROR_MORE_DATA           As Long = 234&
Const ERROR_NOT_ENOUGH_MEMORY   As Long = 8&
Const ERROR_INVALID_NAME        As Long = 123&

Const NERR_BASE                 As Long = 2100&
Const NERR_NetNameNotFound      As Long = (NERR_BASE + 210)


Private Type SHARE_INFO_502
  shi502_netname      As Long   ' LPWSTR    shi502_netname;
  shi502_type         As Long   ' DWORD     shi502_type;
  shi502_remark       As Long   ' LPWSTR    shi502_remark;
  shi502_permissions  As Long   ' DWORD     shi502_permissions;
  shi502_max_uses     As Long   ' DWORD     shi502_max_uses;
  shi502_current_uses As Long   ' DWORD     shi502_current_uses;
  shi502_path         As Long   ' LPWSTR    shi502_path;
  shi502_passwd       As Long   ' LPWSTR    shi502_passwd;
  shi502_reserved     As Long   ' DWORD     shi502_reserved;
  shi502_security_descriptor As Long ' PSECURITY_DESCRIPTOR  shi502_security_descriptor;
End Type

'NET_API_STATUS NET_API_FUNCTION
'NetShareGetInfo (
'    IN  LPTSTR  servername,
'    IN  LPTSTR  netname,
'    IN  DWORD   level,
'    OUT LPBYTE * bufptr
'    );
Private Declare Function NetShareGetInfo Lib "Netapi32.dll" _
  ( _
    strServerName As Any, _
    strNetName As Any, _
    ByVal nLevel As Long, _
    pBuffer As Long _
  ) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
( _
  Destination As Any, _
  ByVal Source As Any, _
  ByVal Length As Long _
)

Private Declare Function NetApiBufferFree Lib "Netapi32.dll" _
( _
  ByVal lpBuffer As Long _
) As Long

Private Declare Sub lstrcpyW Lib "kernel32" _
( _
  dest As Any, _
  ByVal src As Any _
)

Private Declare Function lstrlenW Lib "kernel32" _
( _
  ByVal lpszString As Any _
) As Long

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

David Vans

Muchas gracias Voy a prbarlo ahora mismo
Saludos  ;D

David Vans

en esta linea
Dim x As New CprgNetShareGetInfo
me sale error no se ha difinido tipo definido por el usuario >:(