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

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

Author:  willgonz [ Thu Aug 16, 2012 10:48 pm ]
Post subject:  LookupAccountName hard crash

Does anyone have any idea why this keeps crashing every time it is called?

Function AccountCheck(strNTDomain As String, strNTAccount As String) As String
Soft Declare Function LookupAccountName Lib "advapi32" Alias "LookupAccountNameA" (ByVal IpSystemName As CString, ByVal IpAccountName As CString, pSid As ptr, cbSid As Integer, ByVal ReferencedDomainName As CString, cbReferencedDomainName As Integer, peUse As Integer) As integer
Dim pSiaByte As new memoryblock (5)
Dim pSid As new memoryblock(512)

Dim IReturn As Integer

IReturn = LookupAccountName(strNTDomain, strNTAccount, pSid, 512, strNTDomain, 512, 1)

End Function


Thanks

Author:  DaveS [ Fri Aug 17, 2012 12:03 am ]
Post subject:  Re: LookupAccountName hard crash

Just a guess here... but you are passing a STRING value when the argument is a CString

Internally (if I am not mistaken) a STRING begins with 2 bytes indicating length, followed by the string characters
and a CString is stringdata terminated by 0x00

Author:  timhare [ Fri Aug 17, 2012 1:20 am ]
Post subject:  Re: LookupAccountName hard crash

RB auto-converts String to either CString or WString.

You're passing 512 for cbReferencedDomainName instead of the length of strNTDomain.
cbReferencedDomainName should be passed ByRef.
cbSid should be passed ByRef.
peUse is an output and should be passed ByRef. Use a variable instead of a literal "1".

Author:  willgonz [ Fri Aug 17, 2012 11:21 am ]
Post subject:  Re: LookupAccountName hard crash

Here is the full VB6 code. I used this a long time ago (2001) with a VB6 project.
It allows you to get a SID for the Computer and User.
In VB6 I can leave the strNTDomain blank and it will still return the SID.
If you put your username in strNTAccount you get your user SID.
If you put your computername in strNTAccount you get your computer SID.
I know there is PsGetsid.exe but if you don't want to use an exe this could work great.

Public Declare Function GetSidSubAuthority Lib "advapi32.dll" (pSid As Any, ByVal nSubAuthority As Long) As Long
Public Declare Function GetSidLengthRequired Lib "advapi32.dll" (ByVal nSubAuthorityCount As Byte) As Long
Public Declare Function GetSidSubAuthorityCount Lib "advapi32.dll" (pSid As Any) As Long
Public Declare Function GetSidIdentifierAuthority Lib "advapi32.dll" (pSid As Any) As Long
Public Declare Sub CopyByValMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, ByVal Source As Long, ByVal Length As Long)
Public Declare Sub CopyByRefMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, Source As Any, ByVal Length As Long)
Public Declare Function LookupAccountName Lib "advapi32.dll" Alias "LookupAccountNameA" (ByVal IpSystemName As String, ByVal IpAccountName As String, pSid As Byte, cbSid As Long, ByVal ReferencedDomainName As String, cbReferencedDomainName As Long, peUse As Integer) As Long



Public Function Local_Convert_BIN_To_SDDL(strNTDomain As String, strNTAccount As String) As String
Dim SDDL_SID As String
Dim pSia As Long
Dim pSiaByte(5) As Byte
Dim pSid(512) As Byte
Dim pSubAuthorityCount As Long
Dim bSubAuthorityCount As Byte
Dim pAuthority As Long
Dim lAuthority As Long

Dim pDomain As Long
Dim IReturn As Long
Dim AuthCount As Long
Dim dAuthority As Double

IReturn = LookupAccountName(strNTDomain, strNTAccount, pSid(0), 512, pDomain, 512, 1)
pSia = GetSidIdentifierAuthority(pSid(0))
CopyByValMemory pSiaByte(0), pSia, 6
SDDL_SID = "S-" + LTrim(Str(pSid(0))) + "-" + LTrim(Str(pSiaByte(5)))
'
pSubAuthorityCount = GetSidSubAuthorityCount(pSid(0))
CopyByValMemory bSubAuthorityCount, pSubAuthorityCount, 1
For AuthCount = 0 To bSubAuthorityCount - 1
pAuthority = GetSidSubAuthority(pSid(0), AuthCount)
CopyByValMemory lAuthority, pAuthority, LenB(lAuthority)
dAuthority = lAuthority
If ((lAuthority And &H80000000) <> 0) Then
dAuthority = lAuthority And &H7FFFFFFF
dAuthority = dAuthority + 2 ^ 31
End If
SDDL_SID = SDDL_SID + "-" + LTrim(Str(dAuthority))
Next AuthCount
Local_Convert_BIN_To_SDDL = SDDL_SID
End Function


I'll post the RealBasic version if I figure it out and if someone else doesn't beat me to it.

Author:  timhare [ Fri Aug 17, 2012 12:29 pm ]
Post subject:  Re: LookupAccountName hard crash

IIRC, VB passes variables to a declare ByRef by default. RB does the opposite - passes ByVal by default. You must adjust your declares accordingly.

Author:  willgonz [ Fri Aug 17, 2012 3:37 pm ]
Post subject:  Re: LookupAccountName hard crash

Thanks for the tips!!
What about the Any types? I tried Variable and it didn't like it.
Also is there a better way to handle the CopyByValMemory and CopyByRefMemory calls?
Should I be using Memoryblocks instead of byte? What about ptr's? Should I used those?

Earlier you said to use the length of strNTDomain. Normally I use LEN() to do that would I use it in this case?

Thanks

Author:  timhare [ Fri Aug 17, 2012 4:17 pm ]
Post subject:  Re: LookupAccountName hard crash

For a SID, use a memoryblock.
Instead of the CopyByXXX calls, manipulate a memoryblock.
Use LENB to get the string length in bytes.

Author:  willgonz [ Fri Aug 17, 2012 4:43 pm ]
Post subject:  Re: LookupAccountName hard crash

Here is what I have so far:
Function Local_Convert_BIN_To_SDDL(strNTDomain As cString, strNTAccount As cString) As String

Soft Declare Function GetSidSubAuthority Lib "advapi32.dll" (ByRef 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" (ByRef pSid As ptr) As Integer
Soft Declare Function GetSidIdentifierAuthority Lib "advapi32.dll" (ByRef pSid As ptr) As Integer
Soft Declare Sub CopyByValMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As ptr,ByVal Source As Integer, ByVal Length As Integer)
Soft Declare Sub CopyByRefMemory Lib "kernel32" Alias "RtlMoveMemory" ( ByVal Destination As Integer,ByRef Source As ptr,ByVal Length As Integer)
Soft Declare Function LookupAccountName Lib "advapi32.dll" Alias "LookupAccountNameA" (ByVal IpSystemName As cString,ByVal IpAccountName As cString, ByRef pSid As Ptr,ByRef cbSid As Integer, ByVal ReferencedDomainName As Integer, 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)
Dim pSid As new MemoryBlock(512)
Dim pSubAuthorityCount As Integer
Dim bSubAuthorityCount As Byte
Dim pAuthority As Integer
Dim lAuthority As Integer
dim bob as byte
dim thecbSid as Integer=512
dim thecbRDN as Integer=512
dim thepeUse as Integer = 1
Dim pDomain As Integer
Dim IReturn As Integer
Dim AuthCount As Integer
Dim dAuthority As Double

IReturn = LookupAccountName(strNTDomain, strNTAccount, pSid, thecbSid, pDomain, thecbRDN, thepeUse)
pSia = GetSidIdentifierAuthority(pSid(0))
CopyByValMemory pSiaByte(0), pSia, 6
SDDL_SID = "S-" + LTrim(Str(pSid(0))) + "-" + LTrim(Str(pSiaByte(5)))
'
pSubAuthorityCount = GetSidSubAuthorityCount(pSid(0))
CopyByValMemory bSubAuthorityCount, pSubAuthorityCount, 1
For AuthCount = 0 To bSubAuthorityCount - 1
pAuthority = GetSidSubAuthority(pSid(0), AuthCount)
CopyByValMemory lAuthority, pAuthority, LenB(lAuthority)
dAuthority = lAuthority
If ((lAuthority And &H80000000) <> 0) Then
dAuthority = lAuthority And &H7FFFFFFF
dAuthority = dAuthority + 2 ^ 31
End If
SDDL_SID = SDDL_SID + "-" + LTrim(Str(dAuthority))
Next AuthCount
Local_Convert_BIN_To_SDDL = SDDL_SID
End Function


How do I get the pSid to pass? I tried pSid.ptr(0) pSid.byte(0) pSid.ptr(-1) but it errors out with either:
Code, GetSID.Local_Convert_BIN_To_SDDL, line 27, Parameters are not compatible with this function, IReturn = LookupAccountName(strNTDomain, strNTAccount, pSid, thecbSid, pDomain, thecbRDN, thepeUse)

Author:  timhare [ Fri Aug 17, 2012 5:05 pm ]
Post subject:  Re: LookupAccountName hard crash

Simply pass it as pSid. Memoryblock auto-converts to Ptr.

Author:  willgonz [ Fri Aug 17, 2012 5:39 pm ]
Post subject:  Re: LookupAccountName hard crash

It doesn't like it.
My declare is:
Soft Declare Function LookupAccountName Lib "advapi32.dll" Alias "LookupAccountNameA" (IpSystemName As cString,IpAccountName As cString,ByRef pSid As Ptr, ByRef cbSid As Integer, ReferencedDomainName As Integer, ByRef cbReferencedDomainName As Integer, ByRef peUse As Integer) As Integer


My Dims are:
Dim pSia As Integer
Dim pSiaByte As new MemoryBlock(5)
Dim pSid As new MemoryBlock(512)

dim thecbSid as Integer=512
dim thecbRDN as Integer=512
dim thepeUse as Integer = 1
Dim pDomain As Integer
Dim IReturn As Integer


The Call is:
IReturn = LookupAccountName(strNTDomain, strNTAccount,pSid, thecbSid, pDomain, thecbRDN, thepeUse)

And I still get the error:
Code, GetSID.Local_Convert_BIN_To_SDDL, line 28, Parameters are not compatible with this function, IReturn = LookupAccountName(strNTDomain, strNTAccount, pSid, thecbSid, pDomain, thecbRDN, thepeUse)

Author:  timhare [ Fri Aug 17, 2012 5:51 pm ]
Post subject:  Re: LookupAccountName hard crash

Quote:
ByRef pSid As Ptr,

This should not be ByRef. And shouldn't ReferencedDomainName be a CString?

Author:  willgonz [ Fri Aug 17, 2012 5:58 pm ]
Post subject:  Re: LookupAccountName hard crash

I am calling it with ByRef because the value of pSid has to change from the function.

And for the ReferencedDomainName part it is weird. In the VB6 code pDomain is a Long and it is called in the Function as a String.

Author:  timhare [ Fri Aug 17, 2012 6:10 pm ]
Post subject:  Re: LookupAccountName hard crash

Ptr already implies one level of dereferencing, you don't need to add another with ByRef. That results in a pointer-to-a-pointer, not a pointer to a block of memory.

Ie., "pSid as Ptr" is equivelant to "ByRef pSid as MemoryBlock"

According to MSDN, ReferencedDomainName is a string.

Author:  timhare [ Fri Aug 17, 2012 6:28 pm ]
Post subject:  Re: LookupAccountName hard crash

Update: on closer inspection, ReferenceDomainName is an output parameter, so you should declare it as Ptr and pass it a memoryblock.

Author:  willgonz [ Fri Aug 17, 2012 6:40 pm ]
Post subject:  Re: LookupAccountName hard crash

Thanks, I think we are close. Next I have to untangle this mess:

CopyByValMemory bSubAuthorityCount, pSubAuthorityCount, 1
For AuthCount = 0 To bSubAuthorityCount - 1
pAuthority = GetSidSubAuthority(pSid(0), AuthCount)
CopyByValMemory lAuthority, pAuthority, LenB(lAuthority)
dAuthority = lAuthority
If ((lAuthority And &H80000000) <> 0) Then
dAuthority = lAuthority And &H7FFFFFFF
dAuthority = dAuthority + 2 ^ 31
End If
SDDL_SID = SDDL_SID + "-" + LTrim(Str(dAuthority))
Next AuthCount

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