PSS ID Number: 155698
Article Last Modified on 5/10/2003
#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;
}
111544 HOWTO: Retrieve Current User and Domain Names on Windows NT
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