Oct 15 2008

Code Review FUN!

Legacy code can be fun!
How many wrongs make a right? The more the merrier?

//VB.NET
Public Function ConvertSDToStringSD(ByVal securityDescriptor() As Byte, ByVal securityInfo As Integer) As String
Dim pStringSD As IntPtr
Dim stringSDLen As Integer
Dim success As Boolean = ConvertSecurityDescriptorToStringSecurityDescriptor(securityDescriptor, 1, securityInfo, pStringSD, stringSDLen)
‘ The following ensures that the memory allocated to pStringSD by the unmanaged
‘ Win32 API is freed.
Try
If Not success Then
Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error)

End If
Return Marshal.PtrToStringAuto(pStringSD, stringSDLen)

Finally

If (pStringSD IntPtr.Zero) Then
Marshal.FreeHGlobal(pStringSD)

End If
pStringSD = IntPtr.Zero

End Try

End Function

TOM ANGRY, can you guess why?