List NT Users on Domain

List NT Users on Domain

How do I list all the users on an NT domain with Visual Basic?

The following code should get you started. Note that you need to add good error trapping to this code before using it in a production system:

 Option ExplicitPrivate Declare Function NetUserEnum Lib "netapi32" (abytServer As Byte, ByVal Level As Long, _    ByVal Flags As Long, lngBuffer As Long, ByVal MaxLen As Long, lpEntriesRead As Long, _    lpTotalEntries As Long, lpResume_Handle As Long) As LongPrivate Declare Sub CopyMem Lib "Kernel32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, _    ByVal lSize As Long)Private Declare Function lstrlenW Lib "Kernel32" (ByVal lpString As Long) As LongPrivate Declare Function NetApiBufferFree Lib "Netapi32.dll" (ByVal pBuffer As Long) As Long'-----------------------------------------------------------------' Type declarations for the info passed back from NetUserEnum.'-----------------------------------------------------------------Private Type typUserInfo_10_API    Name As Long    Comment As Long    UserComment As Long    FullName As LongEnd TypePrivate Sub Command1_Click()   Dim lngBuffer           As Long   Dim abytServer()        As Byte   Dim atypUsers()         As typUserInfo_10_API   Dim lngTotalUsers       As Long   Dim lngTotalUsersRead   As Long   Dim lngRtn              As Long   Dim lngResumeHwnd       As Long   Dim lngCurrPos          As Long   Dim strServerName       As String   Dim strName             As String   Dim strFullName         As String   Dim strComment          As String   Dim i                   As Long   Const Flags& = 0                   strServerName = Trim$(Me.Text1.Text)   If Len(strServerName) = 0 Then      abytServer = ""   Else      If InStr(1, strServerName, "\", vbTextCompare) <= 0 Then         strServerName = "\" & strServerName      Else         ' Already OK      End If      abytServer = strServerName & vbNullChar   End If          'call API to Enumerate users   If Len(strServerName) <> 0 Then      lngRtn = NetUserEnum(abytServer(0), 10, Flags, lngBuffer, _                           &H4000, lngTotalUsersRead, _                           lngTotalUsers, lngResumeHwnd)   Else      lngRtn = NetUserEnum(ByVal 0&, 10, Flags, lngBuffer, _                           &H4000, lngTotalUsersRead, _                           lngTotalUsers, lngResumeHwnd)   End If               If lngTotalUsersRead > 0 Then      ReDim atypUsers(0 To lngTotalUsersRead - 1)      CopyMem atypUsers(0), ByVal lngBuffer, Len(atypUsers(0)) * lngTotalUsersRead      i = 0               For i = 0 To lngTotalUsers - 1         strName = PointerToStringW(atypUsers(i).Name)         strFullName = PointerToStringW(atypUsers(i).FullName)         strComment = PointerToStringW(atypUsers(i).Comment)         Debug.Print strName, strFullName, strComment      Next i   End If      If lngBuffer Then      NetApiBufferFree lngBuffer   End If      End SubPublic Function PointerToStringW(lpStringW As Long) As String   Dim yBuffer()   As Byte   Dim lLen        As Long      If lpStringW Then   lLen = lstrlenW(lpStringW) * 2      If lLen Then         ReDim yBuffer(0 To (lLen - 1)) As Byte         CopyMem yBuffer(0), ByVal lpStringW, lLen         PointerToStringW = yBuffer      End If   End IfEnd Function

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes