#Region "Constants"
Const STYPE_DISKTREE As UInteger = 0
Const SECURITY_DESCRIPTOR_REVISION As UInteger = 1
Const ACL_REVISION As UInteger = 2
Const NO_INHERITANCE As UInteger = 0
Const ACCESS_READ As Integer = &H1
Const ACCESS_WRITE As Integer = &H2
Const ACCESS_CREATE As Integer = &H4
Const ACCESS_EXEC As Integer = &H8
Const ACCESS_DELETE As Integer = &H10
Const ACCESS_ATRIB As Integer = &H20
Const ACCESS_PERM As Integer = &H40
Const ACCESS_ALL As Integer = &H7F
#End Region
#Region "Enums"
Public Enum NET_API_STATUS As Integer
NERR_Success = 0
ERROR_ACCESS_DENIED = 5
ERROR_INVALID_PARAMETER = 87
ERROR_INVALID_NAME = 123
ERROR_INVALID_LEVEL = 124
NERR_UnknownDevDir = 2116
NERR_RedirectedPath = 2117
NERR_DuplicateShare = 2118
NERR_BufTooSmall = 2123
End Enum
Public Enum ACCESS_MODE As UInteger
NOT_USED_ACCESS = 0
GRANT_ACCESS = 1
SET_ACCESS = 2
DENY_ACCESS = 3
REVOKE_ACCESS = 4
SET_AUDIT_SUCCESS = 5
SET_AUDIT_FAILURE = 6
End Enum
Public Enum MULTIPLE_TRUSTEE_OPERATION As UInteger
NO_MULTIPLE_TRUSTEE = 0
TRUSTEE_IS_IMPERSONATE = 1
End Enum
Public Enum TRUSTEE_FORM As UInteger
TRUSTEE_IS_SID = 0
TRUSTEE_IS_NAME = 1
TRUSTEE_BAD_FORM = 2
TRUSTEE_IS_OBJECTS_AND_SID = 3
TRUSTEE_IS_OBJECTS_AND_NAME = 4
End Enum
Public Enum TRUSTEE_TYPE As UInteger
TRUSTEE_IS_UNKNOWN = 0
TRUSTEE_IS_USER = 1
TRUSTEE_IS_GROUP = 2
TRUSTEE_IS_DOMAIN = 3
TRUSTEE_IS_ALIAS = 4
TRUSTEE_IS_WELL_KNOWN_GROUP = 5
TRUSTEE_IS_DELETED = 6
TRUSTEE_IS_INVALID = 7
TRUSTEE_IS_COMPUTER = 8
End Enum
#End Region
#Region "Structures"
<StructLayoutAttribute(LayoutKind.Sequential)> _
Public Structure SHARE_INFO_502
<MarshalAsAttribute(UnmanagedType.LPWStr)> Public shi502_netname As String
Public shi502_type As UInteger
<MarshalAsAttribute(UnmanagedType.LPWStr)> Public shi502_remark As String
Public shi502_permissions As Integer
Public shi502_max_uses As Integer
Public shi502_current_uses As Integer
<MarshalAsAttribute(UnmanagedType.LPWStr)> Public shi502_path As String
<MarshalAsAttribute(UnmanagedType.LPWStr)> Public shi502_passwd As String
Public shi502_reserved As Integer
Public shi502_security_descriptor As SECURITY_DESCRIPTOR
End Structure
<StructLayoutAttribute(LayoutKind.Sequential)> _
Public Structure SECURITY_DESCRIPTOR
Public Revision As Byte
Public Sbz1 As Byte
Public Control As UShort
Public Owner As IntPtr
Public Group As IntPtr
Public Sacl As IntPtr
Public Dacl As IntPtr
End Structure
<StructLayoutAttribute(LayoutKind.Sequential)> _
Public Structure ACL
Public AclRevision As Byte
Public Sbz1 As Byte
Public AclSize As UShort
Public AceCount As UShort
Public Sbz2 As UShort
End Structure
<System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, Pack:=0)> _
Public Structure EXPLICIT_ACCESS
Public grfAccessPermissions As UInteger
Public grfAccessMode As ACCESS_MODE
Public grfInheritance As UInteger
Public Trustee As TRUSTEE
End Structure
<System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, Pack:=0)> _
Public Structure TRUSTEE
Public pMultipleTrustee As UInteger
Public MultipleTrusteeOperation As MULTIPLE_TRUSTEE_OPERATION
Public TrusteeForm As TRUSTEE_FORM
Public TrusteeType As TRUSTEE_TYPE
<System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPTStr)> _
Public ptstrName As String
End Structure
#End Region
#Region "Native Methods"
<DllImportAttribute("netapi32.dll", EntryPoint:="NetShareAdd")> _
Public Shared Function NetShareAdd(<InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal servername As String, ByVal level As UInteger, <InAttribute()> ByRef buf As SHARE_INFO_502, <OutAttribute()> ByRef parm_err As Integer) As NET_API_STATUS
End Function
<System.Runtime.InteropServices.DllImportAttribute("advapi32.dll", EntryPoint:="InitializeSecurityDescriptor")> _
Public Shared Function InitializeSecurityDescriptor(ByRef pSecurityDescriptor As SECURITY_DESCRIPTOR, ByVal dwRevision As UInteger) As UInteger
End Function
<System.Runtime.InteropServices.DllImportAttribute("Advapi32.dll", EntryPoint:="SetEntriesInAclW")> _
Public Shared Function SetEntriesInAcl(ByVal cCountOfExplicitEntries As Integer, _
<System.Runtime.InteropServices.InAttribute()> ByRef pListOfExplicitEntries As EXPLICIT_ACCESS, <System.Runtime.InteropServices.InAttribute()> ByVal OldAcl As System.IntPtr, ByRef NewAcl As System.IntPtr) As UInteger
End Function
<System.Runtime.InteropServices.DllImportAttribute("Advapi32.dll", EntryPoint:="BuildExplicitAccessWithNameW")> _
Public Shared Sub BuildExplicitAccessWithName(ByRef pExplicitAccess As EXPLICIT_ACCESS, <InAttribute()> ByVal pTrusteeName As IntPtr, ByVal AccessPermissions As UInteger, ByVal AccessMode As UInteger, ByVal Inheritance As UInteger)
End Sub
<System.Runtime.InteropServices.DllImportAttribute("advapi32.dll", EntryPoint:="SetSecurityDescriptorDacl")> _
Public Shared Function SetSecurityDescriptorDacl(ByRef pSecurityDescriptor As SECURITY_DESCRIPTOR, <MarshalAsAttribute(UnmanagedType.Bool)> _
ByVal bDaclPresent As Boolean, <InAttribute()> ByVal pDacl As System.IntPtr, <MarshalAsAttribute(UnmanagedType.Bool)> ByVal bDaclDefaulted As Boolean) As UInteger
End Function
<DllImportAttribute("advapi32.dll", EntryPoint:="IsValidSecurityDescriptor")> _
Public Shared Function IsValidSecurityDesctiptor(ByRef pSecurityDescriptor As SECURITY_DESCRIPTOR) As UInteger
End Function
#End Region
#Region "Managed Methods"
Private Sub CreateShare(ByVal FullUsername As String)
Dim ea As EXPLICIT_ACCESS = Nothing
Dim AccountNamePtr As IntPtr = Marshal.StringToHGlobalUni(FullUsername)
BuildExplicitAccessWithName(ea, AccountNamePtr, ACCESS_READ, ACCESS_MODE.SET_ACCESS, NO_INHERITANCE)
Dim AclPtr As IntPtr
Dim SetEntriesResult As UInteger = SetEntriesInAcl(1, ea, Nothing, AclPtr)
MessageBox.Show("SetEntries = " & SetEntriesResult)
Dim SecDesc As SECURITY_DESCRIPTOR
Dim DecriptorInitResult As UInteger = InitializeSecurityDescriptor(SecDesc, SECURITY_DESCRIPTOR_REVISION)
MessageBox.Show("InitSecurityDescriptor = " & DecriptorInitResult)
Dim SetSecurityResult As UInteger = SetSecurityDescriptorDacl(SecDesc, True, AclPtr, False)
MessageBox.Show("SetSecurityDescriptorDacl = " & SetSecurityResult)
MessageBox.Show("Is Valid Descriptor = " & IsValidSecurityDesctiptor(SecDesc))
Dim ShareInfo As New SHARE_INFO_502
With ShareInfo
.shi502_netname = "test"
.shi502_type = STYPE_DISKTREE
.shi502_remark = "Testing"
.shi502_permissions = 0
.shi502_max_uses = -1
.shi502_current_uses = 0
.shi502_path = "C:\TestingFolder"
.shi502_passwd = Nothing
.shi502_reserved = 0
.shi502_security_descriptor = SecDesc
End With
'Dim Dacl As ACL = DirectCast(Marshal.PtrToStructure(SecDesc.Dacl, GetType(ACL)), ACL)
'Dim ShareInfoSize As Integer = Marshal.SizeOf(ShareInfo)
'Dim SharePtr As IntPtr = Marshal.AllocCoTaskMem(ShareInfoSize)
'Marshal.StructureToPtr(ShareInfo, SharePtr, False)
Dim ParameterError As Integer = 0
Dim Result As String = NetShareAdd(Nothing, 502, ShareInfo, ParameterError).ToString
MessageBox.Show("NetShareAdd result = " & Result & ", param error = " & ParameterError)
End Sub
#End Region
#Region "Event Handlers"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CreateShare(Environment.UserDomainName & "\" & Environment.UserName)
End Sub
#End Region