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
Download Visual Studio Add-In

CreateWellKnownSid (advapi32)
 
.
Summary
Creates a SID for predefined aliases

C# Signature:

[DllImport("advapi32.dll", SetLastError=true)]
static extern bool CreateWellKnownSid(
    WELL_KNOWN_SID_TYPE WellKnownSidType,
    IntPtr DomainSid,
    IntPtr DomainSid
    IntPtr pSid,
    ref uint cbSid);

VB.Net Signature:

    Private Declare Function CreateWellKnownSid Lib "advapi32.dll" ( _
    ByVal WellKnownSidType As Integer, _
    ByVal DomainSid As IntPtr, _
    ByVal pSID As IntPtr, _
    ByRef cbSid As Integer _
    ) As Boolean

User-Defined Types:

WELL_KNOWN_SID_TYPE

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

  string GetWellKnownSID(WELL_KNOWN_SID_TYPE wellKnownSidType)
  {
      IntPtr domainSid = IntPtr.Zero;
      IntPtr pSid = IntPtr.Zero;
      uint cbSid = 0;
      string sidString = string.Empty;

      NativeMethods.CreateWellKnownSid(wellKnownSidType, domainSid, pSid, ref cbSid);

      pSid = Marshal.AllocCoTaskMem(Convert.ToInt32(cbSid));
      if (NativeMethods.CreateWellKnownSid(wellKnownSidType, domainSid, pSid, ref cbSid))
      {
      NativeMethods.ConvertSidToStringSid(pSid, out sidString);
      }

      Marshal.FreeCoTaskMem(pSid);

      return sidString;
  }

VB.Net Sample Code:

    Private WinWorldSid As Integer = 1
    Private SECURITY_MAX_SID_SIZE As Integer = 68

    ' build a well-known SID for "Everyone"
    sidsize = SECURITY_MAX_SID_SIZE
    EveryoneSID = Marshal.AllocHGlobal(sidsize)
    If CreateWellKnownSid(WinWorldSid, IntPtr.Zero, EveryoneSID, sidsize) = False Then
        ret = Marshal.GetLastWin32Error()
        Throw New Win32Exception(ret)
    End If

Alternative Managed API:

Do you know one? Please contribute it!

using System.Security.Principal;

var sid = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null).ToString();

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