Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

Get information about the currently logged-on user - Part 1

3.03/5 (14 votes)
16 Apr 20062 min read 1   1K  
Get information about the currently logged-on user - Local computer

Sample Image - LoggedOnUSer.jpg

Introduction

When you work in a network and you manage more users, you need know information about the currently logged-on user, in local computer and in remote computer. This article will help you get information about the currently logged-on user in local computer . At part 2 is in remote computer.

To get information about the currently logged-on user in local computerm, I use function NetWkstaUserGetInfo ().

NET_API_STATUS NetWkstaUserGetInfo(
  LPWSTR <A class=synParam onclick=showTip(this)>reserved</A>,  
  DWORD <A class=synParam onclick=showTip(this)>level</A>,      
  LPBYTE *<A class=synParam onclick=showTip(this)>bufptr</A>    
);

The NetWkstaUserGetInfo function returns information about the currently logged-on user. This function must be called in the context of the logged-on user.

Parameters

reserved
This parameter must be set to NULL.
level
[in] Specifies the information level of the data. This parameter can be one of the following values.
ValueMeaning
0Return the name of the user currently logged on to the workstation. The bufptr parameter points to a WKSTA_USER_INFO_0 structure.
1Return information about the workstation, including the name of the current user and the domains accessed by the workstation. The bufptr parameter points to a WKSTA_USER_INFO_1 structure.
1101Return domains browsed by the workstation. The bufptr parameter points to a WKSTA_USER_INFO_1101 structure.

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.

The following code sample demonstrates how to retrieve information about the currently logged-on user using a call to the NetWkstaUserGetInfo function. The sample calls NetWkstaUserGetInfo, specifying information level 1 (WKSTA_USER_INFO_1). If the call succeeds, the sample prints information about the logged-on user. Finally, the sample frees the memory allocated for the information buffer

void CLoggedonUserDlg::OnButton1() 
{
 DWORD dwLevel = 1;
 LPWKSTA_USER_INFO_1 pBuf = NULL;
 NET_API_STATUS nStatus;
 //
 // Call the NetWkstaUserGetInfo function;
 //  specify level 1.
 //
 nStatus = NetWkstaUserGetInfo(NULL,
         dwLevel,
         (LPBYTE *)&pBuf);
 //
 // If the call succeeds, print the information
 //  about the logged-on user.
 //
 if (nStatus == NERR_Success)
 {
  if (pBuf != NULL)
  {
   char strBuffer[256];
   WideCharToMultiByte(CP_ACP, 0, (unsigned short *)pBuf->wkui1_username, -1, strBuffer, 256, NULL, NULL);
   ((CStatic*)GetDlgItem(IDC_STATIC_USER))->SetWindowText(strBuffer);
   WideCharToMultiByte(CP_ACP, 0, (unsigned short *)pBuf->wkui1_logon_domain, -1, strBuffer, 256, NULL, NULL);
   ((CStatic*)GetDlgItem(IDC_STATIC_Domain))->SetWindowText(strBuffer);
   WideCharToMultiByte(CP_ACP, 0, (unsigned short *)pBuf->wkui1_oth_domains, -1, strBuffer, 256, NULL, NULL);
   ((CStatic*)GetDlgItem(IDC_STATIC_Other_Domains))->SetWindowText(strBuffer);
   WideCharToMultiByte(CP_ACP, 0, (unsigned short *)pBuf->wkui1_logon_server, -1, strBuffer, 256, NULL, NULL);
   ((CStatic*)GetDlgItem(IDC_STATIC_Logon_Server))->SetWindowText(strBuffer);
  }
 }
 // Otherwise, print the system error.
 //
 else
  fprintf(stderr, "A system error has occurred: %d\n", nStatus);
 //
 // Free the allocated memory.
 //
 if (pBuf != NULL)
  NetApiBufferFree(pBuf);
}


Note that LPWKSTA_USER_INFO_1 struct contains fields is wide-char string, so i use function WideCharToMultiByte to convert to ansi char and show to dialog

Thanks Mila025 , you help me very much

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here