This article is just a brief discussion on the use of three network management functions:
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.
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:
dwStatus = NetShareEnum(_T("solomon"),
(LPBYTE *) &pShareBuffer,
if (NERR_Success == dwStatus && dwSharesRead > 0)
pShareTemp = pShareBuffer;
for (dwIndex = 0; dwIndex < dwReadEntries; dwIndex++, pShareTemp++)
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:
dwStatus = NetConnectionEnum(_T("solomon"),
(LPBYTE *) &pConnBuffer,
if (NERR_Success == dwStatus && dwConnectionsRead > 0)
pConnTemp = pConnBuffer;
while (dwConnectionsRead > 0)
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.
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:
dwStatus = NetFileEnum(_T("solomon"),
(LPBYTE *) &pBuffer,
if (NERR_Success == dwStatus && dwReadEntries > 0)
pCurrent = pBuffer;
for (dwIndex = 0; dwIndex < dwReadEntries; dwIndex++, pCurrent++)
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
The output of this function produces something like:
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
AdjustTokenPrivileges() for this.