473,378 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Help Me - LookupAccountSid

LookupAccountSid work only first time... Why?

This is my code:

'----------------------------------------------------------------------------------------------------------------------------

Private Declare Function ConvertSidToStringSid Lib "advapi32.dll" Alias
"ConvertSidToStringSidA" (ByVal pSID As IntPtr, ByRef strSID As IntPtr) As
Integer

Declare Function ConvertStringSidToSid Lib "advapi32.dll" Alias
"ConvertStringSidToSidA" (ByVal lpBuffer As String, ByRef lpVal As IntPtr)
As Boolean

Private Declare Sub LocalFree Lib "kernel32.dll" (ByVal hMemHandle As
Integer)

Private Declare Function LookupAccountSid Lib "advapi32.dll" Alias
"LookupAccountSidA" ( _
ByVal lpSystemName As String, _
ByVal pSID As Integer, _
ByVal pName As IntPtr, _
ByRef cchName As Integer, _
ByVal pRefDomain As IntPtr, _
ByRef cchRefDomain As Integer, _
ByRef SidType As Integer) As Integer

'----------------------------------------------------------------------------------------------------------------------------

Public Function AutorityNameBySid(ByVal StrSid As String) As String
Dim pSID As IntPtr
Dim pDomain As IntPtr
Dim ccDomain As Integer
Dim pUserID As IntPtr
Dim ccUserID As Integer
Dim bVal As Integer
Dim sidType As Integer
Dim DomainName As String
Dim AccountName As String
bVal = ConvertStringSidToSid(StrSid, pSID)
If (bVal <> 0) Then
bVal = LookupAccountSid(vbNullString, pSID.ToInt32, pUserID.Zero, ccUserID,
pDomain.Zero, ccDomain, sidType)
bVal = Err.LastDllError
If (bVal = 122) Then
pDomain = Marshal.AllocHGlobal(ccDomain)
pUserID = Marshal.AllocHGlobal(ccUserID)
bVal = LookupAccountSid(vbNullString, pSID.ToInt32, pUserID, ccUserID,
pDomain, ccDomain, sidType)
Dim strBld As New StringBuilder
If (bVal <> 0) Then
strBld.AppendFormat("Target : {0}{1}Domain : {2}{1}Sam Account Name:
{3}{1}", "Local System", vbCrLf, Marshal.PtrToStringAnsi(pDomain),
Marshal.PtrToStringAnsi(pUserID))
Dim cbPtr As Integer = GetLengthSid(pSID)
Dim j As Integer
Dim hexString As New StringBuilder
For j = 0 To cbPtr - 1
hexString.AppendFormat("{0:X2}", Marshal.ReadByte(pSID, j))
Next
DomainName = Marshal.PtrToStringAnsi(pDomain)
AccountName = Marshal.PtrToStringAnsi(pUserID)
'
' Clear Variables
'
Marshal.FreeHGlobal(pDomain)
Marshal.FreeHGlobal(pUserID)
pDomain = pDomain.Zero
pUserID = pUserID.Zero

LocalFree(pSID.ToInt32)
pSID = pSID.Zero
End If
End If
End If
Return DomainName & "\" & AccountName
End Function
Nov 22 '05 #1
4 2991
What happens when you run it for a second time?
"Giovanni pepe" <cs*@msn.com> wrote in message
news:uP****************@TK2MSFTNGP14.phx.gbl...
LookupAccountSid work only first time... Why?

This is my code:

'----------------------------------------------------------------------------------------------------------------------------

Private Declare Function ConvertSidToStringSid Lib "advapi32.dll" Alias
"ConvertSidToStringSidA" (ByVal pSID As IntPtr, ByRef strSID As IntPtr) As
Integer

Declare Function ConvertStringSidToSid Lib "advapi32.dll" Alias
"ConvertStringSidToSidA" (ByVal lpBuffer As String, ByRef lpVal As IntPtr)
As Boolean

Private Declare Sub LocalFree Lib "kernel32.dll" (ByVal hMemHandle As
Integer)

Private Declare Function LookupAccountSid Lib "advapi32.dll" Alias
"LookupAccountSidA" ( _
ByVal lpSystemName As String, _
ByVal pSID As Integer, _
ByVal pName As IntPtr, _
ByRef cchName As Integer, _
ByVal pRefDomain As IntPtr, _
ByRef cchRefDomain As Integer, _
ByRef SidType As Integer) As Integer

'----------------------------------------------------------------------------------------------------------------------------

Public Function AutorityNameBySid(ByVal StrSid As String) As String
Dim pSID As IntPtr
Dim pDomain As IntPtr
Dim ccDomain As Integer
Dim pUserID As IntPtr
Dim ccUserID As Integer
Dim bVal As Integer
Dim sidType As Integer
Dim DomainName As String
Dim AccountName As String
bVal = ConvertStringSidToSid(StrSid, pSID)
If (bVal <> 0) Then
bVal = LookupAccountSid(vbNullString, pSID.ToInt32, pUserID.Zero,
ccUserID,
pDomain.Zero, ccDomain, sidType)
bVal = Err.LastDllError
If (bVal = 122) Then
pDomain = Marshal.AllocHGlobal(ccDomain)
pUserID = Marshal.AllocHGlobal(ccUserID)
bVal = LookupAccountSid(vbNullString, pSID.ToInt32, pUserID, ccUserID,
pDomain, ccDomain, sidType)
Dim strBld As New StringBuilder
If (bVal <> 0) Then
strBld.AppendFormat("Target : {0}{1}Domain : {2}{1}Sam Account Name:
{3}{1}", "Local System", vbCrLf, Marshal.PtrToStringAnsi(pDomain),
Marshal.PtrToStringAnsi(pUserID))
Dim cbPtr As Integer = GetLengthSid(pSID)
Dim j As Integer
Dim hexString As New StringBuilder
For j = 0 To cbPtr - 1
hexString.AppendFormat("{0:X2}", Marshal.ReadByte(pSID, j))
Next
DomainName = Marshal.PtrToStringAnsi(pDomain)
AccountName = Marshal.PtrToStringAnsi(pUserID)
'
' Clear Variables
'
Marshal.FreeHGlobal(pDomain)
Marshal.FreeHGlobal(pUserID)
pDomain = pDomain.Zero
pUserID = pUserID.Zero

LocalFree(pSID.ToInt32)
pSID = pSID.Zero
End If
End If
End If
Return DomainName & "\" & AccountName
End Function

Nov 22 '05 #2
Return DomainName and AccountName Empty....

Nov 22 '05 #3
You missed a line of codein the example

Declare Function GetLengthSid Lib "advapi32.dll" (ByRef pSid As Object) As
Integer

"Giovanni pepe" <cs*@msn.com> wrote in message
news:uP****************@TK2MSFTNGP14.phx.gbl...
LookupAccountSid work only first time... Why?

This is my code:

'----------------------------------------------------------------------------------------------------------------------------

Private Declare Function ConvertSidToStringSid Lib "advapi32.dll" Alias
"ConvertSidToStringSidA" (ByVal pSID As IntPtr, ByRef strSID As IntPtr) As
Integer

Declare Function ConvertStringSidToSid Lib "advapi32.dll" Alias
"ConvertStringSidToSidA" (ByVal lpBuffer As String, ByRef lpVal As IntPtr)
As Boolean

Private Declare Sub LocalFree Lib "kernel32.dll" (ByVal hMemHandle As
Integer)

Private Declare Function LookupAccountSid Lib "advapi32.dll" Alias
"LookupAccountSidA" ( _
ByVal lpSystemName As String, _
ByVal pSID As Integer, _
ByVal pName As IntPtr, _
ByRef cchName As Integer, _
ByVal pRefDomain As IntPtr, _
ByRef cchRefDomain As Integer, _
ByRef SidType As Integer) As Integer

'----------------------------------------------------------------------------------------------------------------------------

Public Function AutorityNameBySid(ByVal StrSid As String) As String
Dim pSID As IntPtr
Dim pDomain As IntPtr
Dim ccDomain As Integer
Dim pUserID As IntPtr
Dim ccUserID As Integer
Dim bVal As Integer
Dim sidType As Integer
Dim DomainName As String
Dim AccountName As String
bVal = ConvertStringSidToSid(StrSid, pSID)
If (bVal <> 0) Then
bVal = LookupAccountSid(vbNullString, pSID.ToInt32, pUserID.Zero,
ccUserID,
pDomain.Zero, ccDomain, sidType)
bVal = Err.LastDllError
If (bVal = 122) Then
pDomain = Marshal.AllocHGlobal(ccDomain)
pUserID = Marshal.AllocHGlobal(ccUserID)
bVal = LookupAccountSid(vbNullString, pSID.ToInt32, pUserID, ccUserID,
pDomain, ccDomain, sidType)
Dim strBld As New StringBuilder
If (bVal <> 0) Then
strBld.AppendFormat("Target : {0}{1}Domain : {2}{1}Sam Account Name:
{3}{1}", "Local System", vbCrLf, Marshal.PtrToStringAnsi(pDomain),
Marshal.PtrToStringAnsi(pUserID))
Dim cbPtr As Integer = GetLengthSid(pSID)
Dim j As Integer
Dim hexString As New StringBuilder
For j = 0 To cbPtr - 1
hexString.AppendFormat("{0:X2}", Marshal.ReadByte(pSID, j))
Next
DomainName = Marshal.PtrToStringAnsi(pDomain)
AccountName = Marshal.PtrToStringAnsi(pUserID)
'
' Clear Variables
'
Marshal.FreeHGlobal(pDomain)
Marshal.FreeHGlobal(pUserID)
pDomain = pDomain.Zero
pUserID = pUserID.Zero

LocalFree(pSID.ToInt32)
pSID = pSID.Zero
End If
End If
End If
Return DomainName & "\" & AccountName
End Function

Nov 22 '05 #4
You have reason, but you can help me?
You have some other example?

Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
4
by: Giovanni pepe | last post by:
LookupAccountSid work only first time... Why? This is my code: '---------------------------------------------------------------------------------------------------------------------------- ...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
4
by: AndyL | last post by:
Hi, i want to enumerate the name of the user from a SID string. So I use the LConvertStringookupAccountSID function. It looks good up to the converting the name pointer to a string. I receive a...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.