Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than advapi32, prefix the name with the module name and a period.
Declare Auto Function ConvertSidToStringSid Lib "advapi32.dll" (ByVal pSID() As Byte, _
ByRef ptrSid As IntPtr) As Boolean
Notes:
As explained here, memory for the returned unmanaged string must be freed by calling LocalFree. Therefore, you must define the string as an IntPtr in managed code and call LocalFree yourself. Otherwise, the marshaler would call CoTaskMemFree which is not correct in this case.
'VB Sample
Public Shared Function ByteArrayToStringSid(ByRef bArray As Byte()) As String
Dim ptrSID As IntPtr = Nothing
Try
Dim sSID As String = String.Empty
If ConvertSidToStringSid(bArray, ptrSID) = True Then
'The PtrToStringXXX call here needs to match the CharSet used on your
'ConvertSidToStringSid DllImport. The default is CharSet.Ansi.
sSID = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(ptrSID)
End If
Return sSID
Finally
LocalFree(ptrSID)
End Try
End Function
'Alternative VB Sample
Public Shared Function ByteArrayToStringSid(ByRef bArray As Byte()) As String
Dim ptrSID As IntPtr = Nothing
Try
Dim sSID As String = String.Empty
If ConvertSidToStringSid(bArray, ptrSID) = True Then
sSID = System.Runtime.InteropServices.Marshal.PtrToStringAuto(ptrSID)
End If
Return sSID
Finally
System.Runtime.InteropServices.Marshal.FreeHGlobal(ptrSID)
End Try
End Function
// Get sub authority count...
int idxAuth = 0;
for (int i = 0; i < sSubAuthorityCount; i++)
{
idxAuth = 8 + i*4;
UInt32 iSubAuth = BitConverter.ToUInt32(sidBytes, idxAuth);
strSid.Append("-");
strSid.Append(iSubAuth.ToString());
}
}
catch (Exception ex)
{
Trace.TraceWarning(ex.Message);
throw;
}
return strSid.ToString();
}
Another C# Example
using System.Security.Principal;
private string ConvertSidBytesToString(byte[] sidBytes)
{
//SecurityIdentifier is defined in the System.Security.Principal namespace.
SecurityIdentifier si = new SecurityIdentifier(sidBytes, 0);
The ConvertSidToStringSid function converts a security identifier (SID) to a string format suitable for display, storage, or transmission.
9/9/2022 3:59:14 AM - -14.140.20.18
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
Frees the specified local memory object and invalidates its handle.
8/27/2008 3:05:46 PM - -151.145.238.91
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
Frees the specified local memory object and invalidates its handle.
8/27/2008 3:05:46 PM - -151.145.238.91
The CoTaskMemFree API
7/6/2013 3:17:22 PM - anonymous
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
Frees the specified local memory object and invalidates its handle.
8/27/2008 3:05:46 PM - -151.145.238.91
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).