Results 1 to 35 of 35

Thread: Setting permissions on shared folders?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Setting permissions on shared folders?

    Is there a .NET way of setting permissions on shared folders? All I've been able to find is suggestions to use XCACLS or SetACL

    I would have thought there was a more .NET compliant method than that!
    ManagePC - the all-in-one PC management and inventory tool

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    Do you mean share permissions or NTFS permissions? I know there are .NET methods for setting NTFS permissions (though I think they are quite complicated and hard to use successfully) but I'm not so sure about share permissions
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: Setting permissions on shared folders?

    Both actually. I need to create shared folders for users private data on a fileserver. So, I need to give them and them only access to the share and then set the NTFS permissions accordingly.
    ManagePC - the all-in-one PC management and inventory tool

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    Have you tried: http://www.lmgtfy.com/?q=vb.net+set+folder+permissions ?

    One useful link out of those results is this: http://msdn.microsoft.com/en-us/library/ms229742.aspx (an ACL is the proper name for the list of permissions that a folder has associated with it)
    and this: http://msdn.microsoft.com/query/dev1...ng-VB)&rd=true
    Last edited by chris128; May 25th, 2010 at 02:50 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: Setting permissions on shared folders?

    Thanks. I'd stumbled across the DirectorySecurity class over the weekend and got some working code to set the NTFS permissions. Now I'm just stuck on setting the share permissions.

    This, it seems, is much harder
    ManagePC - the all-in-one PC management and inventory tool

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    Ah ok, well I just put an example of setting the NTFS permissions in the codebank here http://www.vbforums.com/showthread.php?p=3808272 but I'll take a look at the share permissions now as well..
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    So are you actually looking for how to share a folder (and set the share permissions in the process) or have you already done that bit and just need to set the share permissions on an existing shared folder?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: Setting permissions on shared folders?

    I need to do the following

    1) create a folder on a file server
    2) Create a hidden share for that folder
    3) Set the NTFS permissions so that the user has Change permissions, Helpdesk has Read-Only, Admins have Full Control
    4) Set the SHARE permissions so that only the user can access the share

    1-3 are done. Ideally, I would like to set the share permissions when creating the share (which is done with WMI and the win32_share namespace) but I've not been able to figure out how to do it yet.
    ManagePC - the all-in-one PC management and inventory tool

  9. #9
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    Ah I see, well personally I dont like using WMI as its slow and I find it to be fairly unreliable, so I'm currently looking at using the NetShareAdd Win32 API to create the share and set the permissions but like you said, its not easy!

    PS I'm not sure why you want users to be the only ones in the Share permissions list, the common way of configuring share permissions is to just set Everyone = Full Control and then use the NTFS permissions to actually lock the folder down as this makes it simpler and easier to manage. For example, your helpdesk wont be able to access the files without actually being on the server that the share is on and then browsing to the files locally, wouldnt it be better if they could access the files (as read only still) from the share so they dont have to connect to the server?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Setting permissions on shared folders?

    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  11. #11
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    That is just the NTFS permissions again, not share permissions
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: Setting permissions on shared folders?

    To be honest, I'm not sure why they do it like that either. In fact, all I'm doing is migrating a VB6 app to VB.NET so that I can add some functionality. Perhaps I'll just "adapt" the share creation procedure
    ManagePC - the all-in-one PC management and inventory tool

  13. #13
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    Creating these shares via the NetShareAdd API is proving to be damn tricky... well creating them isn't so bad, but creating them and setting the permissions is quite complicated. I'll try again tonight when I get home but giving up on it for now I'm afraid
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  14. #14
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    Oh and here's the code I've got so far for creating a shared folder, in case you havent got that working yet. This works fine for just sharing the folder, but does not do anything with permissions:

    vb Code:
    1. 'At the top of your code
    2. Imports System.Runtime.InteropServices
    3.  
    4.  
    5. 'API Definitions
    6.  
    7. Const STYPE_DISKTREE As UInteger = 0
    8.  
    9. Public Enum NET_API_STATUS As Integer
    10.         NERR_Success = 0
    11.         ERROR_ACCESS_DENIED = 5
    12.         ERROR_INVALID_PARAMETER = 87
    13.         ERROR_INVALID_NAME = 123
    14.         ERROR_INVALID_LEVEL = 124
    15.         NERR_UnknownDevDir = 2116
    16.         NERR_RedirectedPath = 2117
    17.         NERR_DuplicateShare = 2118
    18.         NERR_BufTooSmall = 2123
    19. End Enum
    20.  
    21. <StructLayoutAttribute(LayoutKind.Sequential)> _
    22. Public Structure SHARE_INFO_2
    23.         '''LPWSTR->WCHAR*
    24.         <MarshalAsAttribute(UnmanagedType.LPWStr)> Public shi2_netname As String
    25.         Public shi2_type As UInteger
    26.         '''LPWSTR->WCHAR*
    27.         <MarshalAsAttribute(UnmanagedType.LPWStr)> Public shi2_remark As String
    28.         Public shi2_permissions As UInteger
    29.         Public shi2_max_uses As Integer
    30.         Public shi2_current_uses As UInteger
    31.         '''LPWSTR->WCHAR*
    32.         <MarshalAsAttribute(UnmanagedType.LPWStr)> Public shi2_path As String
    33.         '''LPWSTR->WCHAR*
    34.         <MarshalAsAttribute(UnmanagedType.LPWStr)> Public shi2_passwd As String
    35. End Structure
    36.  
    37.  
    38. <DllImportAttribute("netapi32.dll", EntryPoint:="NetShareAdd")> _
    39. Public Shared Function NetShareAdd(<InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal servername As String, ByVal level As UInteger, <InAttribute()> ByRef buf As SHARE_INFO_2, <OutAttribute()> ByRef parm_err As UInteger) As NET_API_STATUS
    40. End Function
    41.  
    42.  
    43. 'Example of using the APIs to create a share
    44. Private Sub CreateShare()
    45.         Dim ShareInfo As New SHARE_INFO_2
    46.         With ShareInfo
    47.             .shi2_netname = "TestShare" 'Set your share name here
    48.             .shi2_type = STYPE_DISKTREE
    49.             .shi2_remark = "Testing" 'Set share comment here
    50.             .shi2_permissions = 0
    51.             .shi2_max_uses = -1
    52.             .shi2_current_uses = 0
    53.             .shi2_path = "C:\TestingFolder" 'Set share path here
    54.             .shi2_passwd = Nothing
    55.         End With
    56.         Dim ParameterError As UInteger = 0
    57.         Dim Result As String = NetShareAdd(Nothing, 2, ShareInfo, ParameterError).ToString
    58.  
    59.         MessageBox.Show("Result = " & Result)
    60. End Sub
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  15. #15
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    OK well I've spent literally the last 5 hours trying to get this working and I'm so close now but just keep getting a damn "Invalid Parameter" error on the last of about 6 API calls Its basically because MS decided not to provide any proper documentation on the internal members of the SECURITY_DESCRIPTOR structure... which is fine for C++ users as they can use the declaration of SECURITY_DESCRIPTOR directly from Windows, but us .NET people need to declare the type and all of its members correctly for it to work!

    EDIT: I found the definition of the SECURITY_DESCRIPTOR structure by opening the Winnt.h C++ header file with Visual Studio unfortunately it doesnt help as its already pretty much what I was using so there must be something else I'm doing wrong, I just cant for the life of me figure out what it is and my code is now over 150 lines long... just to create one shared folder and set permissions on it
    Last edited by chris128; May 25th, 2010 at 05:13 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: Setting permissions on shared folders?

    I admire your determination! I am slowly coming to terms with the fact that I may just resort to creating a process to run XCACLS.EXE and RMTSHARE.EXE to do it all.
    ManagePC - the all-in-one PC management and inventory tool

  17. #17
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    Well I'm still working on it and I'll post the results here if I do get it working, here's what I've got currently for anyone else that wants to try and get it working or offer any suggestions all of the API calls work fine and seem to populate the relevant structures correctly and dont return any error codes, right up until the call to NetShareAdd at the end which returns ERROR_INVALID_PARAMETER and sets the parm_err argument to 501 (this is supposed to point to which member of the SHARE_INFO_502 structure is causing the invalid parameter error but there are only 10 members so 501 isnt much use!).

    vb Code:
    1. #Region "Constants"
    2.  
    3.     Const STYPE_DISKTREE As UInteger = 0
    4.     Const SECURITY_DESCRIPTOR_REVISION As UInteger = 1
    5.     Const ACL_REVISION As UInteger = 2
    6.     Const NO_INHERITANCE As UInteger = 0
    7.     Const ACCESS_READ As Integer = &H1
    8.     Const ACCESS_WRITE As Integer = &H2
    9.     Const ACCESS_CREATE As Integer = &H4
    10.     Const ACCESS_EXEC As Integer = &H8
    11.     Const ACCESS_DELETE As Integer = &H10
    12.     Const ACCESS_ATRIB As Integer = &H20
    13.     Const ACCESS_PERM As Integer = &H40
    14.     Const ACCESS_ALL As Integer = &H7F
    15. #End Region
    16.  
    17.  
    18. #Region "Enums"
    19.  
    20.     Public Enum NET_API_STATUS As Integer
    21.         NERR_Success = 0
    22.         ERROR_ACCESS_DENIED = 5
    23.         ERROR_INVALID_PARAMETER = 87
    24.         ERROR_INVALID_NAME = 123
    25.         ERROR_INVALID_LEVEL = 124
    26.         NERR_UnknownDevDir = 2116
    27.         NERR_RedirectedPath = 2117
    28.         NERR_DuplicateShare = 2118
    29.         NERR_BufTooSmall = 2123
    30.     End Enum
    31.  
    32.     Public Enum ACCESS_MODE As UInteger
    33.         NOT_USED_ACCESS = 0
    34.         GRANT_ACCESS = 1
    35.         SET_ACCESS = 2
    36.         DENY_ACCESS = 3
    37.         REVOKE_ACCESS = 4
    38.         SET_AUDIT_SUCCESS = 5
    39.         SET_AUDIT_FAILURE = 6
    40.     End Enum
    41.  
    42.     Public Enum MULTIPLE_TRUSTEE_OPERATION As UInteger
    43.         NO_MULTIPLE_TRUSTEE = 0
    44.         TRUSTEE_IS_IMPERSONATE = 1
    45.     End Enum
    46.  
    47.     Public Enum TRUSTEE_FORM As UInteger
    48.         TRUSTEE_IS_SID = 0
    49.         TRUSTEE_IS_NAME = 1
    50.         TRUSTEE_BAD_FORM = 2
    51.         TRUSTEE_IS_OBJECTS_AND_SID = 3
    52.         TRUSTEE_IS_OBJECTS_AND_NAME = 4
    53.     End Enum
    54.  
    55.     Public Enum TRUSTEE_TYPE As UInteger
    56.         TRUSTEE_IS_UNKNOWN = 0
    57.         TRUSTEE_IS_USER = 1
    58.         TRUSTEE_IS_GROUP = 2
    59.         TRUSTEE_IS_DOMAIN = 3
    60.         TRUSTEE_IS_ALIAS = 4
    61.         TRUSTEE_IS_WELL_KNOWN_GROUP = 5
    62.         TRUSTEE_IS_DELETED = 6
    63.         TRUSTEE_IS_INVALID = 7
    64.         TRUSTEE_IS_COMPUTER = 8
    65.     End Enum
    66.  
    67. #End Region
    68.  
    69.  
    70. #Region "Structures"
    71.  
    72.     <StructLayoutAttribute(LayoutKind.Sequential)> _
    73.     Public Structure SHARE_INFO_502
    74.         <MarshalAsAttribute(UnmanagedType.LPWStr)> Public shi502_netname As String
    75.         Public shi502_type As UInteger
    76.         <MarshalAsAttribute(UnmanagedType.LPWStr)> Public shi502_remark As String
    77.         Public shi502_permissions As Integer
    78.         Public shi502_max_uses As Integer
    79.         Public shi502_current_uses As Integer
    80.         <MarshalAsAttribute(UnmanagedType.LPWStr)> Public shi502_path As String
    81.         <MarshalAsAttribute(UnmanagedType.LPWStr)> Public shi502_passwd As String
    82.         Public shi502_reserved As Integer
    83.         Public shi502_security_descriptor As SECURITY_DESCRIPTOR
    84.     End Structure
    85.  
    86.     <StructLayoutAttribute(LayoutKind.Sequential)> _
    87.     Public Structure SECURITY_DESCRIPTOR
    88.         Public Revision As Byte
    89.         Public Sbz1 As Byte
    90.         Public Control As UShort
    91.         Public Owner As IntPtr
    92.         Public Group As IntPtr
    93.         Public Sacl As IntPtr
    94.         Public Dacl As IntPtr
    95.     End Structure
    96.  
    97.     <StructLayoutAttribute(LayoutKind.Sequential)> _
    98.     Public Structure ACL
    99.         Public AclRevision As Byte
    100.         Public Sbz1 As Byte
    101.         Public AclSize As UShort
    102.         Public AceCount As UShort
    103.         Public Sbz2 As UShort
    104.     End Structure
    105.  
    106.     <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, Pack:=0)> _
    107.     Public Structure EXPLICIT_ACCESS
    108.         Public grfAccessPermissions As UInteger
    109.         Public grfAccessMode As ACCESS_MODE
    110.         Public grfInheritance As UInteger
    111.         Public Trustee As TRUSTEE
    112.     End Structure
    113.  
    114.     <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, Pack:=0)> _
    115.     Public Structure TRUSTEE
    116.         Public pMultipleTrustee As UInteger
    117.         Public MultipleTrusteeOperation As MULTIPLE_TRUSTEE_OPERATION
    118.         Public TrusteeForm As TRUSTEE_FORM
    119.         Public TrusteeType As TRUSTEE_TYPE
    120.         <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPTStr)> _
    121.         Public ptstrName As String
    122.     End Structure
    123.  
    124. #End Region
    125.  
    126.  
    127. #Region "Native Methods"
    128.  
    129.     <DllImportAttribute("netapi32.dll", EntryPoint:="NetShareAdd")> _
    130.     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
    131.     End Function
    132.  
    133.     <System.Runtime.InteropServices.DllImportAttribute("advapi32.dll", EntryPoint:="InitializeSecurityDescriptor")> _
    134.     Public Shared Function InitializeSecurityDescriptor(ByRef pSecurityDescriptor As SECURITY_DESCRIPTOR, ByVal dwRevision As UInteger) As UInteger
    135.     End Function
    136.  
    137.     <System.Runtime.InteropServices.DllImportAttribute("Advapi32.dll", EntryPoint:="SetEntriesInAclW")> _
    138.     Public Shared Function SetEntriesInAcl(ByVal cCountOfExplicitEntries As Integer, _
    139.                                            <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
    140.     End Function
    141.  
    142.     <System.Runtime.InteropServices.DllImportAttribute("Advapi32.dll", EntryPoint:="BuildExplicitAccessWithNameW")> _
    143.     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)
    144.     End Sub
    145.  
    146.     <System.Runtime.InteropServices.DllImportAttribute("advapi32.dll", EntryPoint:="SetSecurityDescriptorDacl")> _
    147.     Public Shared Function SetSecurityDescriptorDacl(ByRef pSecurityDescriptor As SECURITY_DESCRIPTOR, <MarshalAsAttribute(UnmanagedType.Bool)> _
    148.                                                      ByVal bDaclPresent As Boolean, <InAttribute()> ByVal pDacl As System.IntPtr, <MarshalAsAttribute(UnmanagedType.Bool)> ByVal bDaclDefaulted As Boolean) As UInteger
    149.     End Function
    150.  
    151.     <DllImportAttribute("advapi32.dll", EntryPoint:="IsValidSecurityDescriptor")> _
    152.     Public Shared Function IsValidSecurityDesctiptor(ByRef pSecurityDescriptor As SECURITY_DESCRIPTOR) As UInteger
    153.     End Function
    154.  
    155. #End Region
    156.  
    157.  
    158. #Region "Managed Methods"
    159.  
    160.     Private Sub CreateShare(ByVal FullUsername As String)
    161.         Dim ea As EXPLICIT_ACCESS = Nothing
    162.         Dim AccountNamePtr As IntPtr = Marshal.StringToHGlobalUni(FullUsername)
    163.         BuildExplicitAccessWithName(ea, AccountNamePtr, ACCESS_READ, ACCESS_MODE.SET_ACCESS, NO_INHERITANCE)
    164.  
    165.         Dim AclPtr As IntPtr
    166.         Dim SetEntriesResult As UInteger = SetEntriesInAcl(1, ea, Nothing, AclPtr)
    167.         MessageBox.Show("SetEntries = " & SetEntriesResult)
    168.  
    169.         Dim SecDesc As SECURITY_DESCRIPTOR
    170.         Dim DecriptorInitResult As UInteger = InitializeSecurityDescriptor(SecDesc, SECURITY_DESCRIPTOR_REVISION)
    171.         MessageBox.Show("InitSecurityDescriptor = " & DecriptorInitResult)
    172.  
    173.         Dim SetSecurityResult As UInteger = SetSecurityDescriptorDacl(SecDesc, True, AclPtr, False)
    174.         MessageBox.Show("SetSecurityDescriptorDacl = " & SetSecurityResult)
    175.  
    176.         MessageBox.Show("Is Valid Descriptor = " & IsValidSecurityDesctiptor(SecDesc))
    177.  
    178.         Dim ShareInfo As New SHARE_INFO_502
    179.         With ShareInfo
    180.             .shi502_netname = "test"
    181.             .shi502_type = STYPE_DISKTREE
    182.             .shi502_remark = "Testing"
    183.             .shi502_permissions = 0
    184.             .shi502_max_uses = -1
    185.             .shi502_current_uses = 0
    186.             .shi502_path = "C:\TestingFolder"
    187.             .shi502_passwd = Nothing
    188.             .shi502_reserved = 0
    189.             .shi502_security_descriptor = SecDesc
    190.         End With
    191.  
    192.         'Dim Dacl As ACL = DirectCast(Marshal.PtrToStructure(SecDesc.Dacl, GetType(ACL)), ACL)
    193.         'Dim ShareInfoSize As Integer = Marshal.SizeOf(ShareInfo)
    194.         'Dim SharePtr As IntPtr = Marshal.AllocCoTaskMem(ShareInfoSize)
    195.         'Marshal.StructureToPtr(ShareInfo, SharePtr, False)
    196.  
    197.         Dim ParameterError As Integer = 0
    198.         Dim Result As String = NetShareAdd(Nothing, 502, ShareInfo, ParameterError).ToString
    199.  
    200.         MessageBox.Show("NetShareAdd result = " & Result & ", param error = " & ParameterError)
    201.     End Sub
    202.  
    203. #End Region
    204.  
    205.  
    206. #Region "Event Handlers"
    207.  
    208.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    209.         CreateShare(Environment.UserDomainName & "\" & Environment.UserName)
    210.     End Sub
    211.  
    212. #End Region

    The problem definitely lies somewhere with the security descriptor because if I comment out this line then the code completes successfully and shares the folder (it just doesnt set the share permissions obviously) :
    vb Code:
    1. .shi502_security_descriptor = SecDesc
    Last edited by chris128; May 26th, 2010 at 05:06 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  18. #18
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    Nearly got it working Its setting the correct user for the share permissions now... just not actually giving them the permissions I requested. Getting there!
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  19. #19
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    YAY I finally got it all working properly! Only taken 9 hours and 200 lines of code haha
    Only caveat is that it doesnt work on a 64 bit OS if the program is set to target x64 - so it works on a 32 bit OS fine and works on a 64 bit OS as long as the program using it is set to target x86 not x64. Tested on Windows 7 and Windows XP and both worked perfectly - obviously the account running the code has to have permission to create shared folders but that goes without saying.

    I'll post the full code in the codebank in a bit (and put a link here) when I've tidied it up and created a nice managed .NET method that wraps up the API functionality for others to use without having to understand the API


    EDIT: OK I've got my .NET method pretty much finished now, and I've built a class to go with it that simplifies specifying users and their permissions (the SharePermissionEntry class that you see in the code below). I'll post that along with all of the API definitions tomorrow morning but basically once you have copied and pasted all of my API definitions and my .NET method then you can simply do this to create a shared folder:
    vb Code:
    1. 'Create a list that will hold our permissions
    2. Dim PermissionsList As New List(Of SharePermissionEntry)
    3.  
    4. 'Create a new permission entry for the Everyone group and allow them Read access
    5. Dim PermEveryone As New SharePermissionEntry(String.Empty, "Everyone", SharedFolder.SharePermissions.Read, True)
    6. 'Create a new permission entry for the currently logged on user and allow them Full Control
    7. Dim PermUser As New SharePermissionEntry(Environment.UserDomainName, Environment.UserName, SharedFolder.SharePermissions.FullControl, True)
    8. 'Add the two entries declared above to the permissions list
    9. PermissionsList.Add(PermUser)
    10. PermissionsList.Add(PermEveryone)
    11.  
    12. 'Share the folder as "Test Share" and pass in the permissions list
    13. Dim Result As SharedFolder.NET_API_STATUS = _
    14. SharedFolder.ShareExistingFolder("Test Share", "This is a test share", "C:\TestFolder", PermissionsList)
    15.  
    16. If Result = SharedFolder.NET_API_STATUS.NERR_Success Then
    17.        MessageBox.Show("Share created successfully!")
    18. End If

    Last edited by chris128; May 26th, 2010 at 06:13 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: Setting permissions on shared folders?

    Wow, you really spent that long on it? I thought I was obsessive about fixing problems

    I look forward to trying this out though!

    This does seem like a rather strange thing for MS to have missed out of the .NET framework though. You would have imagined it's a fairly common thing to want to do in code.
    Last edited by Ginolard; May 27th, 2010 at 02:27 AM.
    ManagePC - the all-in-one PC management and inventory tool

  21. #21
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    Quote Originally Posted by Ginolard View Post
    Wow, you really spent that long on it? I thought I was obsessive about fixing problems
    Yeah well I spent a while trying to get it working on my own yesterday, then ended up searching google looking for a .NET example but could not find a single one anywhere on the internet that actually set the share permissions as well as creating the share... which made me more determined to get it working as it will probably help quite a few people out.

    Quote Originally Posted by Ginolard View Post
    This does seem like a rather strange thing for MS to have missed out of the .NET framework though. You would have imagined it's a fairly common thing to want to do in code.
    Well I dunno, in the grand scheme of things I dont think setting share permissions is that common compared to things like setting NTFS permissions or general file operations. At the end of the day MS had to have a deadline for the .NET framework release and I'm sure there is loads of stuff they would like to put in there but just didnt get chance as other things took priority.

    PS If anyone does find a .NET example on the internet somewhere I would be interested to see it to see how similar their implementation is to mine
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: Setting permissions on shared folders?

    I'm still somewhat wary of writing unmanaged code. Not because it doesn't work but I'm still learning how to write good MANAGED code!
    ManagePC - the all-in-one PC management and inventory tool

  23. #23
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    lol I just dont like calling windows APIs from .NET because they dont seem to follow any hard and fast rules - like in one API call that wants a String as an argument you will be able to just pass a regular .NET String, but in another you will have to get a pointer to that String and pass the pointer to the API instead, then in another you can pass a normal String as long as you decorate it with the correct Marshall attribute... etc etc. But then that makes it more of a challenge I guess :P

    I'll post the full API code (and the managed code that wraps it) in a couple of minutes, just adding comments to it all
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: Setting permissions on shared folders?

    I guess this isn't the best time to ask if the code lets you pass alternative credentials to be used for creating the share
    ManagePC - the all-in-one PC management and inventory tool

  25. #25
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    lol no there is no option to do that with the NetShareAdd API but I'm guessing you could use Impersonation in .NET before you call it - I've just never really tried to use impersonation myself so couldn't tell you exactly how.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  26. #26
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    Here we go all API definitions and my .NET method that wraps up the API functionality: http://www.vbforums.com/showthread.php?p=3809818
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  27. #27

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: Setting permissions on shared folders?

    Just trying it now. First thing I notice is that the API does not appear to support UNC names for the folder path. I suppose I could map a drive to the share first....
    ManagePC - the all-in-one PC management and inventory tool

  28. #28
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    Huh? I think you are getting something confused - you can only share a local folder. You cant share a UNC path because a UNC path is essentially what is created by a share... so you have to create a share before you even have a UNC path.

    EDIT: If you are wanting to create the share on a remote machine then there is an option for that with the NetShareAdd API - I just havent tried to get it working yet as I thought you only wanted to share a folder on the machine the code would be running on
    Last edited by chris128; May 27th, 2010 at 05:05 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  29. #29

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: Setting permissions on shared folders?

    Hence why the current tool uses RMTSHARE.EXE I guess. I need to create multiple shares per user on our file servers. It wouldn't be practical to have to run the tool on the file servers themselves!
    ManagePC - the all-in-one PC management and inventory tool

  30. #30
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    Why not? What difference does it make if you have to run a program on your PC or just remote desktop onto the file server and run a program?

    Anyway - I've changed the code now so that it works on a remote machine just tested it against a 2008 server here and it worked perfectly. Will modify the codebank post in a minute.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  31. #31

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: Setting permissions on shared folders?

    The tool I'm updating is used for creating new users. It does multiple actions (setting password, creating mailboxes etc). One of things it needs to do is create several file shares. No-one is going to use it if it means logging on to each file server in turn and running the same tool over and over again when they can use the current tool once.

    Of course, this begs the question....how does RMTShare do it?
    ManagePC - the all-in-one PC management and inventory tool

  32. #32
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    Codebank post updated with the new class definition that lets you create remote shares as well http://www.vbforums.com/showthread.p...18#post3809818
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  33. #33
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    Shall I take it from your rating of that last post that the code does what you wanted now?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  34. #34

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: Setting permissions on shared folders?

    Perfectly! Thank you ever so much. I shall have to sit down with it when I have a moment and understand it.

    I am wondering if my fear of unmanaged code can be overcome. Presumably everything one needs to do the Enums and Structures are on MSDN and then it's just a matter of wrestling with it all to get it working.
    ManagePC - the all-in-one PC management and inventory tool

  35. #35
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Setting permissions on shared folders?

    Yeah to be honest that one was a lot harder than all other APIs I have worked with. Usually its just a case of looking up the function definition in the .NET API Viewer (click the link at the top of this article titled CLRInsideOut2008_01.exe http://msdn.microsoft.com/en-us/magazine/cc164193.aspx), copying and pasting that into your code, then doing the same for any structures/enums required. Once you have got all of the definitions, I usually have a go at getting it working myself first as some APIs are fairly straight forward, but often a quick google search gives you an idea of what you should be doing.

    Some people recommend looking on www.pinvoke.net for definitions but if I'm being honest I dont think I have ever found a single example on there that actually just worked straight away... perhaps I have just been unlucky.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width