buildexplicitaccesswithname (advapi32)
Last changed: -217.167.155.117

.
Summary
The BuildExplicitAccessWithName function initializes an EXPLICIT_ACCESS structure with data specified by the caller. The trustee is identified by a name string

C# Signature:

[DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern void BuildExplicitAccessWithName(
    ref EXPLICIT_ACCESS pExplicitAccess,
    string pTrusteeName,
    uint AccessPermissions,
    uint AccessMode,
    uint Inheritance);

VB Signature:

Declare Function BuildExplicitAccessWithName Lib "advapi32.dll" ( _
    ByRef pExplicitAccess As EXPLICIT_ACCESS, _
    ByVal pTrusteeName As String, _
    ByVal AccessPermissions As Integer, _
    ByVal AccessMode As Integer, _
    ByVal Inheritance As Integer)

User-Defined Types:

EXPLICIT_ACCESS

Notes:

BuildExplicitAccessWithName does not appear to work on x64 bit systems. Usually causes an AppCrash in compiled .net apps. Same code works when compiled as x86 app.

Tips & Tricks:

Please add some!

C# Sample Code:

Please add some!

VB.Net Sample Code:

    ' Type of Securable Object we are operating in this sample code
    Private Const SE_FILE_OBJECT = 1&

    ' The Security Information constants required
    Private Const DACL_SECURITY_INFORMATION = 4&
    Private Const SET_ACCESS = 2&

    ' Inheritance Flags
    Private Const CONTAINER_INHERIT_ACE = &H2
    Private Const OBJECT_INHERIT_ACE = &H1

    Dim pSecDesc, pOldDACL As IntPtr
    Dim ea As EXPLICIT_ACCESS
    Dim Win32Error As Win32Exception
    Dim ret, iPerm As Integer

    ' get the Security Descriptor and DACL
    ret = GetNamedSecurityInfo(strPath, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, Nothing, Nothing, pOldDACL, Nothing, pSecDesc)
    If ret <> 0 Then
        Win32Error = New Win32Exception(ret)
        Throw New Exception(Win32Error.Message)
    End If

    ' build an explicit access structure
    BuildExplicitAccessWithName(ea, strUserName, iPerm, SET_ACCESS, CONTAINER_INHERIT_ACE Or OBJECT_INHERIT_ACE)

Alternative managed API:

Do you know one? Please contribute it!

Documentation