Using Api Function SetNamedSecurityInfo in C#

I try used with SetNamedSecurityInfo API function in C# and it's not work

the function not set the new owner to the file.

===================================

Example of my Code

public class Test

{

public Test()

{

int cbSid = 0;

string systemName = null;

string accountName = "SomeUserName";//System.Environment.UserName;

IntPtr Sid = IntPtr.Zero;

StringBuilder sb = new StringBuilder();

StringBuilder domainName = null;

string SidString = null;

int cbDomainName = 0;

int use = 0;

int retValue = LibWrap.LookupAccountName(systemName,accountName,Sid, ref cbSid,domainName,ref cbDomainName,ref use);

domainName = new StringBuilder( cbDomainName );

Sid = Marshal.AllocHGlobal( cbSid );

retValue = LibWrap.LookupAccountName(systemName,accountName,Sid, ref cbSid,domainName,ref cbDomainName,ref use);

int SE_FILEOBJECT_TYPE = 1;

int GROUP_SID=0;

int pDACL=0;

int pSACL=0;

LibWrap.ConvertSidToStringSid(Sid, ref SidString);

//bool bFlag = LibWrap.LookupAccountName(null,System.Environment.UserName,m_strSID,ref cbSID,m_strDomainName,ref cbDomainName,ref pSID);

retValue = LibWrap.SetNamedSecurityInfo(m_strFtpFileName,SE_FILEOBJECT_TYPE,Sid,ref GROUP_SID,ref pDACL,ref pSACL);

}

}

public class LibWrap

{

[ DllImport( "Advapi32.dll", CharSet=CharSet.Auto )]

public static extern int SetNamedSecurityInfo

(

[In,MarshalAs(UnmanagedType.LPTStr)] string fileName,

int ObjType,

IntPtr OWNER_SID,

ref int GROUP_SID,

ref int pDACL,

ref int pSACL);

[ DllImport( "Advapi32.dll", CharSet=CharSet.Auto )]

public static extern int LookupAccountName(

[In,MarshalAs(UnmanagedType.LPTStr)] string systemName,

[In,MarshalAs(UnmanagedType.LPTStr)] string accountName,

IntPtr Sid,

ref int cbSid,

StringBuilder domainName,

ref int cbDomainName,

ref int use);

[DllImport("advapi32", CharSet=CharSet.Auto)]

internal static extern bool ConvertSidToStringSid(IntPtr pSID,

[In,Out,MarshalAs(UnmanagedType.LPTStr)] ref string pStringSid);

}



Answer this question

Using Api Function SetNamedSecurityInfo in C#

  • Leon Mayne

    Igor Grozman wrote:

    [ DllImport( "Advapi32.dll", CharSet=CharSet.Auto )]

    public static extern int SetNamedSecurityInfo

    (

    [In,MarshalAs(UnmanagedType.LPTStr)] string fileName,

    int ObjType,

    IntPtr OWNER_SID,

    ref int GROUP_SID,

    ref int pDACL,

    ref int pSACL);

    SetNamedSecurityInfo takes seven parameters, so you're missing one (the third one of type SECURITY_INFORMATION). The last three should be IntPtrs passed by value, not ref ints.



  • search and deploy

    Mattias,

    Thank you for your help it's work now ok !!!

    Igor


  • Using Api Function SetNamedSecurityInfo in C#