如何获得当前登录的user的名称

shark_dim 2003-02-26 10:14:39
比如,我以"username"登录Windows2000,我怎样获取这个名称!
请大家指教!
...全文
51 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Behard 2003-02-28
  • 打赏
  • 举报
回复
GetUserName
The GetUserName function retrieves the user name of the current thread. This is the name of the user currently logged onto the system.

BOOL GetUserName(
LPTSTR lpBuffer, // address of name buffer
LPDWORD nSize // address of size of name buffer
);
Parameters
lpBuffer
Pointer to the buffer to receive the null-terminated string containing the user's logon name. If this buffer is not large enough to contain the entire user name, the function fails. A buffer size of (UNLEN + 1) characters will hold the maximum length user name including the terminating null character. UNLEN is defined in LMCONS.H.
nSize
Pointer to a DWORD variable that, on input, specifies the maximum size, in characters, of the buffer specified by the lpBuffer parameter. If the function succeeds, the variable receives the number of characters copied to the buffer. If the buffer is not large enough, the function fails and the variable receives the required buffer size, in characters, including the terminating null character.
Return Values
If the function succeeds, the return value is nonzero, and the variable pointed to by nSize contains the number of characters copied to the buffer specified by lpBuffer, including the terminating null character.

If the function fails, the return value is zero. To get extended error information, callGetLastError.

Remarks
If the current thread is impersonating another client, the GetUserName function returns the user name of the client that the thread is impersonating.

QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Unsupported.
Header: Declared in winbase.h.
Import Library: Use advapi32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT.

See Also
System Information Overview, System Information Functions,LookupAccountName
Lewolf 2003-02-27
  • 打赏
  • 举报
回复
GetUserName(void) 获取的是运行当前进程的用户,而不是登录用户,通常两者是相等的,但有很多时候是不相等,尤其是在Service程序中。以下代码是获取登录用户的一个例子
其中UserName 是最后一个登录本工作站的用户,LoginDomain 为登录验证的域(每登录域的时候为本机名称),HostName 为本机的主机名。

void __fastcall TLoginFrm::GetLocalInfo(void)
{
NET_API_STATUS Result;
DWORD EntriesRead;
DWORD TotalEntries;
LPBYTE Buffer;
_WKSTA_USER_INFO_1 *User;
Result = NetWkstaUserEnum(NULL,1,&Buffer,1024,
&EntriesRead ,& TotalEntries,
NULL);
User = (_WKSTA_USER_INFO_1 *)Buffer + TotalEntries - 1;
UserName = AnsiString(User->wkui1_username);
LoginDomain = AnsiString(User->wkui1_logon_domain);
Label1->Caption = "正在尝试获取系统操作权限...";
Refresh();
NetApiBufferFree((void*)Buffer);
char Host[256];
EntriesRead = 255;
gethostname(Host,EntriesRead);
HostName = AnsiString(Host);
Label2->Caption = "用户:'" + UserName + "'已经从'" + HostName +
"'工作站登录至'" + LoginDomain + "域'!";
// Refresh();
// GetServer(); Other Function For Lewolf Uses
}
csdsq 2003-02-26
  • 打赏
  • 举报
回复
这边有几本“破书”...
http://expert.csdn.net/Expert/topic/1470/1470404.xml?temp=.2743647
希望能帮上忙... :)
牧牛人软件 2003-02-26
  • 打赏
  • 举报
回复
GetUserName(void) ;

1,221

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧