Retrieve logged on user name on Windows NT

This piece of sample code explains how to retrieve the Logged on User and Domain name on Win NT/2000 using Win32 functions. .

The function GetUserName is used for retrieving the logged on user name. The LookupAccountNameA is used to find out the domain name of the user.
The following piece of code has a separate scripted function GetDomain(), which does all the initializations and declarations needed for LookupAccountNameA function. Then it returns the domain name associated with the logged on user name. It takes two input parameters viz., User Name and a pointer to the SID, Security Identifier.
The given GetDomain() function can be used for finding the domain name of any user in the domain or in the computer with no/little modifications.


CString GetDomain(LPCSTR wszAccName,PSID * ppSid)
{
// Check input parameters.
if (wszAccName == NULL || ppSid == NULL)
{ return ""; }
// Create buffers that are large enough.
const DWORD INITIAL_SIZE = 50;
DWORD cbSid = 0;
DWORD dwSidBufferSize = INITIAL_SIZE;
DWORD cchDomainName = 0;
DWORD dwDomainBufferSize = INITIAL_SIZE;
char * wszDomainName = NULL;
SID_NAME_USE eSidType;
DWORD lErrorCode = 0;
HRESULT hr = S_OK;
CString strDomain;
// Create buffers for the SID and the domain name.
*ppSid = (PSID) new BYTE[dwSidBufferSize];
if (*ppSid == NULL)
{ return ""; }
memset(*ppSid, 0, dwSidBufferSize);
wszDomainName = new char[dwDomainBufferSize];
if (wszDomainName == NULL)
{ return ""; }
memset(wszDomainName, 0, dwDomainBufferSize*sizeof(char));
// Retrieve the SID for the account name passed.
for ( ; ; )
{
// Set the count variables to the buffer sizes and retrieve the SID.
cbSid = dwSidBufferSize; cchDomainName = dwDomainBufferSize;
if (LookupAccountNameA( NULL, // Computer name.NULL for the local computer
wszAccName, *ppSid, // Pointer to the SID buffer. Use NULL to get the size needed,
&cbSid, // Size of the SID buffer needed.
wszDomainName, // wszDomainName,
&cchDomainName, &eSidType ))
{

if (IsValidSid(*ppSid) == FALSE)
{
wprintf(L"SID for %s is not valid.n", wszAccName);
lErrorCode = E_FAIL;
}
strDomain = wszDomainName;
break;
}
lErrorCode = GetLastError(); // Check if one of the buffers was too small.
if (lErrorCode == ERROR_INSUFFICIENT_BUFFER)
{
if (cbSid > dwSidBufferSize)
{
// Reallocate memory for the SID buffer.
wprintf(L"The SID buffer was too small. It will be reallocated.n");
FreeSid(*ppSid);
*ppSid = (PSID) new BYTE[cbSid];
if (*ppSid == NULL)
{ return ""; }
memset(*ppSid, 0, cbSid);
dwSidBufferSize = cbSid;
}
if (cchDomainName > dwDomainBufferSize)
{
// Reallocate memory for the domain name buffer.
wprintf(L"The domain name buffer was too small. It will be reallocated.n");
delete [] wszDomainName;
wszDomainName = new char[cchDomainName];
if (wszDomainName == NULL)
{ return ""; }
memset(wszDomainName, 0, cchDomainName*sizeof(char));
dwDomainBufferSize = cchDomainName;
}
}
else
{
wprintf(L"LookupAccountName failed. GetLastError returned: %dn", lErrorCode);
hr = HRESULT_FROM_WIN32(lErrorCode);
break;
}
}
delete [] wszDomainName;
return strDomain ;
}
int main()
{
char UserName[100];
CString Domain;
DWORD UserSize = sizeof(UserName);
GetUserName(UserName,&UserSize);
printf("%sn",UserName); 

PSID Sid;
Domain = GetDomain(UserName,&Sid);
printf("%sn",Domain);
}