Hi.
I have vb.net code that share a folder with permission on the network. I attached its 2 class.
but i want to use this code in vb6.
I converted it to vb6. but id does not work properly.
somebody can correct my code?
I have vb.net code that share a folder with permission on the network. I attached its 2 class.
but i want to use this code in vb6.
I converted it to vb6. but id does not work properly.
Code:
Option Explicit
Private Declare Function SetEntriesInAcl Lib "advapi32.dll" Alias "SetEntriesInAclA" (ByVal CountofExplicitEntries As Long, ea As EXPLICIT_ACCESS, ByVal OldAcl As Long, NewAcl As Long) As Long
Private Declare Function InitializeSecurityDescriptor Lib "advapi32.dll" (ByRef pSecurityDescriptor As SECURITY_DESCRIPTOR, ByVal dwRevision As Long) As Long
Private Declare Function SetSecurityDescriptorDacl Lib "advapi32.dll" (pSecurityDescriptor As SECURITY_DESCRIPTOR, ByVal bDaclPresent As Long, pDacl As Long, ByVal bDaclDefaulted As Long) As Long
Private Declare Function NetShareAdd Lib "netapi32" (ByVal servername As Long, ByVal level As Long, buf As SHARE_INFO_502, parmerr As Long) As Long
' The Security Information constants required
Private Const DACL_SECURITY_INFORMATION = 4&
' Inheritance Flags
Private Const CONTAINER_INHERIT_ACE = &H2
Private Const OBJECT_INHERIT_ACE = &H1
Private Const NO_INHERITANCE As Long = &H0
' Access control APIs, such as SetEntriesInAcl and GetExplicitEntriesFromAcl, use this
' structure to identify the account associated with the access-control or audit-control
' information in an EXPLICIT_ACCESS structure.
Private Type TRUSTEE
pMultipleTrustee As Long
MultipleTrusteeOperation As Long
TrusteeForm As Long
TrusteeType As Long
ptstrName As String
End Type
' EXPLICIT_ACCESS structure that specifies access-control information for a specified
' trustee such as access mask as well as inheritance flags
Private Type EXPLICIT_ACCESS
grfAccessPermissions As Long
grfAccessMode As Long
grfInheritance As Long
pTRUSTEE As TRUSTEE
End Type
Private Type SECURITY_DESCRIPTOR
Revision As Byte
Sbz1 As Byte
Control As Long
Owner As Long
Group As Long
Sacl As Long
Dacl As Long
End Type
Private Type SHARE_INFO_502
shi502_netname As Long
shi502_type As Long
shi502_remark As Long
shi502_permissions As Long
shi502_max_uses As Long
shi502_current_uses As Long
shi502_path As Long
shi502_passwd As Long
shi502_reserved As Long
shi502_security_descriptor As SECURITY_DESCRIPTOR
End Type
Private Enum MULTIPLE_TRUSTEE_OPERATION
NO_MULTIPLE_TRUSTEE = 0
TRUSTEE_IS_IMPERSONATE = 1
End Enum
Private Enum TRUSTEE_FORM
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
Private Enum TRUSTEE_TYPE
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
Private Enum ACCESS_MODE
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
Private Enum ACCESS_MASK
GENERIC_ALL = &H10000000
GENERIC_EXECUTE = &H20000000
GENERIC_READ = &H80000000
GENERIC_WRITE = &H40000000
STANDARD_RIGHTS_READ = 131072
End Enum
Private Const SECURITY_DESCRIPTOR_REVISION As Long = 1
Private Sub CreateShare(FullUsername As String, sServer As String, sSharePath As String, sShareName As String, sShareRemark As String, sSharePw As String)
Dim pOldDACL As Long
Dim pNewDACL As Long
Dim ea As EXPLICIT_ACCESS
Dim ExplicitAccessRule As EXPLICIT_ACCESS
Dim FullAccountName As String
FullAccountName = FullUsername
Dim Account As TRUSTEE
With Account
.MultipleTrusteeOperation = MULTIPLE_TRUSTEE_OPERATION.NO_MULTIPLE_TRUSTEE
.pMultipleTrustee = 0
.TrusteeForm = TRUSTEE_FORM.TRUSTEE_IS_NAME
.ptstrName = FullAccountName
.TrusteeType = TRUSTEE_TYPE.TRUSTEE_IS_UNKNOWN
End With
ExplicitAccessRule.grfAccessMode = ACCESS_MODE.GRANT_ACCESS
ExplicitAccessRule.grfAccessPermissions = ACCESS_MASK.GENERIC_ALL
ExplicitAccessRule.grfInheritance = NO_INHERITANCE
ExplicitAccessRule.pTRUSTEE = Account
Dim SetEntriesResult As Long
SetEntriesResult = SetEntriesInAcl(1, ExplicitAccessRule, pNewDACL, pNewDACL)
Dim SecDesc As SECURITY_DESCRIPTOR
SecDesc.Revision = SECURITY_DESCRIPTOR_REVISION
Dim DecriptorInitResult As Long
DecriptorInitResult = InitializeSecurityDescriptor(SecDesc, SECURITY_DESCRIPTOR_REVISION)
Dim SetSecurityResult As Long
SetSecurityResult = SetSecurityDescriptorDacl(SecDesc, True, pNewDACL, False)
Dim dwServer As Long
Dim dwNetname As Long
Dim dwPath As Long
Dim dwRemark 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 remark or password specified,
'obtain pointer to those as well
If Len(sShareRemark) > 0 Then
dwRemark = StrPtr(sShareRemark)
End If
If Len(sSharePw) > 0 Then
dwPw = StrPtr(sSharePw)
End If
Dim ShareInfo As SHARE_INFO_502
With ShareInfo
.shi502_netname = dwNetname
.shi502_type = STYPE_DISKTREE
.shi502_remark = dwRemark
.shi502_permissions = ACCESS_ALL
.shi502_max_uses = -1
.shi502_current_uses = 0
.shi502_path = dwPath
.shi502_passwd = 0
.shi502_reserved = 0
.shi502_security_descriptor = SecDesc
End With
Dim ParameterError As Long
Dim Result As Long
Result = NetShareAdd(dwServer, 502, ShareInfo, ParameterError)
End Sub
Private Sub Command1_Click()
CreateShare "PCName\User", "PCName", "D:\a", "Test", "", ""
End Sub
somebody can correct my code?