Operating System Environment: Do not use simple file sharing. The disk where the shared folder is located is the NTFS file system.
(1) set a dedicated folder for the shared account
Create a shared folder. if it already exists, it is not created and set to everyone access, and set to access by the specified user.
Bool cwindowtaskhelper ::Createsharefolder(Cstring strfoldfullpath, cstring strowner, cstring strpassword, cstring strsharename)
{
Bool bresult = false;
Cfilefind FileFind;
If (FileFind. findfile (strfoldfullpath ))
{
Seteveryoneaccessfolder(Strfoldfullpath );
Bresult = true;
FileFind. Close ();
}
Else
{
Bresult =Createfolder(Strfoldfullpath );
}
If (bresult)
{
Hinstance = NULL;
Bresult =Adjustanderefolder(Strfoldfullpath, strowner, strpassword );
Assert (bresult );
}
If (! Bresult)
{
Vscontroltools: vsmessagebox (_ T ("An error occurred while creating the shared folder! "), Mb_ OK, _ T (" prompt "));
}
Return bresult;
}
FunctionCreatefolder:Create a folder accessed by everyone. if it already exists, it will not be created.
Bool cwindowtaskhelper ::Createfolder(Cstring strfoldfullpath)
{
Bool bresult = false;
Tchar tcchar [max_path];
Memset (tcchar, 0, max_path * (sizeof (tchar )));
Cfilefind FileFind;
Bresult = FileFind. findfile (strfoldfullpath );
If (! Bresult)
{
Security_attributes SA;
Security_descriptor SD;
Initializesecuritydescriptor (& SD, security_descriptor_revision );
Setsecuritydescriptordacl (& SD, true, null, false );
SA. nlength = sizeof (security_attributes );
SA. binherithandle = true;
SA. lpsecuritydescriptor = & SD;
Bresult = createdirectory (strfoldfullpath, & SA );
}
Else
{
FileFind. Close ();
}
# Ifdef _ debug
If (! Bresult)
{
Vscontroltools: vsmessagebox (_ T ("failed to create folder! "), Mb_ OK, _ T (" prompt "));
}
# Endif
Return bresult;
}
FunctionAdjustanderefolder // set the user permission for the shared folder. The sharing permission is only implemented by using the CMD cacls command for a dedicated account.
Bool cwindowtaskhelper ::Adjustanderefolder(Cstring strfoldfullpath, cstring strowner, cstring strpassword)
{
Bool bresult = false;
Hinstance = NULL;
Cstring strtmp = _ T ("");
// Enable everyone
Strtmp. Format (_ T ("/C echo y | cacls % S/T/P everyone: F"), strfoldfullpath );
# Ifdef _ debug
: Afxmessagebox (strtmp );
# Endif
ShellExecute (null, _ T ("open"), _ T ("cmd.exe"), strtmp, null, sw_hide );
// Set the shared folder
Bresult =Sharefolderforuser(Strfoldfullpath, strowner, strpassword );
Assert (bresult );
// Set the folder operation permissions of the specified account
Strtmp. Format (_ T ("/C echo y | cacls % S/T/P % s: F"), strfoldfullpath, strowner );
# Ifdef _ debug
: Afxmessagebox (strtmp );
# Endif
Hinstance = ShellExecute (null, _ T ("open"), _ T ("cmd.exe"), strtmp, null, sw_hide );
// Add a system for use in the service
Strtmp. Format (_ T ("/C cacls % S/T/e/g system: F"), strfoldfullpath );
# Ifdef _ debug
: Afxmessagebox (strtmp );
# Endif
Hinstance = ShellExecute (null, _ T ("open"), _ T ("cmd.exe"), strtmp, null, sw_hide );
Bresult = (INT) hinstance> 32 );
// Set the Administrator's permission to operate folders
Strtmp. Format (_ T ("/C cacls % S/T/e/g administrators: F"), strfoldfullpath );
Hinstance = ShellExecute (null, _ T ("open"), _ T ("cmd.exe"), strtmp, null, sw_hide );
Bresult = (INT) hinstance> 32 );
Assert (bresult );
// Set the folder operation permissions of the current Login User
Cstring strhostname = _ T ("");
Cstring strcurrentuser = _ T ("");
DWORD dwmax = max_path;
Tchar tcchar [max_path];
Memset (tcchar, 0, max_path * (sizeof (tchar )));
: Getcomputername (tcchar, & dwmax );
Strhostname = tcchar;
Dwmax = max_path;
Memset (tcchar, 0, max_path * (sizeof (tchar )));
Tchar * szlogname = NULL;
If (wtsquerysessioninformation (wts_current_server_handle, wts_current_session, wtsusername, & szlogname, & dwmax ))
{
Strcurrentuser = szlogname;
Wtsfreememory (szlogname );
}
Strtmp. Format (_ T ("/C cacls % S/T/e/g % S // % s: F"), strfoldfullpath, strhostname, strcurrentuser );
Hinstance = ShellExecute (null, _ T ("open"), _ T ("cmd.exe"), strtmp, null, sw_hide );
Bresult = (INT) hinstance> 32 );
Assert (bresult );
Return bresult;
}
Function extends folderforuser: // Folder is shared and specified as user access
Bool cwindowtaskhelper ::Sharefolderforuser(Cstring strfoldfullpath, cstring struser, cstring strpassword, cstring strsharename)
{
Bool bresult = false;
Security_descriptor SD;
# Ifdef _ debug
Cstring strtip = _ T ("");
Strtip. Format (_ T ("Path = % s, user = % s, password = % s, ready to share !! "), Strfoldfullpath, struser, strpassword );
Vscontroltools: vsmessagebox (strtip, mb_ OK, _ T ("prompt "));
# Endif
If (Setnetuseraccesspower(Struser, & SD ))
{
Pai_info_502 P;
If (strsharename. isempty ())
{
Strsharename = strfoldfullpath. Right (strfoldfullpath. getlength ()-strfoldfullpath. reversefind (_ T ('/')-1 );
}
P. shi502_netname = strsharename. getbuffer ();
P. shi502_type = stype_disktree; // disk drive
P. shi502_remark = NULL;
P. shi502_permissions = access_all;
P. shi502_max_uses =-1;
P. shi502_current_uses = 0;
P. shi502_path = strfoldfullpath. getbuffer ();
P. shi502_passwd = strpassword. getbuffer ();
P. shi502_reserved = 0;
P. shi502_security_descriptor = & SD;
DWORD parm_err = 0;
Net_api_status res =Netmask add(Null, 502, (lpbyte) & P, & parm_err );
If (RES = no_error | res = 2118) // 2118 indicates that the share already exists.
{
# Ifdef _ debug
Cstring strtip = _ T ("");
Strtip. Format (_ T ("Shared File % s successful! "), Strfoldfullpath );
Vscontroltools: vsmessagebox (strtip, mb_ OK, _ T ("prompt "));
# Endif
Bresult = true;
}
# Ifdef _ debug
Else
{
Cstring strtip = _ T ("");
Strtip. Format (_ T ("res = % d, shared file % s failed! "), Res, strfoldfullpath );
Vscontroltools: vsmessagebox (strtip, mb_ OK, _ T ("prompt "));
}
# Endif
}
Return bresult;
}
FunctionSetnetuseraccesspower: Specify the permissions in "share" under "properties" of the file as a dedicated account and configure the "share" permission table for the folder.
Security_descriptor-> ACL-> ace-> Sid
Bool cwindowtaskhelper ::Setnetuseraccesspower(Cstring struser, psecurity_descriptor PSD, DWORD dwpower)
{
Byte aclbuffer [1024];
PACl = (PACl) & aclbuffer; // declare an ACL. The length is 1024.
Byte sidbuffer [100];
Psid = (psid) & sidbuffer; // declare a Sid. The length is 100.
DWORD sidbuffersize = 100;
Tchar domainbuffer [80];
DWORD domainbuffersize = 80; sid_name_use snu;
// Initialize an SD
Initializesecuritydescriptor (PSD, security_descriptor_revision );
// Initialize an ACL
Initializeacls (PACl, 1024, acl_revision );
// Search for a user and obtain the user's Sid
Bool bresult = lookupaccountname (0,
Struser. getbuffer (),
Psid,
& Sidbuffersize,
Domainbuffer,
& Domainbuffersize,
& Snu );
If (bresult)
{
// Set this user's access-allowed ace with the "All Permissions" permission"
Bresult = addaccessallowedace (PACl, acl_revision, dwpower, psid );
// Sidbuffersize = 100;
// Domainbuffersize = 80;
/// Add "system"
// Bresult = lookupaccountname (0, _ T ("system"), psid, & sidbuffersize, domainbuffer, & domainbuffersize, & snu );
// Assert (bresult );
// Bresult = addaccessallowedace (PACl, acl_revision, dwpower, psid );
// Set ACL to SD
If (bresult)
{
Setsecuritydescriptordacl (PSD, true, PACl, false );
}
# Ifdef _ debug
Else
{
Vscontroltools: vsmessagebox (_ T ("failed to add ace! "), Mb_ OK, _ T (" prompt "));
}
# Endif
}
# Ifdef _ debug
Else
{
Cstring strtip = _ T ("");
Strtip. Format (_ T ("User '% s' does not exist in the system '! "), Struser );
Vscontroltools: vsmessagebox (strtip, mb_ OK, _ T ("prompt "));
}
# Endif
Return bresult
}
(2) Cancel sharing folders
Bool cwindowtaskhelper ::Disableappsfolderforuser(Cstring strfoldfullpath, cstring strsharename)
{
Bool bresult = false;
Cfilefind FileFind;
If (FileFind. findfile (strfoldfullpath ))
{
If (strsharename. isempty ())
{
Int IPOs = strfoldfullpath. getlength ()-strfoldfullpath. reversefind (_ T ('//')-1;
Strsharename = strfoldfullpath. Right (IPOs );
}
Net_api_status res;
Res = netdomaindel (null, strsharename. getbuffer (), 0 );
If (nerr_success = res)
{
Bresult = true;
Cstring strtmp = _ T ("");
Strtmp. Format (_ T ("/C echo y | cacls % S/T/P everyone: F"), strfoldfullpath); // specify it as everyone for access
Hinstance = ShellExecute (null, _ T ("open"), _ T ("cmd.exe"), strtmp, null, sw_hide );
}
# Ifdef _ debug
Else
{
Cstring strtip = _ T ("");
Strtip. Format (_ T ("failed to delete share % s"), strsharename );
Vscontroltools: vsmessagebox (strtip, mb_ OK, _ T ("prompt "));
}
# Endif
}
Else
{
Bresult = true;
}
Return bresult;
}
The above uses netmask add () and netmask del () to share and cancel sharing.
The other method is to run the CMD net share command, but I have not found a way to set the "permission" under the "share" folder, in addition, this method restricts shared access through the Folder Security Attribute. In actual use, the access may fail because it has not been found. We recommend that you use the first method to set sharing because you can set "Permissions" under "sharing" in the file attributes ".
Strtmp. Format (_ T ("/C net share % s = % s"), strsharename, strfoldfullpath );
Strtmp. Format (_ T ("/C net share % S/Delete"), strsharename );
Hinstance = ShellExecute (null, _ T ("open"), _ T ("cmd.exe"), strtmp, null, sw_hide );