Enumerating or listing all the Windows shared resources information in a local 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 or listing all the shared resources information in a local machine

To show: The various Windows C Win32 network management functions - listing the Windows shared resources

 

 

// add the netapi32.lib...

// Network Management Error Codes, Platform SDK: Network Management

// For Win Xp Pro, adjust accordingly for other Windows version

#define _WIN32_WINNT 0x0501

 

// #define UNICODE - already compiled as unicode set by compiler

 

#include <windows.h>

#include <stdio.h>

#include <lm.h>

 

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

{

PSHARE_INFO_502 BufPtr, pth;

NET_API_STATUS res;

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);

}

else

lpszServer = lpszArgv[1];

// Print a report header.

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

printf("----------------------------------------------------------------\n");

 

// Call the NetShareEnum function; specify level 502.

do // begin the do loop

{

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))

printf("Yes\n");

else

printf("No\n");

pth++;

}

// Free the allocated buffer.

NetApiBufferFree(BufPtr);

}

else

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:

 

F:\vc2005project\cplus\debug>cplus

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

testshare E:\mytestfolder 0 No

G$ G:\ 0 No

F$ F:\ 0 No

ADMIN$ C:\WINDOWS 0 No

wwwroot$ c:\inetpub\wwwroot 0 Yes

C$ C:\ 0 No

F:\vc2005project\cplus\debug>

 

 

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