The Win32 Network Management APIs 41

 

 

 

 

 

NetShareDel() Program Example

 

This function deletes a share name from a server's list of shared resources, disconnecting all connections to the shared resource. The extended function NetShareDelEx() allows the caller to specify a SHARE_INFO_0, SHARE_INFO_1, SHARE_INFO_2, SHARE_INFO_502, or SHARE_INFO_503 structure. This function applies only to Server Message Block (SMB) shares. For other types of shares, such as Distributed File System (DFS) or WebDAV shares, use Windows Networking (WNet) functions, which support all types of shares. Only members of the Administrators, Server Operators, or Power Users local group, or those with Server Operator group membership, can successfully delete file shares with a call to the NetShareDel() function. The Print Operator can delete printer shares. If you are programming for Active Directory, you may be able to call certain Active Directory Service Interface (ADSI) methods to achieve the same functionality you can achieve by calling the network management share functions.

The following code sample demonstrates how to delete a share using a call to the NetShareDel() function.

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

 

NetShareDel() Program Example: Creating new Win32 C++ empty console application project

 

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

 

NetShareDel() Program Example: Adding new 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")

 

void wmain(int argc, WCHAR *argv[ ])

{

   NET_API_STATUS res;

 

   if(argc < 3)

   {

         wprintf(L"Usage: NetShareDel servername sharename\n");

         wprintf(L"Example: NetShareDel Mikeserver Myfolder\n");

   }

   else

   {

      // Call the NetShareDel() function to delete the share.

      res=NetShareDel(argv[1], argv[2], 0);

 

      // Display the result of the call.

      if(res==0)

        {

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

              wprintf(L"%s share was removed successfully!\n", argv[2]);

        }

      else

        {

              wprintf(L"NetShareDel() failed! Error %u\n", res);

        }

   }

   return;

}

 

We would like to delete the previous share. Take note that the share name will be used in the program.

 

NetShareDel() Program Example: The shared folder that will be removed

 

So the share name, Testsharefolder will be used as the second parameter.

 

NetShareDel() Program Example: Removing the shared folder by using the shared name as the argument

 

Build and run the project. The following screenshot is a sample output.

 

NetShareDel() Program Example: A sample console program output

 

The share should be removed as shown in the following screenshot.

 

NetShareDel() Program Example: The shared folder removed programmatically

 

 

 

 

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