请问windows下怎样获取当前用户名?

greatll 2001-05-24 04:24:00
多谢!
...全文
1193 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyswatter 2001-05-25
  • 打赏
  • 举报
回复
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


greatll 2001-05-25
  • 打赏
  • 举报
回复
注册表中HKEY_LOCAL_MACHINE下没有Network键啊(至少2000没有)。
yangmajituipo 2001-05-25
  • 打赏
  • 举报
回复
是sdk中的,再看看
greatll 2001-05-25
  • 打赏
  • 举报
回复
CDaoWorkspace::GetUserName?是CString类型的啊。
MFC中好像没有上面几位大虾说的BOOL GetUserName吧?
Gladiator 2001-05-25
  • 打赏
  • 举报
回复
//得到当前用户
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>

#define UULEN 256
int main(int argc,char *argv[])
{
printf("Whoami tell who are you in nt/2000, -- bingle\n");
//OpenProcess();
HANDLE hp , htoken;
char buff[1024];
unsigned long size = 1024;

TOKEN_USER *tuser;
PSID sid;
char user[UULEN], domain[UULEN];
SID_NAME_USE snu;

hp = htoken = INVALID_HANDLE_VALUE;
hp = GetCurrentProcess();
if(!OpenProcessToken(hp, TOKEN_QUERY, &htoken))
{
printf("OpenProcessToken error : %u\r\n", GetLastError());
goto exit_main;
}
if(!GetTokenInformation(htoken, TokenUser, (void*)buff, size, &size))
{
printf("GetTokenInformation error : %u\r\n", GetLastError());
goto exit_main;
}
tuser = (TOKEN_USER*)buff;
sid = tuser->User.Sid;
size = UULEN;
if(!LookupAccountSid(NULL, sid, user, &size, domain, &size, &snu))
{
printf("LookupAccountSid error : %u\r\n", GetLastError());
goto exit_main;
}
printf("you are '%s\\%s'\r\n", domain, user);

exit_main:
if(htoken != INVALID_HANDLE_VALUE)CloseHandle(htoken);
if(hp != INVALID_HANDLE_VALUE)CloseHandle(hp);
return 0;
}


http://www.i-hy.com/xml
kimryo 2001-05-25
  • 打赏
  • 举报
回复
char *name=new char[100];
GetUserName(name,100);
greatll 2001-05-25
  • 打赏
  • 举报
回复
哇靠,这么多热情的兄弟,叫我把分给谁好啊?
BTW,偶是新手,谁能告诉俺该怎样给分啊?
Regen219 2001-05-25
  • 打赏
  • 举报
回复
我没说是2000下的哦
flyswatter 2001-05-25
  • 打赏
  • 举报
回复
参数说明:
LPTSTR lpBuffer接收用户名,字符串
LPDWORD nSize 是个变量,要初始化,指定字符串的长度,返回后接受用户名实际长度
rh 2001-05-25
  • 打赏
  • 举报
回复
拜托!我们说的是api!而不是mfc里的类成员函数!
Regen219 2001-05-24
  • 打赏
  • 举报
回复
在注册表中的HKEY_LOCAL_MACHINE\Network\Logon下可以找到username,即当前用户名
rh 2001-05-24
  • 打赏
  • 举报
回复
BOOL GetUserName(
LPTSTR lpBuffer, // name buffer
LPDWORD nSize // size of name buffer
);
verybigbug 2001-05-24
  • 打赏
  • 举报
回复
char buf[64];

GetUserName(buf, sizeof(buf));
rh 2001-05-24
  • 打赏
  • 举报
回复
BOOL GetUserName(
LPTSTR lpBuffer, // name buffer
LPDWORD nSize // size of name buffer
);
rh 2001-05-24
  • 打赏
  • 举报
回复
getusername
jww330 2001-05-24
  • 打赏
  • 举报
回复
具体一点。

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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