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.
Tips & Tricks:
Please add some!
Sample Code:
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
LocalFree(ptrSID)
End Try
End Function
Alternative Managed API:
Do you know one? Please contribute it!
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.