Simple LookupAccountName Example

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've been scouring the internet for a simple example of the LookupAccountName
function to retrieve a user's SID. All of the ones that I have found are
complex and confusing. All I need is the SID. Does anyone have a SIMPLE
VB.NET (or C#) example of retrieving the SID by using the LookupAccountName
function?
 
After searching for a long time, I found an easy way to get a local user's
SID in string format. To do so in VB.NET, do the following:

1. Import the Interop Services namespace:

- Imports System.Runtime.InteropServices

2. Declare the ConvertSidToStringSid function:

- Private Declare Auto Function ConvertSidToStringSid Lib "advapi32.dll"
(ByVal bSID As IntPtr, <System.Runtime.InteropServices.In(),
System.Runtime.InteropServices.Out(),
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)> ByRef SIDString As String) As Integer

3. Call the ConvertSidToStringSid function:

- Dim strSID As String = ""
Dim intSuccess As Integer

Dim user As New
System.DirectoryServices.DirectoryEntry("WinNT://Machine/User")

Dim sidBytes As Byte() = CType(user.Properties("objectsid").Value,
Byte())
Dim sidPtr As IntPtr =
System.Runtime.InteropServices.Marshal.AllocHGlobal(sidBytes.Length)
System.Runtime.InteropServices.Marshal.Copy(sidBytes, 0, sidPtr,
sidBytes.Length)
intSuccess = ConvertSidToStringSid(sidPtr, strSID)

txtSID.Text = strSID.Trim()





I hope this helps some people. It felt like pulling teeth finding this and
I'm glad I finally found a solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top