Knowledge Base

HOWTO: Retrieve Current User and Domain Names on Windows

PSS ID Number: 155698

Article Last Modified on 5/10/2003


The information in this article applies to:


This article was previously published under Q155698

SUMMARY

This article demonstrates how to programmatically retrieve the user name and domain name for the interactively logged-on user on Microsoft Windows 95, Windows 98, or Windows Millennium Edition (Me) which is only possible by using the 16-bit LAN Manager NetWkstaGetInfo function.

MORE INFORMATION

On a Windows 95-based computer, there is no 32-bit function for obtaining both the user name and domain name associated with the logged-on user. However, it is possible to obtain these values by calling the 16-bit LAN Manager NetWkstaGetInfo function at info-level 10. This function is exported by a 16-bit Netapi.dll library. The header files and libraries associated with the 16-bit LAN Manager functions can be found in the Windows for Workgroups Software Development Kit (SDK).

NOTE: The Windows for Workgroups SDK is no longer available for purchase from Microsoft and is not supported after Nov. 30, 2001. Customers who want functionality that is only provided by the Windows for Workgroups SDK should contact Microsoft Developer Support to obtain the necessary header and library files:
NOTE: If only the user name is required, use the 32-bit GetUserName function with Windows 95, Windows 98, Windows Me, Windows NT, Windows 2000, and Windows XP.

Sample Code

The following sample code demonstrates how to programmatically retrieve the user name and domain name on Windows 95, Windows 98, or Windows Me by using the NetWkstaGetInfo function. Because the NetWkstaGetInfo function is a 16-bit function, this code sample must be compiled by using a 16-bit compiler:
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <netcons.h>
#include <wksta.h>
#include <neterr.h>

//**********************************************************************
// 
//  FUNCTION:     GetCurrentUserAndDomain - This function looks up
//                the user name and domain name for the user account
//                associated with the calling thread.
// 
//  PARAMETERS:   szUser - a buffer that receives the user name
//                pcchUser - the size, in characters, of szUser
//                szDomain - a buffer that receives the domain name
//                pcchDomain - the size, in characters, of szDomain
// 
//  RETURN VALUE: NERR_Success if the function succeeds. Otherwise, the
//                function returns a network error code from neterr.h.
// 
//                If either of the supplied buffers are too small, the
//                function will return NERR_BufTooSmall and pcchUser and
//                pcchDomain will be adjusted to reflect the required
//                buffer sizes.
// 
//**********************************************************************

unsigned short GetCurrentUserAndDomain(char* szUser, int* pcchUser, 
      char* szDomain, int* pcchDomain) {

   unsigned short iNetErr;

   struct wksta_info_10 far* pwi10 = NULL;
   unsigned short cbwi10 = 0;

   int iDomainLen;
   int iUserLen;

   // Obtain the required buffer size for the account information.
   iNetErr = NetWkstaGetInfo(NULL, 10, (char far*) pwi10, cbwi10,
         &cbwi10);

   // Call should have failed due to zero-length buffer.
   if (iNetErr != NERR_BufTooSmall)
      return iNetErr;

   // Allocate buffer for account information.
   pwi10 = (struct wksta_info_10 far*) malloc(cbwi10);
   if (pwi10 == NULL)
      return NERR_ProgNeedsExtraMem;

   // Retrieve the account information.
   iNetErr = NetWkstaGetInfo(NULL, 10, (char far*) pwi10, cbwi10,
         &cbwi10);
   if (iNetErr == NERR_Success) {

      // If possible, copy the user name to the supplied buffer.
      iUserLen = strlen(pwi10->wki10_username) + 1;
      if (iUserLen < *pcchUser)
         strcpy(szUser, pwi10->wki10_username);
      else 
         iNetErr = NERR_BufTooSmall;

      // Set required buffer size.
      *pcchUser = iUserLen;

      // if possible, copy the domain name to the supplied buffer.
      iDomainLen = strlen(pwi10->wki10_logon_domain) + 1;
      if (iDomainLen < *pcchDomain)
         strcpy(szDomain, pwi10->wki10_logon_domain);
      else
         iNetErr = NERR_BufTooSmall;

      // Set required buffer size.
      *pcchDomain = iDomainLen;
   }

   // Free the allocated buffer.
   free(pwi10);

   return iNetErr;
}
				

REFERENCES

In Windows NT, Windows 2000, and Windows XP, you can retrieve the user name and domain name for the calling thread by using 32-bit functions.

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

111544 HOWTO: Retrieve Current User and Domain Names on Windows NT

The sample in this article must be compiled with a 16-bit compiler. Another option is to call the 16-bit function from within a 32-bit application by using a technique called thunking.

For additional information on how to thunk from 32-bit code to 16-bit code, click the article number below to view the article in the Microsoft Knowledge Base:

155763 HOWTO: Call 16-bit Code from 32-bit Code on Windows 95


Additional query words: username domain

Keywords: kbAPI kbcode kbhowto kbKernBase kbnetwork kbSecurity KB155698
Technology: kbAudDeveloper kbOSWin2000 kbOSWin95 kbOSWin98 kbOSWinME kbOSWinNTSearch kbOSWinSearch kbOSWinXP kbOSWinXPSearch kbWin32API kbWin32sSearch