Enumerating all the shared resources information in a network's or domain's machine


Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows Xp Pro SP2

Target platform: none, just for learning and fun

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged. Need to add netapi32.lib (netapi32.dll) to the project. Click the Project menu->Select the your_project_name Properties... sub menu->Expand the Configuration Properties folder on the left pane->Expand the Linker subfolder->Select the Input subfolder->Select the Additional Dependencies field on the right pane->Click the ... at the end of the field->Type in 'netapi32.lib' in the empty pane->Click the OK button->Click the OK button second time to close the project Properties dialog.

To do: Enumerating all the shared resources information in the Windows network's or domain's machine

To show: The various Windows C network management functions - sharing resources





// add the netapi32.lib...

// The return value of share functions code can be found in Network Management Error Codes in MSDN

// For Win 2000 server, adjust accordingly for other Windows version

#define _WIN32_WINNT 0x0500


// #define UNICODE - already compiled as unicode


#include <windows.h>

#include <stdio.h>

#include <lm.h>


int wmain(int argc, TCHAR *lpszArgv[ ])


PSHARE_INFO_502 BufPtr, pth;


LPTSTR lpszServer = NULL;

DWORD er = 0, tr = 0, resume = 0, i;


if(argc != 2)


printf("Usage: %ls <server_name>\n", lpszArgv[0]);

printf("Example: %ls mypersonal\n", lpszArgv[0]);

exit (1);



lpszServer = lpszArgv[1];


// Print a report...

printf("\nShare Local Path Uses Sec. Descriptor\n");


// Call the NetShareEnum() function; specifying level 502.

do // begin do


res = NetShareEnum(lpszServer, 502, (LPBYTE *)&BufPtr, -1, &er, &tr, &resume);

// If the call succeeds...

if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA)


pth = BufPtr;

// Loop through the entries, print the retrieved data.

for(i=1; i<er>shi502_netname, pth->shi502_path, pth->shi502_current_uses);

// Validate the value of the shi502_security_descriptor member.

if (IsValidSecurityDescriptor(pth->shi502_security_descriptor))






// Free the allocated buffer.




printf("Something wrong, error: %ld\n", res);


// Continue to call NetShareEnum() while there are more entries.

while (res == ERROR_MORE_DATA); // end do

return 0;



Output example:



Usage: cplus <server_name>

Example: cplus mypersonal

F:\vc2005project\cplus\debug>cplus mypersonal

Share Local Path Uses Sec. Descriptor


E$ E:\ 0 No

IPC$ 0 No

G$ G:\ 0 No

F$ F:\ 0 No


wwwroot$ c:\inetpub\wwwroot 0 Yes

C$ C:\ 0 No




C and C++ Programming Resources | C & C++ Code Example Index