Retrieving a User Name

An application can use the WNetGetUser function to retrieve the name of the user associated with a connected network resource. The following code example shows how WNetGetUser function retrieves a user name based on a device name called MyDevice.

TCHAR szUserName[80];
DWORD dwResult, 
      cchBuffer = 80;

dwResult = WNetGetUser (TEXT("MyDevice"), szUserName, &cchBuffer);

if (dwResult == ERROR_SUCCESS)
  MessageBox (hwnd, szUserName, TEXT("Info"), MB_OK);
else 
{
  ErrorHandler (hwnd, dwResult, TEXT("WNetGetUser"));
  return FALSE;
}