Click here to Skip to main content
11,430,109 members (72,752 online)
Click here to Skip to main content

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

, 16 Apr 2006
Rate this:
Please Sign up or sign in to vote.
Get information about the currently logged-on user - Local computer
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->

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.
Value Meaning
0 Return the name of the user currently logged on to the workstation. The bufptr parameter points to a WKSTA_USER_INFO_0 structure.
1 Return 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.
1101 Return 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

Share

About the Author

Le Thanh Cong
Software Developer (Senior)
Vietnam Vietnam
Study at Hanoi University of Technology 2001-2006

Comments and Discussions

 
GeneralMy vote of 1 Pin
arman-na15-Dec-08 3:30
memberarman-na15-Dec-08 3:30 
QuestionGet Current status of Logged in User Pin
Vinay Jadia19-Nov-08 18:18
memberVinay Jadia19-Nov-08 18:18 
QuestionArticle purpose? Pin
DavidCrow25-Apr-06 4:41
memberDavidCrow25-Apr-06 4:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150428.2 | Last Updated 17 Apr 2006
Article Copyright 2006 by Le Thanh Cong
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid