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;
nStatus = NetWkstaUserGetInfo(NULL,
dwLevel,
(LPBYTE *)&pBuf);
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);
}
}
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus);
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