Microsoft KB Archive/155698

{|
 * width="100%"|

HOWTO: Retrieve Current User and Domain Names on Windows 95 and Windows 98

 * }

Q155698

-

The information in this article applies to:


 * Microsoft Win32 Application Programming Interface (API), included with:
 * Microsoft Windows 95
 * Microsoft Windows 98

-

SUMMARY
This article demonstrates how to programmatically retrieve the user name and domain name for the interactively logged-on user on Windows 95 and Windows 98, 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).

For information on how to obtain the Windows for Workgroups SDK, please see the following Web site:

"http://www.microsoft.com" NOTE: If only the user name is required, use the 32-bit GetUserName function with Windows 95, Windows 98, Microsoft Windows NT, and Microsoft Windows 2000.

Sample Code
The following sample code demonstrates how to programmatically retrieve the user name and domain name on Windows 95 and 98 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 
 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 
 * 5) include 

//********************************************************************** // // 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; }