Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Enumerate network Users, Groups and Machines using NetQueryDisplayInformation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Enumerate network Users, Groups and Machines using NetQueryDisplayInformation

    Enumerate network Users, Groups and Machines using NetQueryDisplayInformation



    Code:
    'Enumerate network Users, Groups and Machines using NetQueryDisplayInformation
     
    #COMPILE EXE '#Win 8.04#
    #DIM ALL
    #INCLUDE "Win32Api.inc"
    #INCLUDE "LmAccess.inc"
    #INCLUDE "LmWkSta.inc"
    #INCLUDE "CommCtrl.inc"
     
    %LabelDomain      = 1001
    %LabelPdc         = 1002
    %LabelUsername    = 1003
    %LabelLoggedon    = 1004
     
    %ButtonUser       = 1011
    %ButtonMachine    = 1012
    %ButtonGroup      = 1013
    %ButtonExit       = 1014
     
    %TextboxDomain    = 1021
    %TextboxPdc       = 1022
    %TextboxLoggedon  = 1023
    %TextboxUsername  = 1024
     
    %ListboxUser      = 1031
     
    %CheckboxPdc      = 1041
    %CheckboxLoggedon = 1042
     
    %Statusbar        = 1051
     
    %User_Level       = 0001
    %Machine_Level    = 0002
    %Group_Level      = 0003
     
    DECLARE FUNCTION NetApiBufferFree LIB "NETAPI32.DLL" ALIAS "NetApiBufferFree"(BYVAL pBuffer AS DWORD) AS DWORD
     
    GLOBAL hDlg AS DWORD
    '______________________________________________________________________________
     
    FUNCTION WinError$(BYVAL ErrorCode AS DWORD) AS STRING
     LOCAL pzError  AS ASCIIZ POINTER 'Max is 64K
     LOCAL ErrorLen AS DWORD
     
     ErrorLen = FormatMessage(%FORMAT_MESSAGE_FROM_SYSTEM OR %FORMAT_MESSAGE_ALLOCATE_BUFFER, _
                              BYVAL %NULL, ErrorCode, %NULL, BYVAL VARPTR(pzError), %NULL, BYVAL %NULL)
     IF ErrorLen THEN
       #IF %DEF(%PB_CC32)
       CharToOem(BYVAL pzError, BYVAL pzError) 'Console characters
       #ENDIF
       FUNCTION = "Error" & STR$(ErrorCode) & " (0x" & HEX$(ErrorCode) & ") : " & @pzError
       LocalFree(pzError)
     ELSE
       FUNCTION = "Unknown error" & STR$(ErrorCode) & " (0x" & HEX$(ErrorCode) & ")"
     END IF
     
    END FUNCTION
    '_____________________________________________________________________________
     
    FUNCTION pUnicodeToAnsi(BYVAL pUnicode AS DWORD)AS STRING
     LOCAL CharCount AS DWORD
     LOCAL sAnsi     AS STRING
     
     IF pUnicode THEN
       CharCount = LStrlenW(BYVAL pUnicode)
       sAnsi = NUL$(CharCount)
       WideCharToMultiByte(%CP_ACP, %NULL, BYVAL pUnicode, CharCount, _
                           BYVAL STRPTR(sAnsi), CharCount, BYVAL %NULL, BYVAL %NULL)
       FUNCTION = sAnsi
     END IF
     
    END FUNCTION
    '_____________________________________________________________________________
     
    FUNCTION PdcGet(BYVAL sServer AS STRING, BYVAL sDomainName AS STRING) AS STRING
     LOCAL pBuffer AS DWORD
     
     IF LEN(sServer) THEN
       IF LEFT$(sServer, 2) <> "\\" THEN
         sServer = "\\" & sServer
       END IF
       sServer = UCODE$(sServer)
     END IF
     
     sDomainName = UCODE$(sDomainName)
     
     IF NetGetDcName(BYVAL STRPTR(sServer), BYVAL STRPTR(sDomainName), pBuffer) = %ERROR_SUCCESS THEN
       FUNCTION = pUnicodeToAnsi(pBuffer)
       NetApiBufferFree(pBuffer)
     END IF
     
    END FUNCTION
    '______________________________________________________________________________
     
    FUNCTION FlagInfo(BYVAL dwFlags AS DWORD ) AS STRING
     
     dwFlags = dwFlags AND %UF_ACCOUNT_TYPE_MASK
     
     SELECT CASE dwFlags
       CASE %UF_NORMAL_ACCOUNT            : FUNCTION = "Domain user"
       CASE %UF_TEMP_DUPLICATE_ACCOUNT    : FUNCTION = "Local user"
       CASE %UF_WORKSTATION_TRUST_ACCOUNT : FUNCTION = "Workstation account"
       CASE %UF_SERVER_TRUST_ACCOUNT      : FUNCTION = "PDC/BDC account"
       CASE %UF_INTERDOMAIN_TRUST_ACCOUNT : FUNCTION = "Trusted domain account"
       CASE ELSE                          : FUNCTION = "Unkown account type"
     END SELECT
     
    END FUNCTION
    '______________________________________________________________________________
     
    SUB NetQuery(BYVAL sServer AS STRING, Level AS DWORD)
     LOCAL NetDisplayMachinePtr AS NET_DISPLAY_MACHINE POINTER
     LOCAL NetDisplayGroupPtr   AS NET_DISPLAY_GROUP POINTER
     LOCAL NetDisplayUserPtr    AS NET_DISPLAY_USER POINTER
     LOCAL SortedBuf            AS DWORD
     LOCAL NextIndex            AS DWORD
     LOCAL EntrieCount          AS DWORD
     LOCAL DomainAccountCount   AS DWORD
     LOCAL DomainGroupCount     AS DWORD
     LOCAL DomainMachineCount   AS DWORD
     LOCAL EntriesRequested     AS DWORD
     LOCAL Looper               AS LONG
     LOCAL Retval               AS LONG
     
     EntriesRequested = 99
     sServer          = UCODE$(sServer)
     
     DO
       SortedBuf = %NULL
       'NetQueryDisplayInformation returns user account, computer, or group account information.
       'Call this function to quickly enumerate account information for display in user interfaces.
       Retval = NetQueryDisplayInformation(BYVAL STRPTR(sServer), Level, NextIndex, _
                                           BYVAL EntriesRequested, %MAX_PREFERRED_LENGTH, _
                                           BYVAL VARPTR(EntrieCount), BYVAL VARPTR(SortedBuf))
       IF Retval <> %ERROR_MORE_DATA AND Retval <> %ERROR_SUCCESS THEN
         LISTBOX ADD hDlg, %ListboxUser, "NetQueryDisplayInformation failed - " & WinError$(Retval)
         EXIT DO
       END IF
     
       LISTBOX ADD hDlg, %ListboxUser, FORMAT$(EntrieCount) & " entrie" & IIF$((EntrieCount > 1), "s", "")
       LISTBOX ADD hDlg, %ListboxUser, ""
     
       SELECT CASE Level
     
         CASE %User_Level
           NetDisplayUserPtr = SortedBuf
           FOR Looper = 0 TO EntrieCount - 1
             'IF (@NetDisplayUserPtr.usri1_flags AND _NORMAL_Account) THEN
             INCR DomainAccountCount
             LISTBOX ADD hDlg, %ListboxUser, FORMAT$(DomainAccountCount, "0000") & $TAB & _
                     pUnicodeToAnsi(@NetDisplayUserPtr.usri1_name)              & $TAB & _
                     pUnicodeToAnsi(@NetDisplayUserPtr.usri1_full_name)         & $TAB & _
                     FlagInfo(@NetDisplayUserPtr.usri1_flags)
             INCR NetDisplayUserPtr
             'END IF
           NEXT
           CONTROL REDRAW hDlg, %ListboxUser
           IF EntrieCount > 0 THEN
             DECR NetDisplayUserPtr
             NextIndex = @NetDisplayUserPtr.usri1_next_index
           END IF
     
         CASE %Machine_Level
           NetDisplayMachinePtr = SortedBuf
           FOR Looper = 0 TO EntrieCount - 1
             INCR DomainMachineCount
             LISTBOX ADD hDlg, %ListboxUser, FORMAT$(DomainMachineCount, "0000") & $TAB & _
                     pUnicodeToAnsi(@NetDisplayMachinePtr.usri2_name)           & $TAB & _
                     pUnicodeToAnsi(@NetDisplayMachinePtr.usri2_comment)        & $TAB & _
                     FlagInfo(@NetDisplayMachinePtr.usri2_flags)
             INCR NetDisplayMachinePtr
           NEXT
           CONTROL REDRAW hDlg, %ListboxUser
           IF EntrieCount > 0 THEN
             DECR NetDisplayMachinePtr
             NextIndex = @NetDisplayMachinePtr.usri2_next_index
           END IF
     
         CASE %Group_Level
           NetDisplayGroupPtr = SortedBuf
           FOR Looper = 0 TO EntrieCount - 1
             INCR DomainGroupCount
             LISTBOX ADD hDlg, %ListboxUser, FORMAT$(DomainGroupCount, "0000") & $TAB & _
                     pUnicodeToAnsi(@NetDisplayGroupPtr.grpi3_name)           & $TAB & _
                     pUnicodeToAnsi(@NetDisplayGroupPtr.grpi3_comment)
             INCR NetDisplayGroupPtr
           NEXT
           CONTROL REDRAW hDlg, %ListboxUser
           IF EntrieCount > 0 THEN
             DECR NetDisplayGroupPtr
             NextIndex = @NetDisplayGroupPtr.grpi3_next_index
           END IF
     
       END SELECT
     
       NetApiBufferFree(SortedBuf)
     
     LOOP WHILE Retval = %ERROR_MORE_DATA
     
    END SUB
    '______________________________________________________________________________
     
    CALLBACK FUNCTION DlgProc() AS LONG
     LOCAL  pUserInfo   AS WKSTA_USER_INFO_1 POINTER
     LOCAL  pMinMaxInfo AS MINMAXINFO POINTER
     LOCAL  zUserName   AS ASCIIZ * %UNLEN
     STATIC sServer     AS STRING
     LOCAL  TryLoggedOn AS LONG
     LOCAL  Retval      AS LONG
     
     SELECT CASE CBMSG
     
       CASE %WM_INITDIALOG
         'NetWkstaUserGetInfo returns information about the currently logged-on user.
         'This function must be called in the context of the logged-on user.
         Retval = NetWkstaUserGetInfo(BYVAL %NULL, BYVAL 1, BYVAL VARPTR(pUserInfo))
         CONTROL SET TEXT CBHNDL, %TextboxDomain, pUnicodeToAnsi(@pUserInfo.wkui1_logon_domain)
         CONTROL SET TEXT CBHNDL, %TextboxPdc, PdcGet("", "")
         sServer = pUnicodeToAnsi(@pUserInfo.wkui1_logon_server)
         IF LEFT$(sServer, 2) <> "\\" THEN sServer = "\\" & sServer
         CONTROL SET TEXT CBHNDL, %TextboxLoggedon, sServer
     
         GetUserName zUserName, %UNLEN
         CONTROL SET TEXT CBHNDL, %TextboxUsername, zUserName
     
         DIM TabLen(0 TO 2) AS DWORD
         TabLen(0) = 25
         TabLen(1) = 130
         TabLen(2) = 330
         CONTROL SEND hDlg, %ListboxUser, %LB_SETTABSTOPS, 3, VARPTR(TabLen(0))
         CONTROL SEND hDlg, %ListboxUser, %LB_SETHORIZONTALEXTENT, 1500, 0
     
       CASE %WM_COMMAND
         SELECT CASE CBCTL
     
           CASE %ButtonUser, %ButtonGroup, %ButtonMachine
             IF CBCTLMSG = %BN_CLICKED THEN
               LISTBOX RESET CBHNDL, %ListboxUser
               sServer = ""
               CONTROL GET CHECK CBHNDL, %CheckboxLoggedon TO TryLoggedOn
               IF TryLoggedOn THEN
                 CONTROL GET TEXT CBHNDL, %TextboxLoggedon TO sServer
               ELSE
                 CONTROL GET TEXT CBHNDL, %TextboxPdc TO sServer
               END IF
               CONTROL SET TEXT CBHNDL, %StatusBar, "Please wait..."
               MOUSEPTR 11 'HourGlass
               CONTROL DISABLE CBHNDL, %ButtonUser
               CONTROL DISABLE CBHNDL, %ButtonGroup
               CONTROL DISABLE CBHNDL, %ButtonMachine
               IF LEN(sServer) THEN
                 SELECT CASE CBCTL
     
                   CASE %ButtonUser
                     NetQuery sServer, %User_Level
                     CONTROL SET TEXT CBHNDL, %StatusBar, "Domain User info"
     
                   CASE %ButtonMachine
                     NetQuery sServer, %Machine_Level
                     CONTROL SET TEXT CBHNDL, %StatusBar, "Domain Machine info"
     
                   CASE %ButtonGroup
                     NetQuery sServer, %Group_Level
                     CONTROL SET TEXT CBHNDL, %StatusBar, "Domain Group info"
     
                 END SELECT
               ELSE
                 IF TryLoggedOn THEN
                   LISTBOX ADD hDlg, %ListboxUser, "LoggedOn field is empty !"
                 ELSE
                   LISTBOX ADD hDlg, %ListboxUser, "PDC field is empty !"
                 END IF
               END IF
               CONTROL ENABLE CBHNDL, %ButtonUser
               CONTROL ENABLE CBHNDL, %ButtonGroup
               CONTROL ENABLE CBHNDL, %ButtonMachine
               MOUSEPTR 1 'Arrow
             END IF
     
           CASE %CheckboxPdc
             IF CBCTLMSG = %BN_CLICKED THEN
               CONTROL ENABLE hDlg, %TextboxPdc
               CONTROL DISABLE hDlg, %TextboxLoggedon
             END IF
     
           CASE %CheckboxLoggedon
             IF CBCTLMSG = %BN_CLICKED THEN
               CONTROL DISABLE hDlg, %TextboxPdc
               CONTROL ENABLE hDlg, %TextboxLoggedon
             END IF
     
           CASE %ButtonExit
             IF CBCTLMSG = %BN_CLICKED THEN
               DIALOG END CBHNDL, 0
             END IF
     
         END SELECT
     
       CASE %WM_SIZE
         MoveWindow(GetDlgItem(CBHNDL, %ListboxUser), 5, 145, _
                    LOWRD(CBLPARAM) - 10, HIWRD(CBLPARAM) - 170, %TRUE)
         SendMessage(GetDlgItem(CBHNDL, %StatusBar), CBMSG, CBWPARAM, CBLPARAM)
     
       CASE %WM_GETMINMAXINFO
         pMinMaxInfo = CBLPARAM
         DIALOG UNITS CBHNDL, 285, 200 TO PIXELS @pMinMaxInfo.ptMinTrackSize.x, _
                                                 @pMinMaxInfo.ptMinTrackSize.y
     END SELECT
     
    END FUNCTION
    '______________________________________________________________________________
     
    FUNCTION WINMAIN(BYVAL hInst AS DWORD, BYVAL hPrevInstance AS DWORD, _
                     BYVAL lpszCmdLine AS ASCIIZ PTR, BYVAL nCmdShow AS LONG) AS LONG
     LOCAL hIconBig   AS DWORD
     LOCAL hIconSmall AS DWORD
     
     DIALOG NEW %HWND_DESKTOP, "NetQueryDisplayInformation", , , 400, 250, _
                %WS_POPUP OR %WS_VISIBLE OR %WS_CLIPSIBLINGS OR %WS_CAPTION OR _
                %WS_SYSMENU OR %WS_THICKFRAME OR %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX OR _
                %DS_3DLOOK OR %DS_NOFAILCREATE OR %DS_SETFONT OR %DS_MODALFRAME, _
                %WS_EX_DLGMODALFRAME OR %WS_EX_CONTROLPARENT OR %WS_EX_CLIENTEDGE OR _
                %WS_EX_WINDOWEDGE TO hDlg
     
     ExtractIconEx("mstsc.exe", 1, BYVAL VARPTR(hIconBig), BYVAL VARPTR(hIconSmall), 1)
     SetClassLong(hDlg, %GCL_HICONSM, hIconSmall) 'Set an icon
     SetClassLong(hDlg, %GCL_HICON, hIconBig) 'Set an icon
     SendMessage(hDlg, %WM_SETICON, %ICON_SMALL, hIconSmall)
     SendMessage(hDlg, %WM_SETICON, %ICON_BIG, hIconBig)
     
     CONTROL ADD LABEL, hDlg, %LabelDomain, "Domain", 10, 10, 45, 15, %WS_CHILD OR %WS_VISIBLE
     
     CONTROL ADD LABEL, hDlg, %LabelPdc, "PDC", 10, 30, 45, 15, %WS_CHILD OR %WS_VISIBLE
     
     CONTROL ADD LABEL, hDlg, %LabelUsername, "UserName", 10, 70, 45, 15, %WS_CHILD OR %WS_VISIBLE
     
     CONTROL ADD LABEL, hDlg, %LabelLoggedon, "Logged on", 10, 50, 45, 15, %WS_CHILD OR %WS_VISIBLE
     
     CONTROL ADD TEXTBOX, hDlg, %TextboxDomain, "Domain", 55, 5, 140, 15, _
                 %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %ES_AUTOHSCROLL, %WS_EX_CLIENTEDGE
     
     CONTROL ADD TEXTBOX, hDlg, %TextboxPdc, "PDC", 55, 25, 140, 15, _
                 %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %ES_AUTOHSCROLL, %WS_EX_CLIENTEDGE
     CONTROL DISABLE hDlg, %TextboxPdc
     
     CONTROL ADD TEXTBOX, hDlg, %TextboxLoggedon, "Logged on", 55, 45, 140, 15, _
                 %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %ES_AUTOHSCROLL, %WS_EX_CLIENTEDGE
     
     CONTROL ADD TEXTBOX, hDlg, %TextboxUsername, "UserName", 55, 65, 140, 15, _
                 %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %ES_AUTOHSCROLL, %WS_EX_CLIENTEDGE
     
     CONTROL ADD OPTION, hDlg, %CheckboxPdc, "Use PDC", 210, 25, 60, 20
     
     CONTROL ADD OPTION, hDlg, %CheckboxLoggedon, "Use server", 210, 45, 60, 20
     
     CONTROL SET OPTION hDlg, %CheckboxLoggedon, %CheckboxPdc, %CheckboxLoggedon
     
     CONTROL ADD BUTTON, hDlg, %ButtonUser, "Get user", 330, 5, 60, 15, _
                 %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %BS_CENTER OR %BS_VCENTER
     
     CONTROL ADD BUTTON, hDlg, %ButtonMachine, "Get machine", 330, 25, 60, 15, _
                 %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %BS_CENTER OR %BS_VCENTER
     
     CONTROL ADD BUTTON, hDlg, %ButtonGroup, "Get group", 330, 45, 60, 15, _
                 %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %BS_CENTER OR %BS_VCENTER
     
     CONTROL ADD BUTTON, hDlg, %ButtonExit, "Exit", 330, 65, 60, 15, _
                 %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %BS_CENTER OR %BS_VCENTER
     
     CONTROL ADD LISTBOX, hDlg, %ListboxUser, , 5, 95, 335, 236, _
                 %WS_CHILD OR %WS_VISIBLE OR %WS_BORDER OR %WS_VSCROLL OR _
                 %WS_TABSTOP OR %LBS_NOTIFY OR %Lbs_UseTabStops OR %WS_HSCROLL _
                 OR %LBS_NOINTEGRALHEIGHT, %WS_EX_CLIENTEDGE
     
     CONTROL ADD "MsCtls_StatusBar32", hDlg, %Statusbar,"NetQueryDisplayInformation",_
                 %NULL,%NULL,%NULL,%NULL, %WS_CHILD OR %WS_CLIPSIBLINGS OR %WS_VISIBLE OR %SBS_SIZEGRIP
     
     DIALOG SHOW MODAL hDlg, CALL DlgProc
     
     DestroyIcon(hIconSmall)
     DestroyIcon(hIconBig)
     
    END FUNCTION
    '______________________________________________________________________________
    '
    Last edited by Pierre Bellisle; 5 May 2016, 04:06 PM.
Working...
X