Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
getlengthsid (advapi32)
 
.
Summary
Returns the length in bytes of a valid SID

C# Signature:

[DllImport("advapi32.dll")]
static extern uint GetLengthSid(IntPtr pSid);

VB Signature

    Public Declare Function GetLengthSid Lib "advapi32.dll"( _
        pSID As System.IntPtr _
    ) As Integer

User-Defined Types:

None.

Notes:

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.

Tips & Tricks:

Please add some!

Sample Code:

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);
    }
}

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions