The Win32 Network Management APIs 43

 

 

 

 

 

NetShareGetInfo() Program Example

 

This function retrieves information about a particular shared resource on a server. The following code sample demonstrates how to retrieve information about a particular shared resource using a call to the NetShareGetInfo() function. The sample calls NetShareGetInfo(), specifying information level 502 (SHARE_INFO_502). If the call succeeds, the code prints the retrieved data. The sample also calls the IsValidSecurityDescriptor() function to validate the shi502_security_descriptor member. Finally, the sample frees the memory allocated for the information buffer.

Create a new empty Win32 console application project. Give the project name and change the project location is needed.

 

NetShareGetInfo() Program Example: Creating new Win32 C++ console application project using Visual C++ .NET

 

Then, add the source file and give it a suitable name.

 

NetShareGetInfo() Program Example: Addding C++ source file for the C++ source code

 

Then, add the following source code.

 

#include <windows.h>

#include <stdio.h>

#include <lm.h>

 

#pragma comment(lib, "Netapi32.lib")

 

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

{

   PSHARE_INFO_502 BufPtr;

   NET_API_STATUS res;

   LPTSTR   lpszServer = NULL, lpszShare;

 

   // Check command line arguments.

   switch(argc)

   {

   case 3:

      lpszServer = lpszArgv[2];

   case 2:

      lpszShare = lpszArgv[1];

      break;

   default:

         {

               wprintf(L"Usage: %s sharename <servername>\n", lpszArgv[0]);

               wprintf(L"Example: %s testsharefolder Bigserver\n", lpszArgv[0]);

               return 1;

         }

   }

 

   // Call the NetShareGetInfo() function, specifying level 502.

   if((res = NetShareGetInfo(lpszServer,lpszShare,502,(LPBYTE *) &BufPtr)) == ERROR_SUCCESS)

   {

         wprintf(L"NetShareGetInfo() is OK!\n\n");

         // Print the retrieved data.

         wprintf(L"Share name       Path  Current Use\n");

         wprintf(L"----------------------------------\n");

         wprintf(L"%s\t%s\t%u\n\n",BufPtr->shi502_netname, BufPtr->shi502_path, BufPtr->shi502_current_uses);

         // Validate the value of the shi502_security_descriptor member.

         if (IsValidSecurityDescriptor(BufPtr->shi502_security_descriptor))

               wprintf(L"It has a valid Security Descriptor.\n");

         else

               wprintf(L"It does not have a valid Security Descriptor.\n");

         // Free the allocated memory.

         NetApiBufferFree(BufPtr);

   }

   else

   {

         wprintf(L"NetShareGetInfo() failed, error: %ld\n", res);

   }

   return 0;

}

 

Build and run the project. The following screenshot is a sample output based on the previous shared folder.

 

NetShareGetInfo() Program Example: The shared folder which information will be extracted

 

NetShareGetInfo() Program Example: A sample program output

 

 

 

 

< Win32 Network Management APIs 42 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 44 >