c++编个小程序用GetUsername()函数获取计算机用户名

用c++编一个小程序调用GetUsername()函数获取并显示计算机用户名。谢谢... 用c++编一个小程序调用GetUsername()函数获取并显示计算机用户名。谢谢 展开
 我来答
laom75
高粉答主

推荐于2016-01-12 · 关注我不会让你失望
知道大有可为答主
回答量:4.6万
采纳率:84%
帮助的人:5026万
展开全部
  函数功能:获取当前用户帐号
  API原型:
  BOOL GetUserName( LPTSTR lpBuffer, // name buffer 缓冲区,存放用户名。 LPDWORD nSize // size of name buffer //返回用户名大小。);

  #include<windows.h>
  #include<cstdlib>
  #include<string>
  #include<iostream>
  usingnamespace std;
  string GetUserName()
  {
  char szUser[80];
   DWORD cbUser = 80;
  if(GetUserName (szUser, &cbUser))
   return szUser;
   else
   return("GetUserName failed.\n");
  }
  int main()
  {
   string UserName=GetUserName();
   cout<<"当前用户名为:"<<UserName<<endl; system("PAUSE");
   return 0;
  }
火驹
推荐于2018-03-19 · TA获得超过1447个赞
知道小有建树答主
回答量:6294
采纳率:0%
帮助的人:1707万
展开全部
#include<windows.h>
#include<cstdlib>
#include<string>
#include<iostream>
using namespace std;
string GetUserName()
{
char szUser[80];
DWORD cbUser = 80;
if (GetUserName (szUser, &cbUser))
return szUser;
else
return ("GetUserName failed.\n");
}

int main()
{
string UserName=GetUserName();
cout<<"当前用户名为:"<<UserName<<endl;
system("PAUSE");
return 0;
}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
sys_master
2009-03-22 · TA获得超过7942个赞
知道小有建树答主
回答量:1202
采纳率:0%
帮助的人:717万
展开全部
有现成函数GetUserName。具体的你查查MSDN吧,如果没安装,直接看我copy的吧:

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.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式