Windows API 之一:GetUserName和GetComputerName

来源:互联网 发布:小学生编程比赛 编辑:程序博客网 时间:2024/06/07 12:30

GetUserName函数,MSDN的解释为:Retrieves the name of the user associated with the current thread,得到运行当前线程的用户名

函数原型:

BOOL WINAPI GetUserName(  _Out_    LPTSTR lpBuffer,  _Inout_  LPDWORD lpnSize);

实例如下,在VS2008上编译通过

LPTSTR getUserName(){const int MAX_BUFFER_LEN = UNLEN + 1;  TCHAR szBuffer[MAX_BUFFER_LEN];  DWORD dwNameLen;  LPTSTR userName;dwNameLen = MAX_BUFFER_LEN;  if(GetUserName(szBuffer, &dwNameLen))  userName = szBuffer;return userName;}

GetComputerName函数,获取计算机名,MSDN的解释为:retrieves only the NetBIOS name of the local compute

函数原型

BOOL WINAPI GetComputerName(  _Out_    LPTSTR lpBuffer,  _Inout_  LPDWORD lpnSize);

实例如下,在VS2008上编译通过

CString Authority::getComputerName() {TCHAR  infoBuf[MAX_COMPUTERNAME_LENGTH + 1];DWORD  bufCharCount = MAX_COMPUTERNAME_LENGTH + 1;// Get and display the name of the computer. CString computerName;if(GetComputerName(infoBuf, &bufCharCount))computerName = CString(infoBuf);return computerName;}


0 0