WNetGetUserW function

The WNetGetUser function retrieves the current default user name, or the user name used to establish a network connection.

Syntax

DWORD WNetGetUserW(
  LPCWSTR lpName,
  LPWSTR  lpUserName,
  LPDWORD lpnLength
);

Parameters

lpName

A pointer to a constant null-terminated string that specifies either the name of a local device that has been redirected to a network resource, or the remote name of a network resource to which a connection has been made without redirecting a local device.

If this parameter is NULL or the empty string, the system returns the name of the current user for the process.

lpUserName

A pointer to a buffer that receives the null-terminated user name.

lpnLength

A pointer to a variable that specifies the size of the lpUserName buffer, in characters. If the call fails because the buffer is not large enough, this variable contains the required buffer size.

Return Value

If the function succeeds, the return value is NO_ERROR.

If the function fails, the return value is a system error code, such as one of the following values.

Return code Description
ERROR_NOT_CONNECTED
The device specified by the lpName parameter is not a redirected device or a connected network name.
ERROR_MORE_DATA
More entries are available with subsequent calls.
ERROR_NO_NETWORK
The network is unavailable.
ERROR_EXTENDED_ERROR
A network-specific error occurred. To obtain a description of the error, call the WNetGetLastError function.
ERROR_NO_NET_OR_BAD_PATH
None of the providers recognize the local name as having a connection. However, the network is not available for at least one provider to whom the connection may belong.

Remarks

The WNetGetUser function is not aware of shares on the Distributed File System (DFS). If the name specified by the lpName parameter is a local device redirected to a DFS share or a remote resource that represents a DFS share, the WNetGetUser function fails with ERROR_NOT_CONNECTED.

Examples

The following code sample illustrates how to use the WNetGetUser function to retrieve the name of the user associated with a redirected local device or a remote network resource.

#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "mpr.lib")

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <Winnetwk.h>

int wmain(int argc, wchar_t * argv[])
{
    DWORD dwRetVal;

    WCHAR UserName[MAX_PATH];

    DWORD dwNameLength = MAX_PATH;

    if (argc != 2) {
        wprintf
            (L"Usage: %s [Redirected-LocalDevice or Network-Resource-Remote-name\n",
             argv[0]);
        exit(1);
    }

    wprintf(L"Calling WNetGetUser with Network-Resource = %s\n", argv[1]);

    dwRetVal = WNetGetUser(argv[1], UserName, &dwNameLength);
    //
    // If the call succeeds, print the user information.
    //
    if (dwRetVal == NO_ERROR) {

        wprintf(L"WNetGetUser returned success\n");
        wprintf(L"\tUsername=%s   NameLength=%d\n", &UserName, dwNameLength);
        exit(0);
    }

    else {
        wprintf(L"WNetGetUser failed with error: %u\n", dwRetVal);
        exit(1);
    }
}


Requirements

   
Minimum supported client Windows 2000 Professional [desktop apps only]
Minimum supported server Windows 2000 Server [desktop apps only]
Target Platform Windows
Header winnetwk.h
Library Mpr.lib
DLL Mpr.dll

See Also

Retrieving the User Name

WNetGetConnection

Windows Networking (WNet) Overview

Windows Networking Functions