Retrieve Computer and User names

Submitted by date of submission user level 

Bulent Ozkir 01/15/2001 Beginner 

  

A sample function to retrieve logged on "Computer Name + User Name +

CString CRegBatchDlg::StartLog() 

/* this function just returns the initial logging string by returning computername, user name and domain name simply I did not want to use NetWkstaUserGetInfo(...) since NET API functions consume too much time... */ 

CString str1, str2, str3; 

// The name of the computer running this code. 

TCHAR szComputerName[MAX_COMPUTERNAME_LENGTH+1]; 

DWORD dwCompNameBufferSize = MAX_COMPUTERNAME_LENGTH; 

GetComputerName(szComputerName, &dwCompNameBufferSize); 

szComputerName[dwCompNameBufferSize] = '\0'; 

str1.Format(_T("%s"),szComputerName); 

// get current username running the current thread 

TCHAR szUserName[dwNameLength]; 

DWORD dwUserName = sizeof szUserName / sizeof TCHAR; 

GetUserName(szUserName, &dwUserName ); 

str2.Format(_T("%s"), szUserName); 

// get the domain name via Environment Variable 

TCHAR szDomainName[dwNameLength]; 

DWORD dwDomainNameLength = ExpandEnvironmentStrings(_T("%USERDOMAIN%"), 

szDomainName, dwNameLength); 

str3.Format(_T("%s"), szDomainName); 

return _T("##### Starting REGBATCH operations; Computername:") + str1 + 

_T(", UserName:") + str2 + _T(", DomainName:") + str3 + _T(" #####\n"); 

 

 

About the Author:

Bulent Ozkir is a Turkey based senior software engineer. He has worked as a senior support engineer for Microsoft from 1998 to 2000. In his over 5 years programming career he has been working on various programming technologies including VB, VC, Visual Interdev, JavaScript, ASP, HTML/DHTML, WMI, COM+, TCP/IP, IIS, SS3, MCIS25, ASP and SQL Server. His background includes B.A. Comp. Sc., MCSE + Internet and MCSD. See members area for more details.