ConvertSidToStringSid

C# Signature:

[DllImport("advapi32", CharSet=CharSet.Auto, SetLastError=true)]
static extern bool ConvertSidToStringSid(
    [MarshalAs(UnmanagedType.LPArray)] byte [] pSID, 
    out IntPtr ptrSid);

[DllImport("advapi32", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool ConvertSidToStringSid(IntPtr pSid, out string strSid);

C# Signature:

static extern bool ConvertSidToStringSid(
        IntPtr pSid, // binary SID
        out IntPtr strSid); // string SID

VB Signature:

Declare Auto Function ConvertSidToStringSid Lib "advapi32.dll" (ByVal pSID() As Byte, _
   ByRef ptrSid As IntPtr) As Boolean

Sample Code:

// C# sample
public static string GetSidString(byte[] sid)
{
   IntPtr ptrSid;
   string sidString;
   if (!ConvertSidToStringSid(sid,out ptrSid))
     throw new System.ComponentModel.Win32Exception();
   try
   {
     sidString = Marshal.PtrToStringAuto(ptrSid);
   }
   finally
   {
     LocalFree(ptrSid);
   }
   return sidString;
}

Sample Code:

Sample Code:

Sample Code:

Alternative Managed Code:

Another C# Example

A PowerShell Example

Última actualización