CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

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

楼主greatll(流流)2001-05-24 16:24:00 在 VC/MFC / 基础类 提问

多谢! 问题点数:20、回复次数:16Top

1 楼jww330(追意)回复于 2001-05-24 16:40:00 得分 0

具体一点。Top

2 楼rh(花覆茅檐)回复于 2001-05-24 16:45:00 得分 0

getusernameTop

3 楼verybigbug(等待中)回复于 2001-05-24 16:46:00 得分 0

char   buf[64];  
   
  GetUserName(buf,   sizeof(buf));Top

4 楼rh(花覆茅檐)回复于 2001-05-24 16:46:00 得分 0

BOOL   GetUserName(  
      LPTSTR   lpBuffer,     //   name   buffer  
      LPDWORD   nSize           //   size   of   name   buffer  
  );  
  Top

5 楼rh(花覆茅檐)回复于 2001-05-24 16:49:00 得分 0

BOOL   GetUserName(  
      LPTSTR   lpBuffer,     //   name   buffer  
      LPDWORD   nSize           //   size   of   name   buffer  
  );  
  Top

6 楼Regen219(雨)回复于 2001-05-24 16:53:00 得分 0

在注册表中的HKEY_LOCAL_MACHINE\Network\Logon下可以找到username,即当前用户名Top

7 楼greatll(流流)回复于 2001-05-25 12:48:00 得分 0

CDaoWorkspace::GetUserName?是CString类型的啊。  
  MFC中好像没有上面几位大虾说的BOOL   GetUserName吧?  
  Top

8 楼yangmajituipo()回复于 2001-05-25 12:50:00 得分 0

是sdk中的,再看看Top

9 楼greatll(流流)回复于 2001-05-25 12:52:00 得分 0

注册表中HKEY_LOCAL_MACHINE下没有Network键啊(至少2000没有)。Top

10 楼flyswatter(细雨)回复于 2001-05-25 12:59:00 得分 0

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    
   
     
  Top

11 楼rh(花覆茅檐)回复于 2001-05-25 13:01:00 得分 10

拜托!我们说的是api!而不是mfc里的类成员函数!Top

12 楼flyswatter(细雨)回复于 2001-05-25 13:02:00 得分 5

参数说明:  
  LPTSTR   lpBuffer接收用户名,字符串  
  LPDWORD   nSize     是个变量,要初始化,指定字符串的长度,返回后接受用户名实际长度  
  Top

13 楼Regen219(雨)回复于 2001-05-25 13:08:00 得分 0

我没说是2000下的哦Top

14 楼greatll(流流)回复于 2001-05-25 13:10:00 得分 0

哇靠,这么多热情的兄弟,叫我把分给谁好啊?  
  BTW,偶是新手,谁能告诉俺该怎样给分啊?Top

15 楼kimryo(God is on my side)回复于 2001-05-25 13:11:00 得分 0

char   *name=new   char[100];  
  GetUserName(name,100);Top

16 楼Gladiator()回复于 2001-05-25 13:16:00 得分 5

//得到当前用户  
  #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  
  Top

相关问题

  • 如何获取WINDOWS当前用户!
  • 关于java获取windows当前登录用户名的问题
  • 获取当前登陆用户的用户名
  • 调用GetUserName()为什么无法获取当前用户名?
  • 怎么获取当前用户的权限?(在线等待)
  • 请问如何用VBScript获取当前用户所在域?
  • 关于获取当前系统用户名的问题
  • 如何获取非当前登陆用户的信息
  • 我要获得sharepoint当前的登录用户,如何获取?
  • (不使用Windows Authentication)ASP.NET获取客户端用户登录Windows的用户名

关键词

  • 用户
  • hp
  • getusername
  • uulen
  • lpdword nsize
  • lptstr lpbuffer
  • htoken
  • buffer
  • tuser
  • character

得分解答快速导航

  • 帖主:greatll
  • rh
  • flyswatter
  • Gladiator

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo