473,387 Members | 1,549 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,387 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 2993
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...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.