Platform SDK: Windows Networking

Retrieving the User Name

Windows NT/Windows 2000

To retrieve the name of the user associated either with a local device connected to a network resource or with the name of a network, an application can call the WNetGetUser function. The following example uses the device name to retrieve the name of the user. The sample calls an application-defined error handler to process errors, and the TextOut function for printing.

CHAR szUserName[80]; 
DWORD dwResult, cchBuff = 80; 
 
// Call the WNetGetUser function.
//
dwResult = WNetGetUser("z:", 
    (LPSTR) szUserName, 
    &cchBuff); 
 
// If the call succeeds, print the user name.
//
if(dwResult == NO_ERROR) 
    TextOut(hdc, 10, 10, 
        (LPSTR) szUserName, 
        lstrlen((LPSTR) szUserName)); 
 
// Call an application-defined error handler.
//
else { 
    NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetGetUser"); 
    return FALSE; 
}

For more information about using an application-defined error handler, see Retrieving Network Errors.