December 13, 2005

VB and loathing: in search of an SID

Seems that once every six months or so I have to brush off my VB6 knowlegde to get some work done. Have I mentioned I hate VB6. Did I mention I REALLY hate VB6 when I have to make calls to the Win32 API.

So the reason for my displeasure at this point was I need to get any domain users SID (Security ID). BTW: you can't seem to get this in .NET either, without going back to into the Win32 API -- no nice OO wrapper goodness for this one. How fun.

The particular API I am using actually requires one to be able to add a user. It is part of their security process. Lovely. So after a day of searching through the bowels of Google, I found my answer -- not all in one place. No, that would be too easy.

So, if any of you are needing a user's SID and are playing with the LookupAccountName and ConvertSidToStringSid APIs from VB6, here is some help.



Private Declare Function ConvertSidToStringSid Lib "ADVAPI32.dll" Alias "ConvertSidToStringSidA" (ByVal Sid As String, ByRef lpStringSid As Long) As Long
Private Declare Function IsValidSid Lib "ADVAPI32.dll" (ByRef pSid As Any) As Long
Private Declare Function LookupAccountName Lib "ADVAPI32.dll" Alias "LookupAccountNameA" (ByVal lpSystemName As String, ByVal lpAccountName As String, ByVal Sid As String, ByRef cbSid As Long, ByVal ReferencedDomainName As String, ByRef cbReferencedDomainName As Long, ByRef peUse As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
Private Declare Function GlobalFree Lib "kernel32.dll" (ByVal hMem As Long) As Long
Private Declare Function LocalAlloc Lib "kernel32.dll" (ByVal wFlags As Long, ByVal wBytes As Long) As Long
Private Declare Function LocalFree Lib "kernel32.dll" (ByVal hMem As Long) As Long

Private Sub Command1_Click()
MsgBox GetSid("DOMAIN\USER")
End Sub


Private Function GetSid(accountName As String) As String

Dim userName As String
Dim UserNameSize As Long
Dim Sid As String
Dim SidSize As Long
Dim Domain As String
Dim DomainSize As Long
Dim snu As Long
Dim sReturn As String

userName = String$(255, 0&)
Sid = String$(255, 0&)
Domain = String$(255, 0&)
sReturn = String$(255, 0&)

UserNameSize = 255
SidSize = 255
DomainSize = 255

Call LookupAccountName(vbNullString, accountName, Sid, SidSize, Domain, DomainSize, snu)
If IsValidSid(ByVal Sid) = 0& Then Exit Function

Call ConvertSidToStringSid(Sid, snu)
Call CopyMemory(ByVal sReturn, ByVal snu, 255)

sReturn = Left$(sReturn, InStr(vbNull, sReturn, vbNullChar, vbBinaryCompare) - vbNull)
Call GlobalFree(snu)

GetSid = sReturn
End Function

December 08, 2005

Delphi News: Thorpe gone to Google

OK, this can't be good for the Delphi community.

According to CNET (see article here), Danny Thorpe has left Borland for Google.

I dont see this as a death nel for Delphi, but it is another set of shoes that now have to be filled.