Click here to Skip to main content
Click here to Skip to main content

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

By , 2 May 2004
 

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.

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:

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

About the Author

DavidCrow
Software Developer (Senior) Pinnacle Business Systems
United States United States
Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to differentiate the same user logged in from different computersmemberpclearn17 May '11 - 4:55 
David: From your screenshots i see JYOUNG listed at different places. My question is: How can i differentiate that JYOUNG is using File-A on Computer-A (vs) JYOUNG is using File-B on Computer-B. Any insights in how to solve this will be helpful.
pavanchand

AnswerRe: How to differentiate the same user logged in from different computersmemberDavidCrow17 May '11 - 5:06 
You're example compares two different computers: Computer-A and Computer-B. My screenshot was for one computer.

"One man's wage rise is another man's price increase." - Harold Wilson

"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather


GeneralRe: How to differentiate the same user logged in from different computersmemberpclearn18 May '11 - 11:24 
David: I was trying to use your screenshot to ask my question. I wasnt contridicting what you wrote. Here is my question a little in detail.
 
Say JYOUNG has an account in Domain1 and Domain2. Say on Computer-A he is logged in with DOMAIN1\\JYOUNG and opens a File-A and on Computer-B he is logged in with DOMAIN2\\JYOUNG and opens File-B.
 
How can i differentiate between DOMAIN1 access (VS) DOMAIN2 access. Since NetFileEnum cals only returns JYOUNG.
pavanchand

GeneralRe: How to differentiate the same user logged in from different computersmemberDavidCrow18 May '11 - 16:44 
pclearn wrote:
How can i differentiate between DOMAIN1 access (VS) DOMAIN2 access. Since NetFileEnum cals only returns JYOUNG.

I do not know. Good question, though.

"One man's wage rise is another man's price increase." - Harold Wilson

"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather


QuestionINTERNAL COMPILER ERROR & NetDemoDlg.sbr' [modified]memberjohn_172616 Oct '09 - 12:25 
When I attempt to build the sample solution in Visual Studio 2008, I receive the following errors,
 
Error 1 Command line error D8030 : INTERNAL COMPILER ERROR in '' cl NetDemo
Error 2 error BK1506 : cannot open file '.\Debug\NetDemoDlg.sbr': No such file or directory BSCMAKE NetDemo
 
Do you have any suggestions?
 
TIA,
 
Roger
 
modified on Friday, October 16, 2009 7:01 PM

QuestionRe: INTERNAL COMPILER ERROR & NetDemoDlg.sbr'memberjohn_172616 Oct '09 - 13:01 
Please note that I have added the following directory to the list of library directories so that it will include the Netapi32.lib file when building,
 
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Lib
 
What else am I missing?
AnswerRe: INTERNAL COMPILER ERROR & NetDemoDlg.sbr'mvpDavidCrow19 Oct '09 - 2:56 
john_1726 wrote:
Error 1 Command line error D8030 : INTERNAL COMPILER ERROR in '' cl NetDemo

 
D8030
 
john_1726 wrote:
Error 2 error BK1506 : cannot open file '.\Debug\NetDemoDlg.sbr': No such file or directory BSCMAKE NetDemo

 
BK1506
 
I have not tried compiling this with anything other than VS6. Other than Unicode issues, I know of no reason why it would not compile. You could always create a new VS2008 project and copy the CPP/H files from this article.
 

"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons


QuestionRe: INTERNAL COMPILER ERROR & NetDemoDlg.sbr' [modified]memberjohn_172619 Oct '09 - 5:02 
DavidCrow wrote:
I have not tried compiling this with anything other than VS6.

 
Is there anywhere I can download a Microsoft Visual Studio 6 Express edition for C++?
 
modified on Monday, October 19, 2009 11:35 AM

AnswerRe: INTERNAL COMPILER ERROR & NetDemoDlg.sbr'mvpDavidCrow19 Oct '09 - 5:04 
I don't know, but you can get VS2008 Express. It does not support MFC, though.
 

"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons


AnswerRe: INTERNAL COMPILER ERROR & NetDemoDlg.sbr' [modified]memberjohn_172619 Oct '09 - 5:25 
john_1726 wrote:
Is there anywhere I can download a Microsoft Visual Studio 6 Express edition for C++?

 
David, please disregard my previous message--my boss still has the old Visual Studio CDs.
 
Really appreciate your help--thanks for creating these tools!
 
modified on Monday, October 19, 2009 11:35 AM

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 3 May 2004
Article Copyright 2004 by DavidCrow
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid