The following code sample demonstrates how to retrieve information about each shared resource on a server using a call to the NetShareEnum function. The sample calls NetShareEnum, specifying information level 502 ( SHARE_INFO_502). If the call succeeds, the code loops through the entries and prints information about each share. The sample also calls the IsValidSecurityDescriptor function to validate the shi502_security_descriptor member. Finally, the code sample frees the memory allocated for the information buffer.#define UNICODE #include <windows.h> #include <stdio.h> #include <lm.h>void wmain( int argc, TCHAR *lpszArgv[ ]) { PSHARE_INFO_502 BufPtr,p; NET_API_STATUS res; LPTSTR lpszServer = NULL; DWORD er=0,tr=0,resume=0, i; switch(argc) { case 2: lpszServer = lpszArgv[1]; break; default: printf("Usage: NetShareEnum <servername>\n"); return; } // // Print a report header. // printf("Share: Local Path: Uses: Descriptor:\n"); printf("---------------------------------------------------------------------\n"); // // Call the NetShareEnum function; specify 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) { p=BufPtr; // // Loop through the entries; // print retrieved data. // for(i=1;i<=er;i++) { printf("%-20S%-30S%-8u",p->shi502_netname, p->shi502_path, p->shi502_current_uses); // // Validate the value of the // shi502_security_descriptor member. // if (IsValidSecurityDescriptor(p->shi502_security_descriptor)) printf("Yes\n"); else printf("No\n"); p++; } // // Free the allocated buffer. // NetApiBufferFree(BufPtr); } else printf("Error: %ld\n",res); } // Continue to call NetShareEnum while // there are more entries. // while (res==ERROR_MORE_DATA); // end do return; }
#include <windows.h>
#include <stdio.h>
#include <lm.h>void wmain( int argc, TCHAR *lpszArgv[ ])
{
PSHARE_INFO_502 BufPtr,p;
NET_API_STATUS res;
LPTSTR lpszServer = NULL;
DWORD er=0,tr=0,resume=0, i; switch(argc)
{
case 2:
lpszServer = lpszArgv[1];
break;
default:
printf("Usage: NetShareEnum <servername>\n");
return;
}
//
// Print a report header.
//
printf("Share: Local Path: Uses: Descriptor:\n");
printf("---------------------------------------------------------------------\n");
//
// Call the NetShareEnum function; specify 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)
{
p=BufPtr;
//
// Loop through the entries;
// print retrieved data.
//
for(i=1;i<=er;i++)
{
printf("%-20S%-30S%-8u",p->shi502_netname, p->shi502_path, p->shi502_current_uses);
//
// Validate the value of the
// shi502_security_descriptor member.
//
if (IsValidSecurityDescriptor(p->shi502_security_descriptor))
printf("Yes\n");
else
printf("No\n");
p++;
}
//
// Free the allocated buffer.
//
NetApiBufferFree(BufPtr);
}
else
printf("Error: %ld\n",res);
}
// Continue to call NetShareEnum while
// there are more entries.
//
while (res==ERROR_MORE_DATA); // end do
return;
}