The Win32 Network Management APIs 39

 

 

 

 

 

NetShareAdd() Program Example

 

This function shares a server resource. The following code sample demonstrates how to share a network resource using a call to the NetShareAdd() function. The code sample fills in the members of the SHARE_INFO_2 structure and calls NetShareAdd(), specifying information level 2. A password is not required because these platforms do not support share-level security.

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

 

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

 

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

 

NetShareAdd() Program Example: Adding new C++ source file for 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;

   SHARE_INFO_2 p;

   DWORD parm_err = 0;

 

   if(argc < 2)

   {

      wprintf(L"Usage: %s ServerName\n", argv[0]);

      wprintf(L"Example: %s BigfootSvr\n", argv[0]);

   }

   else

   {

      // Fill in the SHARE_INFO_2 structure.

      p.shi2_netname = L"Testsharefolder";   

      p.shi2_type = STYPE_DISKTREE; // disk drive

      p.shi2_remark = L"This is a test share folder using NetShareAdd() function";

      p.shi2_permissions = 0;   

      p.shi2_max_uses = 4;

      p.shi2_current_uses = 0;

      // The following is the folder to be share

      p.shi2_path = L"C:\\MyTestShare";

      p.shi2_passwd = NULL; // no password

 

      // Call the NetShareAdd() function, specifying level 2.

      res=NetShareAdd(argv[1], 2, (LPBYTE) &p, &parm_err);

 

      // If the call succeeds, inform the user.

      if(res==0)

        {

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

              wprintf(L"%s share was created successfully!\n", p.shi2_netname);

        }     

      // Otherwise, print an error, and identify the parameter in error.

      else

         wprintf(L"Error: %u\tparmerr = %u\n", res, parm_err);

   }

   return;

}

 

Build and run the project. Firstly we create a folder, MyTestShare (this folder name has been hard-coded in the program and you may want to change it) which will be shared.

 

NetShareAdd() Program Example: Creating a shared folder

 

Then we run the project. The following screenshot is a sample output.

 

NetShareAdd() Program Example: Creating a shared folder programmatically

 

Refresh the Windows explorer. The MyTestShare folder should be shared (indicated with the holding hand icon).

 

NetShareAdd() Program Example: The shared folder seen in Windows explorer

 

When we invoke the shared folder properties’ page, the Comment also was set as in the program source code.

 

NetShareAdd() Program Example: The shared folder comment created programmatically

 

 

 

 

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