Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Tue Nov 19, 2019 1:06 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: LookupAccountName hard crash
PostPosted: Thu Aug 16, 2012 10:48 pm 
Offline

Joined: Thu May 13, 2010 7:27 pm
Posts: 341
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


Top
 Profile  
Reply with quote  
 Post subject: Re: LookupAccountName hard crash
PostPosted: Fri Aug 17, 2012 12:03 am 
Offline
User avatar

Joined: Sun Aug 05, 2007 10:46 am
Posts: 4931
Location: San Diego, CA
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

_________________
Dave Sisemore
iMac I7[2012], OSX Mountain Lion 10.8.3 RB2012r2.1
Note : I am not interested in any solutions that involve custom Plug-ins of any kind


Top
 Profile  
Reply with quote  
 Post subject: Re: LookupAccountName hard crash
PostPosted: Fri Aug 17, 2012 1:20 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
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".


Top
 Profile  
Reply with quote  
 Post subject: Re: LookupAccountName hard crash
PostPosted: Fri Aug 17, 2012 11:21 am 
Offline

Joined: Thu May 13, 2010 7:27 pm
Posts: 341
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: LookupAccountName hard crash
PostPosted: Fri Aug 17, 2012 12:29 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
IIRC, VB passes variables to a declare ByRef by default. RB does the opposite - passes ByVal by default. You must adjust your declares accordingly.


Top
 Profile  
Reply with quote  
 Post subject: Re: LookupAccountName hard crash
PostPosted: Fri Aug 17, 2012 3:37 pm 
Offline

Joined: Thu May 13, 2010 7:27 pm
Posts: 341
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


Top
 Profile  
Reply with quote  
 Post subject: Re: LookupAccountName hard crash
PostPosted: Fri Aug 17, 2012 4:17 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
For a SID, use a memoryblock.
Instead of the CopyByXXX calls, manipulate a memoryblock.
Use LENB to get the string length in bytes.


Top
 Profile  
Reply with quote  
 Post subject: Re: LookupAccountName hard crash
PostPosted: Fri Aug 17, 2012 4:43 pm 
Offline

Joined: Thu May 13, 2010 7:27 pm
Posts: 341
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)


Top
 Profile  
Reply with quote  
 Post subject: Re: LookupAccountName hard crash
PostPosted: Fri Aug 17, 2012 5:05 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Simply pass it as pSid. Memoryblock auto-converts to Ptr.


Top
 Profile  
Reply with quote  
 Post subject: Re: LookupAccountName hard crash
PostPosted: Fri Aug 17, 2012 5:39 pm 
Offline

Joined: Thu May 13, 2010 7:27 pm
Posts: 341
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)


Top
 Profile  
Reply with quote  
 Post subject: Re: LookupAccountName hard crash
PostPosted: Fri Aug 17, 2012 5:51 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Quote:
ByRef pSid As Ptr,

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


Top
 Profile  
Reply with quote  
 Post subject: Re: LookupAccountName hard crash
PostPosted: Fri Aug 17, 2012 5:58 pm 
Offline

Joined: Thu May 13, 2010 7:27 pm
Posts: 341
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: LookupAccountName hard crash
PostPosted: Fri Aug 17, 2012 6:10 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: LookupAccountName hard crash
PostPosted: Fri Aug 17, 2012 6:28 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Update: on closer inspection, ReferenceDomainName is an output parameter, so you should declare it as Ptr and pass it a memoryblock.


Top
 Profile  
Reply with quote  
 Post subject: Re: LookupAccountName hard crash
PostPosted: Fri Aug 17, 2012 6:40 pm 
Offline

Joined: Thu May 13, 2010 7:27 pm
Posts: 341
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group