Click here to Skip to main content
15,881,852 members
Articles / Desktop Programming / MFC
Article

A brief discussion on how to use three of the network management functions

Rate me:
Please Sign up or sign in to vote.
4.44/5 (16 votes)
2 May 20042 min read 149.3K   1.4K   33   61
A brief discussion on how to use NetShareEnum, NetConnectionEnum, and NetFileEnum

Introduction

This article is just a brief discussion on the use of three network management functions: NetShareEnum(), NetConnectionEnum(), and NetFileEnum(). The first two can be used together to get a list of shared resources on a given server, and then to get a list of connections made to each of those shares. The last one is used to get a list of open files on a server. You can see examples of these using the Computer Management snap-in.

Listing Shares

To get a listing of shared resources on a server, including ones on which the function is running, use NetShareEnum(). As the name implies, it enumerates network shares. It's used like:

DWORD          dwStatus,
               dwReadEntries,
               dwTotalEntries,
               dwIndex;
LPSHARE_INFO_2 pShareBuffer,
               pShareTemp;

dwStatus = NetShareEnum(_T("solomon"),
                        2,
                        (LPBYTE *) &pShareBuffer,
                        MAX_PREFERRED_LENGTH,
                        &dwReadEntries,
                        &dwTotalEntries,
                        NULL);
if (NERR_Success == dwStatus && dwSharesRead > 0)
{
    pShareTemp = pShareBuffer;
    for (dwIndex = 0; dwIndex < dwReadEntries; dwIndex++, pShareTemp++)
        ...

    NetApiBufferFree(pShareBuffer);
}

Something to note about this function, and the other two, is that the server name need not begin with \\. MSDN states that it must, but my testing found that either way was acceptable.

The fourth parameter, prefmaxlen, indicates how much data is to be returned. I couldn't find any reason to not use MAX_PREFERRED_LENGTH here. Maybe in times past with limited amounts of available RAM.

Listing Connections to Shares

To get a listing of connections to each of the shares gathered earlier, use NetConnectionEnum(). As the name implies, it enumerates connections to network shares. It's used like:

DWORD               dwStatus,
                    dwConnectionsRead,
                    dwTotalConnections;
LPCONNECTION_INFO_1 pConnBuffer,
                    pConnTemp;

dwStatus = NetConnectionEnum(_T("solomon"),
                             pShareTemp->shi2_netname,
                             1,
                             (LPBYTE *) &pConnBuffer,
                             MAX_PREFERRED_LENGTH,
                             &dwConnectionsRead,
                             &dwTotalConnections,
                             NULL);
if (NERR_Success == dwStatus && dwConnectionsRead > 0)
{
    pConnTemp = pConnBuffer; 
    while (dwConnectionsRead > 0)    
    {        
        ...        
        dwConnectionsRead--;        
        pConnTemp++;
    } 

    NetApiBufferFree(pConnBuffer);
} 

The second parameter, qualifier, is the name of a share returned by the previous call to NetShareEnum(). Putting these two functions together yields something like the following. I used a tree control so that the relationship between a share and its connection(s) could be seen.

Image 1

Listing Open Files

To get a listing of open files on a server, use NetFileEnum(). As the name implies, it enumerates files on a network. It's used like:

DWORD         dwIndex,
              dwStatus,
              dwReadEntries,
              dwTotalEntries;
LPFILE_INFO_3 pBuffer,
              pCurrent;

dwStatus = NetFileEnum(_T("solomon"),
                       NULL,
                       NULL,
                       3,
                       (LPBYTE *) &pBuffer,
                       MAX_PREFERRED_LENGTH,
                       &dwReadEntries,
                       &dwTotalEntries,
                       NULL);
if (NERR_Success == dwStatus && dwReadEntries > 0)
{
    pCurrent = pBuffer;    

    for (dwIndex = 0; dwIndex < dwReadEntries; dwIndex++, pCurrent++)
        ... 
    
    NetApiBufferFree(pBuffer);
}

You can see what files are open and by whom. Something of interest regarding the fi3_permissions member of the FILE_INFO_3 structure is that it can have four more values than are noted by MSDN. Those values are ACCESS_EXEC, ACCESS_DELETE, ACCESS_ATRIB, and ACCESS_PERM.

The output of this function produces something like:

Image 2

Notes

Each of these functions behave in accordance to the membership of the calling process. If the calling process is a member of the Administrators group, all is well. Otherwise, additional code will be necessary to mimic membership of such a group. Use OpenProcessToken() and AdjustTokenPrivileges() for this.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Pinnacle Business Systems
United States United States

The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.

HTTP 404 - File not found
Internet Information Services

Comments and Discussions

 
GeneralRe: I want to use this without Unicode mode Pin
KmAshif3-May-04 5:14
KmAshif3-May-04 5:14 
Generalcompile error Pin
KmAshif3-May-04 2:36
KmAshif3-May-04 2:36 
GeneralRe: compile error Pin
David Crow3-May-04 2:38
David Crow3-May-04 2:38 
GeneralNetShareEnum() APi is not working in my project Pin
KmAshif3-May-04 2:28
KmAshif3-May-04 2:28 
GeneralRe: NetShareEnum() APi is not working in my project Pin
David Crow3-May-04 2:42
David Crow3-May-04 2:42 
GeneralNicely written, David Pin
Nish Nishant2-May-04 19:58
sitebuilderNish Nishant2-May-04 19:58 
GeneralExcillentl article Pin
KmAshif29-Apr-04 17:56
KmAshif29-Apr-04 17:56 
GeneralRe: Excillentl article Pin
David Crow30-Apr-04 2:42
David Crow30-Apr-04 2:42 
Great! That was the intent.


"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)


GeneralRe: Excillentl article - I don't think so Pin
AnhNT807-Apr-05 21:34
AnhNT807-Apr-05 21:34 
GeneralRe: Excillentl article - I don't think so Pin
David Crow8-Apr-05 2:39
David Crow8-Apr-05 2:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.