Creating a shared (folder) with remarks and password
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows Xp Pro SP2
Target platform: none, just for learning and fun
Header file: Standard and Windows
Additional library: [url=https://www.tenouk.com/clabworksheet/windowspsdk.html]Windows Platform SDK[/url]
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: non-CLR or unmanaged. Need to add netapi32.lib (netapi32.dll) to the project. Click the Project menu->Select the your_project_name Properties... sub menu->Expand the Configuration Properties folder on the left pane->Expand the Linker subfolder->Select the Input subfolder->Select the Additional Dependencies field on the right pane->Click the ... at the end of the field->Type in 'netapi32.lib' in the empty pane->Click the OK button->Click the OK button second time to close the project Properties dialog.
To do: Creating the Windows shared (folder) with some remarks and password programmatically
To show: The various Windows network management C functions - sharing the resources resources
// Don't forget to link to netapi32.lib
// The return value of share functions code can be found in Network Management Error Codes in MSDN
// For Win Xp Pro, adjust accordingly for other Windows version
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <stdio.h>
// need netapi32.lib
#include <lm.h>
// Unicode main()...
int wmain(int argc, TCHAR *argv[ ])
{
NET_API_STATUS res;
SHARE_INFO_2 p;
DWORD parm_err = 0;
// Some prompt...
if(argc < 5)
{
printf("Usage: %ls <server_name> <share_name> <share_remark> <share_pass>\n", argv[0]);
printf("Example: %ls mypersonal testshare \"setting shared folder\" 123abc\n", argv[0]);
}
else
{
// Fill in the SHARE_INFO_2 structure.
p.shi2_netname = ((LPWSTR)argv[2]);
p.shi2_type = STYPE_DISKTREE; // disk drive including the directory...
p.shi2_remark = ((LPWSTR)argv[3]); // share remark
p.shi2_permissions = ACCESS_ALL; // all permission
p.shi2_max_uses = -1; // unlimited
p.shi2_current_uses = 0; // no current uses
// Try finding a way to accept the share path through the command line...
p.shi2_path = TEXT("E:\\mytestfolder"); // share path, here we want to share a folder
// make sure the folder is there....
p.shi2_passwd = argv[4]; // 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)
printf("%ls share created successfully.\n", p.shi2_netname);
// Otherwise, print an error, and identify the parameter in error.
else
printf("Failed to create %ls, error: %u parmerr = %u\n", p.shi2_netname, res, parm_err);
}
return 0;
}
Output example:
(This program run at the command prompt)
F:\vc2005project\cplus\debug>cplus
Usage: cplus <server_name> <share_name> <share_remark> <share_pass>
Example: cplus mypersonal testshare "setting shared folder" 123abc
F:\vc2005project\cplus\debug>cplus mypersonal testshare "test shared folder" xyz123
testshare share created successfully.
F:\vc2005project\cplus\debug>