Calling the API function Netshareadd () sets the folder to share, and if you do not make other settings after calling this function, the network user cannot access the shared folder because it is in an NTFS partition and is under the NTFS file system access control, so it requires a 2nd step;
Call DOS command cacls, give guest user group read and write permission;
If you want to cancel the file share, call the API function directly Netsharedel ()
For folders in the FAT32 partition, the cacls command is not working, and this command is dedicated to files and folders in NTFS format. This means that the first step of this workaround is not available, so you can now only add file shares using the Netshareadd () function. After calling this function, if you do not make other settings, network users can access the shared folder but have Full control (this is the default sharing permission setting), and in the actual project we want to be able to programmatically control read and write permissions. In the NTFS partition, we also have the NTFS file system access control function hood, in the FAT32 partition can only rely on the operating system folder share its own permissions settings function.
1net_api_status netshareadd (2_in_ lpwstr servername, 3_in_ DWORD level, 4_in_ lpbyte buf, 5_out_ 6struct _share_info_2 {8LPWSTR shi2_netname; 9DWORD shi2_type;10lpwstr Shi2 _remark;11dword shi2_permissions;12dword shi2_max_uses;13dword shi2_current_uses;14lpwstr shi2_ Path;15lpwstr shi2_passwd; share_info_2, *pshare_info_2, *lpshare_info_2;
AddShare.cpp
1 //Description:2 //The FAT system calls this function, and the shi502_permissions parameter is invalid. Have and only share permissions are the highest permissions. 3 //The NTFS system calls this function, modify the shi502_permissions parameter to use the following permissions:4 //Access_read, Access_write, Access_create, Access_exec5 //Access_delete, Access_atrib, Access_perm, Access_all6 BOOL Addshare (LPTSTR lpsharename, LPTSTR lpsharedir)7 {8 net_api_status Res;9 share_info_502 p;Ten OneP.shi502_netname = Lpsharename;//Share name AP.shi502_type =Stype_disktree; -P.shi502_remark =NULL; -P.shi502_permissions =Access_all; thep.shi502_max_uses = shi_uses_unlimited;//Maximum number of links -P.shi502_current_uses =0; -P.shi502_path = Lpsharedir;//paths that need to be shared -P.SHI502_PASSWD =NULL; +p.shi502_reserved =0; -P.shi502_security_descriptor =NULL; + Ares = Netshareadd (NULL,502, (LPBYTE) &p, NULL); at - if(Nerr_success = =Res) - { -printf"Share created.\n"); - } - Else in { -printf"netshareadd Error:%u\n", res); to } + return(res==nerr_success); -}
DelShare.cpp
1 BOOL delshare (LPTSTR lpsharename)2 {3 net_api_status Res;4 5res = Netsharedel (NULL, (LMSTR) Lpsharename,0);6 if(Nerr_success = =Res)7 {8printf"Netsharedel ok.\n");9 }Ten Else One { Aprintf"Netsharedel Error:%u\n", res); - if(Error_access_denied = =Res) - { theprintf"The user does not has access to the requested information.\n"); - } - if(Nerr_netnamenotfound = =Res) - { +printf"The share name does not exist.\n"); - } + } A return(res==nerr_success); at}
Windows all version-implements the specified path share