Real Software Forums
http://forums.realsoftware.com/

LookupAccountName hard crash
http://forums.realsoftware.com/viewtopic.php?f=6&t=45010
Page 2 of 2

Author:  willgonz [ Mon Aug 20, 2012 8:43 pm ]
Post subject:  Re: LookupAccountName hard crash

Here is the latest code. However, I am not getting the values I'd expect.
Function Local_Convert_BIN_To_SDDL(strNTDomain As cString, strNTAccount As cString) As String

Soft Declare Function GetSidSubAuthority Lib "advapi32.dll" (pSid As ptr, byval nSubAuthority As Integer) As Integer
Soft Declare Function GetSidLengthRequired Lib "advapi32.dll" (ByVal nSubAuthorityCount As Byte) As Integer
Soft Declare Function GetSidSubAuthorityCount Lib "advapi32.dll" ( pSid As ptr) As Integer
Soft Declare Function GetSidIdentifierAuthority Lib "advapi32.dll" ( pSid As ptr) As Ptr
Soft Declare Sub CopyByValMemory Lib "kernel32" Alias "RtlMoveMemory" ( Destination As ptr,ByVal Source As Integer, ByVal Length As Integer)
Soft Declare Sub CopyByRefMemory Lib "kernel32" Alias "RtlMoveMemory" ( Destination As ptr, Source As Integer, Length As Integer)
Soft Declare Sub CopyINTMemory Lib "kernel32" Alias "RtlMoveMemory" (byref Destination As Integer, Source As Integer, Length As Integer)
Soft Declare Function LookupAccountName Lib "advapi32.dll" Alias "LookupAccountNameA" (ByRef cbSid As Integer, ReferencedDomainName As Integer, ByRef cbReferencedDomainName As Integer, ByRef peUse As Integer) As Integer
soft Declare Function LookupAccountName Lib "advapi32.dll" Alias "LookupAccountNameA" (IpSystemName As cString,IpAccountName As cString, pSid As ptr,ByRef cbSid As Integer, ReferencedDomainName As cString, ByRef cbReferencedDomainName As Integer, ByRef peUse As Integer) As Integer
' strNTDomain strNTAccount pSid(0) 512 pDomain 512 1
Dim SDDL_SID As String
Dim pSia As Integer
Dim pSiaByte As new MemoryBlock(5)'5
Dim pSid As new MemoryBlock(512)'512
Dim pSubAuthorityCount As Integer
Dim bSubAuthorityCount As new MemoryBlock(512)'(512)'512
Dim pAuthority As Integer
Dim lAuthority As Integer
dim convertDouble as Integer
dim thecbSid as Integer=512'512
dim thecbRDN as Integer=512'512
dim thepeUse as Integer = 1
Dim pDomain As String
Dim IReturn As Integer
Dim AuthCount As Integer
Dim dAuthority As Double
'bSubAuthorityCount.size=MemoryBlock.SizeUnknown
' String String MB INT INT INT INT
IReturn = LookupAccountName(strNTDomain, strNTAccount, pSid, thecbSid, pDomain, thecbRDN, thepeUse)


'pSia = GetSidIdentifierAuthority(pSid) This was before.
pSiabyte=GetSidIdentifierAuthority(pSid)

'Copy by Val Memory MemBlk , Int
' Dest Source
'CopyByValMemory pSiaByte, pSia, 6
SDDL_SID = "S-" + Trim(str(pSid.Byte(0))) + "-" + Trim(str(pSiaByte.byte(5)))
msgbox SDDL_SID
'
pSubAuthorityCount = GetSidSubAuthorityCount(pSid)

'copybyrefmemory MemBlk , Int
CopyByRefMemory bSubAuthorityCount, pSubAuthorityCount, 1

msgbox str(bSubAuthorityCount.size)
For AuthCount = 0 To bSubAuthorityCount.Size - 1
pAuthority = GetSidSubAuthority(pSid, AuthCount)

CopyINTMemory lAuthority, pAuthority, lenb(str(lAuthority))
dAuthority = lAuthority
If ((lAuthority And &H80000000) <> 0) Then

dAuthority = lAuthority And &H7FFFFFFF
dAuthority = dAuthority + 2 ^ 31

End If
SDDL_SID = SDDL_SID + "-" + Trim(Str( dAuthority))
Next AuthCount
Return SDDL_SID
End Function


Any ideas? Thanks

Author:  willgonz [ Tue Aug 21, 2012 3:58 pm ]
Post subject:  Re: LookupAccountName hard crash

Ok so I got better results when I changed:

SDDL_SID = SDDL_SID + "-" + Trim(Str( dAuthority))


to

SDDL_SID = SDDL_SID + "-" + Trim(format(dAuthority,"#" ))


Now the problem I am having is that I seem to be getting the wrong digits after,"S-1-5-21-"
I should get "2075796300" but instead I am getting, "9036".

Also, I want to have MemoryBlocks, but I don't to use the full size, just what is needed.

How do I do Unknown size for the MemoryBlocks?

Author:  timhare [ Tue Aug 21, 2012 4:14 pm ]
Post subject:  Re: LookupAccountName hard crash

Here is what I am using:
Soft Declare Function LookupAccountName Lib "advapi32.dll" Alias "LookupAccountNameA" _
(IpSystemName As cString, _
IpAccountName As cString, _
pSid As Ptr, _
ByRef cbSid As Integer, _
ReferencedDomainName As ptr, _
ByRef cbReferencedDomainName As Integer, _
ByRef peUse As Integer) As Integer

soft declare function ConvertSidToStringSid lib "advapi32.dll" alias "ConvertSidToStringSidA" _
(psid as Ptr, byref ssid as ptr) as boolean

Dim pSia As Integer
Dim pSiaByte As new MemoryBlock(5)
Dim pSid As new MemoryBlock(512)

dim thecbSid as Integer
dim thecbRDN as Integer
dim thepeUse as Integer = 1
Dim pDomain As new memoryblock(512)
Dim IReturn As Integer

dim strNTDomain, strNTAccount as string

strNTDomain = ""
strNTAccount= "Administrator"


IReturn = LookupAccountName(strNTDomain, strNTAccount,pSid, thecbSid, pDomain, thecbRDN, thepeUse)

pSid= new MemoryBlock(thecbSid)
pDomain= new MemoryBlock(thecbRDN)

IReturn = LookupAccountName(strNTDomain, strNTAccount,pSid, thecbSid, pDomain, thecbRDN, thepeUse)

dim sSid as Ptr
dim strSid as string
dim mb as MemoryBlock

call ConvertSidToStringSid(psid, ssid)
mb= ssid
strSid= mb.Cstring(0)

Author:  willgonz [ Tue Aug 21, 2012 4:29 pm ]
Post subject:  Re: LookupAccountName hard crash

Dang!! You make it look so easy. Thanks, a ton for all your help. There will be more as I start converting my old VB6 projects to RealBasic.

Page 2 of 2 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/