WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

String Manipulation

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

UDF - Convert String SID to HexAscii Sid

Keywords: 	  UDF - Convert String SID to HexAscii Sid

; sid S-1-5-21-1614895754-2146847981-1606980848-176469
; 0105000000000005150000008A5A4160ED4CF67FF094C85F55B10200
#DefineFunction byte2Hex(Dec)
   IsZero=@TRUE
   str="0123456789ABCDEF"
   hex=""
   
   for x=7 to 0 by -1
       nibble= (dec >> (x*4)) & 15
       if nibble==0 && IsZero==@TRUE then continue
       IsZero=@FALSE
       hex=strcat(hex,Strsub(str,nibble+1,1))
   next
   hex=StrFixLeft(hex,0,2)
   return(hex)
#EndFunction

#DefineFunction StringSid2HexSid(sid)
   bb=BinaryAlloc(4)
   BinaryEODSet(bb,4)
   
   dll=Strcat(DirWIndows(1),"advapi32.dll")
   kdll=Strcat(DirWIndows(1),"kernel32.dll")
   rc=DllCall(dll,long:"ConvertStringSidToSidA",lpstr:sid,lpbinary:bb)
   le=DllLastError()
   ;Message("RC = %rc%","LastError = %le%")
   if rc == @TRUE
      ptr=BinaryPeek4(bb,0)
      sidlen=DllCall(dll,long:"GetLengthSid",long:ptr)
      ;Message("SidLen",sidlen)
      sidbb=BinaryAlloc(sidlen)
   
      hexstring=""
      for xx=0 to sidlen-1
         byte=IntControl(32,ptr+xx,"BYTE",0,0)
         hex=Byte2Hex(byte)
         hexstring=Strcat(hexstring,Hex)
      next
   
      ;Cleanup
      DllCall(kdll,long:"LocalFree",long:ptr)
      ;Message(sid,hexstring)
   else
      Message("Error - bad Sid",sid) 
      return 0;
   endif
   BinaryFree(bb)
   return (hexstring)
#EndFunction

;test
sid="S-1-5-21-1614895754-2146847981-1606980848-176469"
hexsid=StringSid2HexSid(sid)
Message(sid,hexsid)



exit



Article ID:   W15762
File Created: 2003:05:13:11:29:58
Last Updated: 2003:05:13:11:29:58