[DllImport("advapi32.dll")]
static extern uint GetLengthSid(IntPtr pSid);
Public Declare Function GetLengthSid Lib "advapi32.dll"( _
pSID As System.IntPtr _
) As Integer
None.
If pSid points to an unamanged array of bytes containing a SID, this function determines the length of the SID (by the formula: 8 + 4 times the value of the second byte), which you need to know, for example, when copying the SID into a managed byte array.
Consider also the use of CopySid instead of Marshal.Copy below. MSDN isn't clear if that is necessary or not, but better safe than sorry.
Please add some!
public static byte[] GetSidFromString(string stringSid)
{
IntPtr pSid;
if ( !ConvertStringSidToSid(stringSid, out pSid) )
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
try
{
int length = (int) GetLengthSid(pSid);
byte[] sid = new byte[length];
Marshal.Copy(pSid, sid, 0, length);
return sid;
}
finally
{
LocalFree(pSid);
}
}
Do you know one? Please contribute it!