Microsoft KB Archive/96772

= How To Use WNetGetUser in Windows for Workgroups =

Article ID: 96772

Article Last Modified on 7/14/2004

-

APPLIES TO


 * Microsoft Windows Software Development Kit 3.11

-



This article was previously published under Q96772



SUMMARY
WNetGetUser is documented on page 194 in the Microsoft Windows Device Driver Kit (DDK) &quot;Device Driver Adaptation Guide&quot; for version 3.1. This function can be used to retrieve the current user name of the user logged on to the network underlying Windows. However, when using this function in Microsoft Windows for Workgroups, WNetGetUser returns WN_SUCCESS but the user name string is empty. Because WFWNET.DRV is a multinet driver, the network that supports WNetGetUser must be activated before using this function.



MORE INFORMATION
Until the release of the Windows for Workgroups SDK, WNetGetUser was used internally by Windows. It is documented in the version 3.1 &quot;Device Driver Adaptation Guide&quot; for network device driver developers. Now, the Windows for Workgroups SDK allows you to call WNetGetUser from an application; however, when using this function on a system with a multinet driver, the supporting network must be activated first using MNetSetNextTarget. If the target network has not been set, the function returns WN_SUCCESS but the returned string for the user name is empty. This is documented in the description of MNetSetNextTarget in the Windows for Workgroups SDK.

It is not sufficient to just call MNetSetNextTarget before WNetGetUser. The current target network may not support WNetGetUser. To properly set the target network to use WNetGetUser, the networks must be enumerated and WNetGetUser called for each network. If WNetGetUser does not return WN_SUCCESS, then enumerate the next target network and iterate this loop. If the loop iterates through all of the networks but WNetGetUser never returns WN_SUCCESS, then none of the target networks support WNetGetUser.

To determine whether a system supports multinet operations, WNetGetCaps can be used with WNNC_NET_TYPE. The return value has the WNNC_NET_MultiNet (0x8000) set if the system supports multinet operations.

Sample Code

 * 1) include 

//   // Function:  NetGetCaps //   // Purpose:   Attempts to Call WNetGetCaps from USER.EXE //   // Returns:   See WNetGetCaps. If the entry point cannot be located, //           returns NULL //

WORD NetGetCaps( WORD wCaps ) {     typedef         WORD (CALLBACK *LPFNWNETGETCAPS)( WORD );

LPFNWNETGETCAPS lpfnWNetGetCaps; WORD           wNetType = NULL; HINSTANCE      hInst;

// Get the entry point for WNetGetUser from USER.EXE. USER implements // the function by calling through to the network driver. hInst = LoadLibrary( &quot;USER.EXE&quot; ); if ( !( lpfnWNetGetCaps = (LPFNWNETGETCAPS) GetProcAddress( hInst, &quot;WNETGETCAPS&quot; )) ) {        OutputDebugString(&quot;Could not locate WNetGetCaps entry point.\n&quot;); }     else // Make the call wNetType = (*lpfnWNetGetCaps)( wCaps );

return wNetType; }

//   // Function:  NetGetUser //   // Purpose:   Attempts to call WNetGetUser from USER.EXE //   // Returns:   TRUE if the function succeeded, otherwise FALSE. //

BOOL NetGetUser (LPSTR lpszUserName, int *pcb ) {     typedef BOOL (CALLBACK *LPFNGETUSER)( LPSTR, int * );

// Find the entry point for WNetGetUser in USER.EXE HINSTANCE  hInst       = LoadLibrary( &quot;USER.EXE&quot; ); LPFNGETUSER lpfnGetUser = (LPFNGETUSER) GetProcAddress( hInst, &quot;WNETGETUSER&quot; );

if (!lpfnGetUser) return FALSE;

// See if this computer includes multinet support if ( NetGetCaps( WNNC_NET_TYPE ) & WNNC_NET_MultiNet ) {        //          // Multinet support found. Get entry points to the MNet APIs //

typedef WORD (CALLBACK *LPFNMNETNETWORKENUM)( HANDLE FAR * ); typedef WORD (CALLBACK *LPFNMNETSETNEXTTARGET)( HANDLE );

LPFNMNETNETWORKENUM  lpfnMNetNetworkEnum; LPFNMNETSETNEXTTARGET lpfnMNetSetNextTarget;

OutputDebugString(&quot;GetUser: WNNC_NET_MultiNet is set.\n&quot;);

// Get the module handle for the network driver hInst = (HINSTANCE) NetGetCaps( 0xFFFF );

// Get the entry points lpfnMNetNetworkEnum  = (LPFNMNETNETWORKENUM)GetProcAddress(hInst,&quot;MNETNETWORKENUM&quot;); lpfnMNetSetNextTarget = (LPFNMNETSETNEXTTARGET)GetProcAddress (hInst,&quot;MNETSETNEXTTARGET&quot;);

// If both entry points were located, use them if ( lpfnMNetNetworkEnum && lpfnMNetSetNextTarget ) {           HANDLE hEnum   = NULL; WORD  wResult = lpfnMNetNetworkEnum( &hEnum );

while ( wResult == WN_SUCCESS ) {              lpfnMNetSetNextTarget( hEnum );     // activate that network wResult = lpfnGetUser( lpszUserName, pcb );

if ( wResult == WN_SUCCESS ) return TRUE;

OutputDebugString (&quot;WNetGetUser failed, try next provider.\n&quot;);

// otherwise, try the next one for WNetGetUser support wResult = lpfnMNetNetworkEnum( &hEnum ); }

return FALSE; }

// Entry points for the MNet APIs we need weren't located. // Try calling WNetGetUser anyway. Note: Windows NT 3.51 gives // this behavior

OutputDebugString(&quot;Warning - WFWNET.DRV doesn't contain \            entry points for MNetNetworkEnum and/or MNetSetNextTarget.\n&quot;); }

WORD wResult = lpfnGetUser( lpszUserName, pcb );

return (wResult) ? FALSE : TRUE; }

Additional query words: username multi-net

Keywords: kbhowto kbapi kbnetwork kbwnet KB96772

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.