Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

NetWkstaUserEnum (netapi32)
 
.
Summary
The NetWkstaUserEnum function lists information about all users currently logged on to the workstation. This list includes interactive, service and batch logons.

C# Signature:

    [DllImport("netapi32.dll", CharSet = CharSet.Unicode, SetLastError=true)]
    static extern int NetWkstaUserEnum(
       string servername,
       int level,
       out IntPtr bufptr,
       int prefmaxlen,
       out int entriesread,
       out int totalentries,
       ref int resume_handle);

VB Signature:

Declare Function NetWkstaUserEnum Lib "netapi32.dll" (TODO) As TODO

User-Defined Types:

See sample code for types and constants.

Alternative Managed API:

WMI can do this sort of thing (the System.Management namespace in .Net 2.0)

Notes:

If you want to find the user logged in to the workstation, consider instead a WMI query ("select UserName from Win32_ComputerSystem"), which has certain advantages (runs faster, less ambigious results, doesn't require Interop if using .Net 2.0 System.Management namespace, etc.)

Tips & Tricks:

Please add some!

Sample Code:

  class NetWorkstationUserEnum
  {
    [DllImport("netapi32.dll", CharSet = CharSet.Unicode, SetLastError=true)]
    static extern int NetWkstaUserEnum(
       string servername,
       int level,
       out IntPtr bufptr,
       int prefmaxlen,
       out int entriesread,
       out int totalentries,
       ref int resume_handle);

    [DllImport("netapi32.dll")]
    static extern int NetApiBufferFree(
       IntPtr Buffer);

    const int NERR_SUCCESS = 0;
    const int ERROR_MORE_DATA = 234;

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct WKSTA_USER_INFO_0
    {
        public string wkui0_username;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct WKSTA_USER_INFO_1
    {
        public string wkui1_username;
        public string wkui1_logon_domain;
        public string wkui1_oth_domains;
        public string wkui1_logon_server;
    }
    void ScanHost(string hostName)
    {

        IntPtr bufptr = IntPtr.Zero;
        int dwEntriesread;
        int dwTotalentries = 0;
        int dwResumehandle = 0;
        int nStatus;
        Type tWui1 = typeof(WKSTA_USER_INFO_1);
        int nStructSize = Marshal.SizeOf(tWui1);
        WKSTA_USER_INFO_1 wui1;

        //this.listView1.Items.Clear();

        do
        {
        nStatus = NetWkstaUserEnum(
          hostName,
1,           out bufptr,
          32768,
          out dwEntriesread,
          out dwTotalentries,
          ref dwResumehandle);

        //
        // If the call succeeds,
        //
        if ((nStatus == NERR_SUCCESS) | (nStatus == ERROR_MORE_DATA))
        {
            if (dwEntriesread > 0)
            {
            IntPtr pstruct = bufptr;

            //
            // Loop through the entries.
            //
            for (int i = 0; i < dwEntriesread; i++)
            {
                wui1 = (WKSTA_USER_INFO_1)Marshal.PtrToStructure(pstruct, tWui1);
                Console.WriteLine(wui1.wkui1_logon_domain + "\\" + wui1.wkui1_username);
                pstruct = (IntPtr)((int)pstruct + nStructSize);
            }
            }
            else
            {
            Console.WriteLine("A system error has occurred : " + nStatus);
            }
        }

        if (bufptr != IntPtr.Zero)
            NetApiBufferFree(bufptr);

        } while (nStatus == ERROR_MORE_DATA);
    }
    static public void Main(string[] args)
    {
        string host = Environment.MachineName;
        if (args.Length > 0)
        {
        host = args[0];
        }
        NetWorkstationUserEnum nws = new NetWorkstationUserEnum();
        nws.ScanHost(host);
    }
  }

Documentation

   MSDN Home >  MSDN Library >  Win32 and COM Development >  Network Management >  Network Management >  Network Management Reference >  Network Management Functions  

  Network Management

NetWkstaUserEnum

The NetWkstaUserEnum function lists information about all users currently logged on to the workstation. This list includes interactive, service and batch logons.

NET_API_STATUS NetWkstaUserEnum(

  LPWSTR servername,
  DWORD level,
  LPBYTE* bufptr,
  DWORD prefmaxlen,
  LPDWORD entriesread,
  LPDWORD totalentries,
  LPDWORD resumehandle

);

Parameters

servername

in Pointer to a string that specifies the DNS or NetBIOS name of the remote server on which the function is to execute. If this parameter is NULL, the local computer is used.

Windows NT: This string must begin with \\.

level

in Specifies the information level of the data. This parameter can be one of the following values. Value Meaning

0 Return the names of users currently logged on to the workstation. The bufptr parameter points to an array of WKSTA_USER_INFO_0 structures.

1 Return the names of the current users and the domains accessed by the workstation. The bufptr parameter points to an array of WKSTA_USER_INFO_1 structures.

bufptr

out Pointer to the buffer that receives the data. The format of this data depends on the value of the level parameter. This buffer is allocated by the system and must be freed using the NetApiBufferFree function. Note that you must free the buffer even if the function fails with ERROR_MORE_DATA.

prefmaxlen

in Specifies the preferred maximum length of returned data, in bytes. If you specify MAX_PREFERRED_LENGTH, the function allocates the amount of memory required for the data. If you specify another value in this parameter, it can restrict the number of bytes that the function returns. If the buffer size is insufficient to hold all entries, the function returns ERROR_MORE_DATA. For more information, see Network Management Function Buffers and Network Management Function Buffer Lengths.

entriesread

out Pointer to a value that receives the count of elements actually enumerated.

totalentries

out Pointer to a value that receives the total number of entries that could have been enumerated from the current resume position. Note that applications should consider this value only as a hint.

resumehandle

[in, out] Pointer to a value that contains a resume handle which is used to continue an existing search. The handle should be zero on the first call and left unchanged for subsequent calls. If resumehandle is NULL, no resume handle is stored.

Return Values

If the function succeeds, the return value is NERR_Success.

If the function fails, the return value can be one of the following error codes.

Return code Description

ERROR_ACCESS_DENIED The user does not have access to the requested information.

ERROR_MORE_DATA More entries are available. Specify a large enough buffer to receive all entries.

ERROR_INVALID_LEVEL The level parameter is invalid.

Remarks

Note that since the NetWkstaUserEnum function lists entries for service and batch logons, as well as for interactive logons, the function can return entries for users who have logged off a workstation. This can occur, for example, when a user calls a service that impersonates the user. In this instance, NetWkstaUserEnum returns an entry for the user until the service stops impersonating the user.

Windows Server 2003 and Windows XP: If you call this function on a domain controller that is running Active Directory, access is allowed or denied based on the ACL for the securable object. To enable anonymous access, the user Anonymous must be a member of the "Pre-Windows 2000 compatible access" group. This is because anonymous tokens do not include the Everyone group SID by default. If you call this function on a member server or workstation, all authenticated users can view the information. Anonymous access is also permitted if the RestrictAnonymous policy setting permits anonymous access. If the RestrictAnonymous policy setting does not permit anonymous access, only an administrator can successfully execute the function. Members of the Administrators, and the Server, System and Print Operator local groups can also view information. For more information about restricting anonymous access, see Security Requirements for the Network Management Functions. For more information on ACLs, ACEs, and access tokens, see Access Control Model.

Windows 2000: If you call this function on a domain controller that is running Active Directory, access is allowed or denied based on the access control list (ACL) for the securable object. The default ACL permits all authenticated users and members of the " Pre-Windows 2000 compatible access" group to view the information. By default, the "Pre-Windows 2000 compatible access" group includes Everyone as a member. This enables anonymous access to the information if the system allows anonymous access. If you call this function on a member server or workstation, all authenticated users can view the information. Anonymous access is also permitted if the RestrictAnonymous policy setting allows anonymous access.

Windows NT: Only members of the Administrators local group can successfully execute NetWkstaUserEnum function both locally and on a remote server.

To compile an application that uses this function, define the WIN32WINNT macro as 0x0400 or later. For more information,see Using the Windows Headers.

Example Code

The following code sample demonstrates how to list information about all users currently logged on to a workstation using a call to the NetWkstaUserEnum function. The sample calls NetWkstaUserEnum, specifying information level 0 ( WKSTA_USER_INFO_0). The sample loops through the entries and prints the names of the users logged on to a workstation. Finally, the code sample frees the memory allocated for the information buffer, and prints the total number of users enumerated.

#ifndef UNICODE

#define UNICODE

#endif

#include <stdio.h>

#include <assert.h>

#include <windows.h>

#include <lm.h>

int wmain(int argc, wchar_t *argv[])

{

   LPWKSTA_USER_INFO_0 pBuf = NULL;
   LPWKSTA_USER_INFO_0 pTmpBuf;
   DWORD dwLevel = 0;
   DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
   DWORD dwEntriesRead = 0;
   DWORD dwTotalEntries = 0;
   DWORD dwResumeHandle = 0;
   DWORD i;
   DWORD dwTotalCount = 0;
   NET_API_STATUS nStatus;
   LPTSTR pszServerName = NULL;

   if (argc > 2)
   {
      fwprintf(stderr, L"Usage: %s [\\\\ServerName]\n", argv[0]);
      exit(1);
   }
   // The server is not the default local computer.
   //
   if (argc == 2)
      pszServerName = argv[1];
   fwprintf(stderr, L"\nUsers currently logged on %s:\n", pszServerName);
   //
   // Call the NetWkstaUserEnum function, specifying level 0.
   //
   do // begin do
   {
      nStatus = NetWkstaUserEnum(pszServerName,
                 dwLevel,
                 (LPBYTE*)&pBuf,
                 dwPrefMaxLen,
                 &dwEntriesRead,
                 &dwTotalEntries,
                 &dwResumeHandle);
      //
      // If the call succeeds,
      //
      if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
      {
     if ((pTmpBuf = pBuf) != NULL)
     {
        //
        // Loop through the entries.
        //
        for (i = 0; (i < dwEntriesRead); i++)
        {
           assert(pTmpBuf != NULL);

           if (pTmpBuf == NULL)
           {
          //
          // Only members of the Administrators local group
          //  can successfully execute NetWkstaUserEnum
          //  locally and on a remote server.
          //
          fprintf(stderr, "An access violation has occurred\n");
          break;
           }
           //
           // Print the user logged on to the workstation.
           //
           wprintf(L"\t-- %s\n", pTmpBuf->wkui0_username);

           pTmpBuf++;
           dwTotalCount++;
        }
     }
      }
      //
      // Otherwise, indicate a system error.
      //
      else
     fprintf(stderr, "A system error has occurred: %d\n", nStatus);
      //
      // Free the allocated memory.
      //
      if (pBuf != NULL)
      {
     NetApiBufferFree(pBuf);
     pBuf = NULL;
      }
   }
   //
   // Continue to call NetWkstaUserEnum while
   //  there are more entries.
   //
   while (nStatus == ERROR_MORE_DATA); // end do
   //
   // Check again for allocated memory.
   //
   if (pBuf != NULL)
      NetApiBufferFree(pBuf);
   //
   // Print the final count of workstation users.
   //
   fprintf(stderr, "\nTotal of %d entries enumerated\n", dwTotalCount);

   return 0;

}

Requirements

Client Requires Windows Vista, Windows XP, Windows 2000 Professional, or Windows NT Workstation.

Server Requires Windows Server "Longhorn", Windows Server 2003, Windows 2000 Server, or Windows NT Server.

Header Declared in Lmwksta.h; include Lm.h.

Library Link to Netapi32.lib.

DLL Requires Netapi32.dll.

See Also

Network Management Overview, Network Management Functions, Workstation and Workstation User Functions, NetWkstaGetInfo, NetWkstaSetInfo, WKSTA_USER_INFO_0, WKSTA_USER_INFO_1

Send comments about this topic

Manage Your Profile |Legal |Contact Us |MSDN Flash Newsletter

© 2006 Microsoft Corporation. All rights reserved. Terms of Use |Trademarks |Privacy Statement

Requirements

Client Requires Windows Vista, Windows XP, Windows 2000 Professional, or Windows NT Workstation.

Server Requires Windows Server "Longhorn", Windows Server 2003, Windows 2000 Server, or Windows NT Server.

Header Declared in Lmwksta.h; include Lm.h.

Library Link to Netapi32.lib.

DLL Requires Netapi32.dll.

See Also

Network Management Overview, Network Management Functions, Workstation and Workstation User Functions, NetWkstaGetInfo, NetWkstaSetInfo, WKSTA_USER_INFO_0, WKSTA_USER_INFO_1

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions