列举当前登陆的用户

int main(argc, argv)
  int argc;
  char *argv[];
{
  LPWKSTA_USER_INFO_0
    /*
     * This structure contains the name of the user on a specified workstation.
     */
    buffer = NULL,
    tempbuffer = NULL;

  NET_API_STATUS
    /*
     * Return code            Description
     * NERR_SUCCESS           The function call successfull.
     * ERROR_ACCESS_DENIED    The user does not have access to the requested information.
     * ERROR_MORE_DATA        More totalentries are available. Specify a large enough
     *                        buffer to receive all totalentries.
     * ERROR_INVALID_LEVEL    The level parameter is invalid.
     */
    status;

  char
    /*
     * 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.
     */
    *server = NULL,
    username[MAX_PATH],
    servername[MAX_PATH * 2];

  DWORD
    /*
     * 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.
     */
    level = 0,

    /*
     * 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
     */
    prefmaxlen = MAX_PREFERRED_LENGTH,

    /*
     * Pointer to a value that receives the count of elements actually enumerated.
     */
    entriesread = 0,

    /*
     * 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.
     */
    totalentries = 0,

    /*
     * 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 this parameter is NULL, no resume handle is stored.
     */
    resumehandles = 0,
    usercount = 0,
    i, j;

  if (argc > 2)
  {
    fprintf(stderr, "Usage: %s [\\\\ServerName]\n", argv[0]);
    return 1;
  }

  fprintf(stdout, "\nLogged users on ");

  /*
   * The server is not default of local computer.
   */
  if (argc != 2)
  {
    server = NULL;
    fprintf(stdout, "localhost:\n");
  }
  else
  {
    // The first parameter of the NetWkstaUserEnum function must be unicode format.
    MultiByteToWideChar(
      0, 0,
      argv[1],
      lstrlen(argv[1]),
      (LPWSTR)servername,
      MAX_PATH);
    server = servername;
    fprintf(stdout, "%s:\n", argv[1]);
  }

  do
  {
   /*
    * Call the NetWkstaUserEnum function, specifying level 0.
    *
    * 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.
    */
    status = NetWkstaUserEnum(
      (LPWSTR)server,
      level,
      (LPBYTE*)&buffer,
      prefmaxlen,
      &entriesread,
      &totalentries,
      &resumehandles);

    // If an error has occurred,
    if (status != NERR_SUCCESS)
      if (status != ERROR_MORE_DATA)
      {
        fprintf(stderr, "A system error has occurred: %d\n", status);
        break;
      }

    // If the call succeeds,
    if ((tempbuffer = buffer) != NULL)
    {
      for (i = 0; i < entriesread; i++)
      {
        if (tempbuffer == 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;
        }

        for (j = 0; ; j++)
          if (!tempbuffer->wkui0_username[j])
            break;

        // Emptying the username.
        RtlZeroMemory(username, MAX_PATH);

        WideCharToMultiByte(
          0, 0,
          tempbuffer->wkui0_username,
          j + 1,
          username,
          MAX_PATH,
          0, 0);

        // Print the user logged on to the workstation.
        fprintf(stdout, " %d -- %s\n", usercount + 1, username);

        tempbuffer++;
        usercount++;
      }

      // Free the allocated memory.
      NetApiBufferFree(buffer);
      buffer = NULL;
    }
  }
  /*
   * Continue to call NetWkstaUserEnum while
   *  there are more totalentries.
   */
  while (status == ERROR_MORE_DATA); // end do

  fprintf(stdout, "\n\tTotal %lu users.\n", usercount);

  // Check again for allocated memory.
  if (buffer != NULL)
    NetApiBufferFree(buffer);

  return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值