How to Share a Folder using NetShareAdd

解决方案 »

  1.   

    To a form add a command button (Command1) and five text boxes (Text1 - Text5). Labels are optional. Add the following to the form: --------------------------------------------------------------------------------
     
    Option Explicit
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Copyright ©1996-2002 VBnet, Randy Birch, All Rights Reserved.
    ' Some pages may also contain other copyrights by the author.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Distribution: You can freely use this code in your own
    '               applications, but you can not publish
    '               or reproduce this code on any web site,
    '               on any online service, or distribute on
    '               any media without express permission.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Private Const NERR_SUCCESS As Long = 0&'share types
    Private Const STYPE_ALL       As Long = -1  'note: my const
    Private Const STYPE_DISKTREE  As Long = 0
    Private Const STYPE_PRINTQ    As Long = 1
    Private Const STYPE_DEVICE    As Long = 2
    Private Const STYPE_IPC       As Long = 3
    Private Const STYPE_SPECIAL   As Long = &H80000000'permissions
    Private Const ACCESS_READ     As Long = &H1
    Private Const ACCESS_WRITE    As Long = &H2
    Private Const ACCESS_CREATE   As Long = &H4
    Private Const ACCESS_EXEC     As Long = &H8
    Private Const ACCESS_DELETE   As Long = &H10
    Private Const ACCESS_ATRIB    As Long = &H20
    Private Const ACCESS_PERM     As Long = &H40
    Private Const ACCESS_ALL      As Long = ACCESS_READ Or _
                                            ACCESS_WRITE Or _
                                            ACCESS_CREATE Or _
                                            ACCESS_EXEC Or _
                                            ACCESS_DELETE Or _
                                            ACCESS_ATRIB Or _
                                            ACCESS_PERMPrivate Type SHARE_INFO_2
      shi2_netname       As Long
      shi2_type          As Long
      shi2_re        As Long
      shi2_permissions   As Long
      shi2_max_uses      As Long
      shi2_current_uses  As Long
      shi2_path          As Long
      shi2_passwd        As Long
    End Type
      
    Private Declare Function NetShareAdd Lib "netapi32" _
      (ByVal servername As Long, _
       ByVal level As Long, _
       buf As Any, _
       parmerr As Long) As Long   Private Sub Form_Load()
      
       Text1.Text = "\\" & Environ$("COMPUTERNAME")
       Text2.Text = "c:\program files\adobe"
       Text3.Text = "vbnetdemo"
       Text4.Text = "VBnet demo test share"
       Text5.Text = ""
       
    End Sub
    Private Sub Command1_Click()   Dim success As Long
                   
       success = ShareAdd(Text1.Text, _
                          Text2.Text, _
                          Text3.Text, _
                          Text4.Text, _
                          Text5.Text)
                          
       Select Case success
          Case 0:    MsgBox "share created successfully!"
          Case 2118: MsgBox "share name already exists"
          Case Else: MsgBox "create error " & success
       End SelectEnd Sub
    Private Function ShareAdd(sServer As String, _
                              sSharePath As String, _
                              sShareName As String, _
                              sShareRe As String, _
                              sSharePw As String) As Long
       
       Dim dwServer   As Long
       Dim dwNetname  As Long
       Dim dwPath     As Long
       Dim dwRe   As Long
       Dim dwPw       As Long
       Dim parmerr    As Long
       Dim si2        As SHARE_INFO_2
       
      'obtain pointers to the server, share and path
       dwServer = StrPtr(sServer)
       dwNetname = StrPtr(sShareName)
       dwPath = StrPtr(sSharePath)
       
      'if the re or password specified,
      'obtain pointer to those as well
       If Len(sShareRe) > 0 Then
          dwRe = StrPtr(sShareRe)
       End If
       
       If Len(sSharePw) > 0 Then
          dwPw = StrPtr(sSharePw)
       End If
          
      'prepare the SHARE_INFO_2 structure
       With si2
          .shi2_netname = dwNetname
          .shi2_path = dwPath
          .shi2_re = dwRe
          .shi2_type = STYPE_DISKTREE
          .shi2_permissions = ACCESS_ALL
          .shi2_max_uses = -1
          .shi2_passwd = dwPw
       End With
                              
      'add the share
       ShareAdd = NetShareAdd(dwServer, _
                              2, _
                              si2, _
                              parmerr)
                              
    End Function
     
      

  2.   

    To a form add a command button (Command1) and five text boxes (Text1 - Text5). Labels are optional. Add the following to the form: --------------------------------------------------------------------------------
     
    Option Explicit
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Copyright ©1996-2002 VBnet, Randy Birch, All Rights Reserved.
    ' Some pages may also contain other copyrights by the author.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Distribution: You can freely use this code in your own
    '               applications, but you can not publish
    '               or reproduce this code on any web site,
    '               on any online service, or distribute on
    '               any media without express permission.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Private Const NERR_SUCCESS As Long = 0&'share types
    Private Const STYPE_ALL       As Long = -1  'note: my const
    Private Const STYPE_DISKTREE  As Long = 0
    Private Const STYPE_PRINTQ    As Long = 1
    Private Const STYPE_DEVICE    As Long = 2
    Private Const STYPE_IPC       As Long = 3
    Private Const STYPE_SPECIAL   As Long = &H80000000'permissions
    Private Const ACCESS_READ     As Long = &H1
    Private Const ACCESS_WRITE    As Long = &H2
    Private Const ACCESS_CREATE   As Long = &H4
    Private Const ACCESS_EXEC     As Long = &H8
    Private Const ACCESS_DELETE   As Long = &H10
    Private Const ACCESS_ATRIB    As Long = &H20
    Private Const ACCESS_PERM     As Long = &H40
    Private Const ACCESS_ALL      As Long = ACCESS_READ Or _
                                            ACCESS_WRITE Or _
                                            ACCESS_CREATE Or _
                                            ACCESS_EXEC Or _
                                            ACCESS_DELETE Or _
                                            ACCESS_ATRIB Or _
                                            ACCESS_PERMPrivate Type SHARE_INFO_2
      shi2_netname       As Long
      shi2_type          As Long
      shi2_re        As Long
      shi2_permissions   As Long
      shi2_max_uses      As Long
      shi2_current_uses  As Long
      shi2_path          As Long
      shi2_passwd        As Long
    End Type
      
    Private Declare Function NetShareAdd Lib "netapi32" _
      (ByVal servername As Long, _
       ByVal level As Long, _
       buf As Any, _
       parmerr As Long) As Long   Private Sub Form_Load()
      
       Text1.Text = "\\" & Environ$("COMPUTERNAME")
       Text2.Text = "c:\program files\adobe"
       Text3.Text = "vbnetdemo"
       Text4.Text = "VBnet demo test share"
       Text5.Text = ""
       
    End Sub
    Private Sub Command1_Click()   Dim success As Long
                   
       success = ShareAdd(Text1.Text, _
                          Text2.Text, _
                          Text3.Text, _
                          Text4.Text, _
                          Text5.Text)
                          
       Select Case success
          Case 0:    MsgBox "share created successfully!"
          Case 2118: MsgBox "share name already exists"
          Case Else: MsgBox "create error " & success
       End SelectEnd Sub
    Private Function ShareAdd(sServer As String, _
                              sSharePath As String, _
                              sShareName As String, _
                              sShareRe As String, _
                              sSharePw As String) As Long
       
       Dim dwServer   As Long
       Dim dwNetname  As Long
       Dim dwPath     As Long
       Dim dwRe   As Long
       Dim dwPw       As Long
       Dim parmerr    As Long
       Dim si2        As SHARE_INFO_2
       
      'obtain pointers to the server, share and path
       dwServer = StrPtr(sServer)
       dwNetname = StrPtr(sShareName)
       dwPath = StrPtr(sSharePath)
       
      'if the re or password specified,
      'obtain pointer to those as well
       If Len(sShareRe) > 0 Then
          dwRe = StrPtr(sShareRe)
       End If
       
       If Len(sSharePw) > 0 Then
          dwPw = StrPtr(sSharePw)
       End If
          
      'prepare the SHARE_INFO_2 structure
       With si2
          .shi2_netname = dwNetname
          .shi2_path = dwPath
          .shi2_re = dwRe
          .shi2_type = STYPE_DISKTREE
          .shi2_permissions = ACCESS_ALL
          .shi2_max_uses = -1
          .shi2_passwd = dwPw
       End With
                              
      'add the share
       ShareAdd = NetShareAdd(dwServer, _
                              2, _
                              si2, _
                              parmerr)
                              
    End Function