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.

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

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(LUsage: %s ServerName\n, argv[0]);
wprintf(LExample: %s BigfootSvr\n, argv[0]);
}
else
{
// Fill in the SHARE_INFO_2 structure.
p.shi2_netname = LTestsharefolder;
p.shi2_type = STYPE_DISKTREE; // disk drive
p.shi2_remark = LThis 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 = LC:\\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(LNetShareAdd() 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(LError: %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.

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

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

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