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.
#pragma comment(lib, "Netapi32.lib")
void wmain(int argc, WCHAR *argv[ ])
DWORD parm_err = 0;
if(argc < 2)
wprintf(L"Usage: %s ServerName\n", argv);
wprintf(L"Example: %s BigfootSvr\n", argv);
// 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, 2, (LPBYTE) &p, &parm_err);
// If the call succeeds, inform the user.
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.
wprintf(L"Error: %u\tparmerr = %u\n", res, parm_err);
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.