Platform SDK: Windows Networking

Retrieving Information About a Network Resource

Windows NT/Windows 2000

To identify the network provider that owns a resource, an application can call the WNetGetResourceInformation function, as illustrated in the following code sample.

The following sample is a function (CheckServer) that takes a server name as a parameter and returns information about that server. First the function calls the ZeroMemory function to initialize a block of memory to zeroes. Then the sample calls the WNetGetResourceInformation function, specifying a buffer large enough to hold only a NETRESOURCE structure. The routine includes error processing to handle the case when a buffer of this size is insufficient to hold the variable-length strings to which members of the NETRESOURCE structure point. If this error occurs, the sample calls the LocalAlloc function to allocate sufficient memory and then the code calls WNetGetResourceInformation again. Finally, the sample calls LocalFree to free the allocated memory.

Note that the sample assumes that the pszServer parameter points to a server name that one of the network providers on the local computer recognizes.

//
// Verify a server on the network. 
//
DWORD
CheckServer(
    LPTSTR pszServer
    )
{  
    DWORD          dwError;
    NETRESOURCE    nr;
    NETRESOURCE    nrOut;
    LPTSTR         pszSystem = NULL;          // pointer to variable-length strings
    LPBYTE         lpBuffer  = &nrOut;        // buffer
    DWORD          cbResult  = sizeof(nrOut); // buffer size

    //
    // Fill a block of memory with zeroes; then 
    //  initialize the NETRESOURCE structure. 
    //
    ZeroMemory(nr, sizeof(nr));

    nr.dwScope       = RESOURCE_GLOBALNET;
    nr.dwType        = RESOURCETYPE_ANY;
    nr.lpRemoteName  = pszServer;

    //
    // First call the WNetGetResourceInformation function with 
    //  memory allocated to hold only a NETRESOURCE structure. This 
    //  method can succeed if all the NETRESOURCE pointers are NULL.
    //
    dwError = WNetGetResourceInformation(&nr, lpBuffer, &cbResult, &pszSystem);

    //
    // If the call fails because the buffer is too small, 
    //   call the LocalAlloc function to allocate a larger buffer.
    //
    if (dwError == ERROR_MORE_DATA)
    {
        lpBuffer = LocalAlloc(LMEM_FIXED, cbResult);

        if (lpBuffer == NULL)
        {
            dwError = ERROR_NOT_ENOUGH_MEMORY;
        }
        //
        // Call WNetGetResourceInformation again
        //  with the larger buffer.
        //
        else
        {
            dwError = WNetGetResourceInformation(&nr, lpBuffer, &cbResult, &pszSystem);
        }
    }
    if (dwError == NO_ERROR)
    {
        // If the call succeeds, process the contents of the 
        //  returned NETRESOURCE structure and the variable-length
        //  strings in lpBuffer. Then free the memory.
        //
        if (lpBuffer != &nrOut)
        {
            LocalFree(lpBuffer);
        }
    }

    return dwError;
}