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

ConvertStringSidToSid (advapi32)
 
.
Summary
The ConvertStringSidToSid function converts a string-format SID into a valid, functional SID. You can use this function to retrieve a SID that the ConvertSidToStringSid function converted to string format.

C# Signature:

[DllImport("advapi32.dll", SetLastError=true)]
static extern bool ConvertStringSidToSid(
            string StringSid,
            out IntPtr ptrSid
            );

VB Signature:

    Private Declare Auto Function ConvertSidToStringSid Lib "advapi32.dll" ( _
    ByVal Sid As IntPtr, _
    ByRef StringSid As IntPtr _
    ) As Boolean
    Private Declare Function GetLengthSid Lib "advapi32.dll" Alias "GetLengthSid" (ByVal pSID As IntPtr) As Integer

User-Defined Types:

None.

Notes:

    'BOOL ConvertSidToStringSid(
    '  PSID Sid,
    '  LPTSTR* StringSid
    ');

Tips & Tricks:

Please add some!

VB.Net Sample Code:

    Dim Sid, SidPtr As IntPtr
    Dim SidString As String

    ' convert the SID to a string
    If Environment.Version.Major = 4 Then
        SidString = ConvertSidToStringSidNT(Sid)
    Else
        If ConvertSidToStringSid(Sid, SidPtr) = False Then
        Exit Sub
        End If
        SidString = Marshal.PtrToStringAuto(SidPtr)
        Marshal.FreeHGlobal(SidPtr)
    End If

VB.Net Sample Code:

Private Function ConvertSidFromStringToByte(ByVal i_Sid As String, ByRef io_sid As Byte()) As Boolean

    '*********************************************************************
    '* Converts a sid string to a byte array
    '*********************************************************************

    Dim result As Boolean
    Dim m_IntPtr As IntPtr
    Dim res As Integer

    Try
        res = ConvertStringSidToSid(i_Sid, m_IntPtr)

        If res <> -1 Then
        Dim m_LastWin32Error As Integer = System.Runtime.InteropServices.Marshal.GetLastWin32Error()
        Dim m_LastWin32ErrorString As String = System.Runtime.InteropServices.Marshal.GetLastWin32Error.ToString
        Console.WriteLine("Error: " & m_LastWin32ErrorString)
        Else
        Dim m_SidLength As Integer = GetLengthSid(m_IntPtr) - 1     'Aint 100% sure about -1
        ReDim io_sid(m_SidLength)
        System.Runtime.InteropServices.Marshal.Copy(m_IntPtr, io_sid, 0, m_SidLength)
        result = True
        End If

    Catch ex As Exception

    Finally
        System.Runtime.InteropServices.Marshal.FreeHGlobal(m_IntPtr)
    End Try

    Return result

    End Function

Alternative Managed API:

    'typedef struct _SID_IDENTIFIER_AUTHORITY {
    '  BYTE Value[6];
    '} SID_IDENTIFIER_AUTHORITY, *PSID_IDENTIFIER_AUTHORITY;
    Structure SID_IDENTIFIER_AUTHORITY
    <MarshalAs(UnmanagedType.ByValArray, SizeConst:=6)> Dim Value() As Byte
    End Structure

    'BOOL IsValidSid(
    '  PSID pSid
    ');
    Private Declare Function IsValidSid Lib "advapi32.dll" ( _
    ByVal pSid As IntPtr _
    ) As Boolean

    'PSID_IDENTIFIER_AUTHORITY GetSidIdentifierAuthority(
    '  PSID pSid
    ');
    Private Declare Function GetSidIdentifierAuthority Lib "advapi32.dll" ( _
    ByVal pSid As IntPtr _
    ) As IntPtr

    'PUCHAR GetSidSubAuthorityCount(
    '  PSID pSid
    ');
    Private Declare Function GetSidSubAuthorityCount Lib "advapi32.dll" ( _
    ByVal pSid As IntPtr _
    ) As IntPtr

    'PDWORD GetSidSubAuthority(
    '  PSID pSid,
    '  DWORD nSubAuthority
    ');
    Private Declare Function GetSidSubAuthority Lib "advapi32.dll" ( _
    ByVal pSid As IntPtr, _
    ByVal nSubAuthority As Integer _
    ) As IntPtr

    ' a WinNT version of the ConvertSidToStringSid function
    Function ConvertSidToStringSidNT(ByVal Sid As IntPtr) As String
    Dim ans As String
    Dim psia As SID_IDENTIFIER_AUTHORITY
    Dim i, num, temp As Integer
    Dim top As Long
    Dim iptr As IntPtr

    If Not IsValidSid(Sid) Then
        Return ans
    End If

    ans = "S-1-"

    ' Get the top level authority
    iptr = GetSidIdentifierAuthority(Sid)
    psia = Marshal.PtrToStructure(iptr, GetType(SID_IDENTIFIER_AUTHORITY))

    ' How many sub authorities?
    iptr = GetSidSubAuthorityCount(Sid)
    num = Marshal.ReadInt16(iptr)

    ' hex version of the top authority?
    If psia.Value(0) <> 0 And psia.Value(1) <> 0 Then
        ans &= Hex(psia.Value(0))
        ans &= Hex(psia.Value(1)).PadLeft(2, "0")
        ans &= Hex(psia.Value(2)).PadLeft(2, "0")
        ans &= Hex(psia.Value(3)).PadLeft(2, "0")
        ans &= Hex(psia.Value(4)).PadLeft(2, "0")
        ans &= Hex(psia.Value(5)).PadLeft(2, "0")
    Else
        top = psia.Value(5)
        top += psia.Value(4) * 256
        top += psia.Value(3) * 256 * 256
        top += psia.Value(2) * 256 * 256 * 256
        ans &= top.ToString
    End If

    For i = 0 To num - 1
        iptr = GetSidSubAuthority(Sid, i)
        temp = Marshal.ReadInt32(iptr)
        ans &= "-" & temp.ToString
    Next

    Return ans
    End Function

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
Find References
Show Printable Version
Revisions