现在的位置: 首页 > 综合 > 正文

(转)vc++如何设置文件或目录的NTFS共享安全属性

2012年01月23日 ⁄ 综合 ⁄ 共 4877字 ⁄ 字号 评论关闭

要求代码运行在具有管理员权限的账户下。  

PSID   pSid   =   NULL;   //   定义一个指向SID结构的指针  
DWORD   cbSid;   //   存放SID的长度   (in   bytes)  
TCHAR   RefDomain[DNLEN   +   1];   //   缓冲区用来存放所在域的名称    
DWORD   cchDomain   =   DNLEN   +   1;//   可以存入缓冲区的字符的个数   (in   TCHARs)  
SID_NAME_USE   peUse;   //   一个枚举类型,用来指出SID的类型  
SECURITY_DESCRIPTOR   sd;   //   用来描述安全属性的结构  
PACL   pDacl   =   NULL;   //   指向权限控制列表的指针  
DWORD   dwAclSize   ;   //     列表的长度;  
LPTSTR   Username   =   _T( "everyone ");//设定可以访问到此共享资源的用户或组  

SHARE_INFO_502   si502;   //   这是一个结构,用来描述共享资源共资源   享属性  
NET_API_STATUS   res;   //用来显示调用相关函数后的结果,指示调用是否成功  
WCHAR*   szShareName=L "ShareDatabase ";//显示在网络上的共享名  

WCHAR*   szSharePath   =L "f:// ";   //   欲共享的目录  

cbSid   =   96;   //   设定SID的长度为96  
pSid   =   (PSID)HeapAlloc(GetProcessHeap(),   0,   cbSid);       //为SID分配空间  
if(pSid   ==   NULL)   //错误处理  
{  
AfxMessageBox(_T( "HeapAlloc   error!/n "));                    
}              

//       得到可以访问此资源的用户或组的SID.              
if(!LookupAccountName(NULL,   //   [in]   这个参数指明查找的用户或组在哪个系统上,为NULL表示本地系统                    
Username,   //   [in]   欲授予访问权限的用户或组                    
pSid,   //   [out]   存放返回的SID值                  
&cbSid,   //   [in,out]   进去的是你设定的缓冲区长度,出来的是实际SID的长度                    
RefDomain,   //   [out]   域名                    
&cchDomain,//   [in,out]   长度                    
&peUse   ))   //   [out]   结构,用来指示用户的类型  
{                                                                              
if(GetLastError()   ==   ERROR_INSUFFICIENT_BUFFER)   //   如果缓冲区不足,   try   again  
{                              
pSid   =   (PSID)HeapReAlloc(GetProcessHeap(),   0,   pSid,   cbSid);                              
if(pSid   ==   NULL)    
{                                    
AfxMessageBox(_T( "HeapReAlloc   error!/n "));                                    
}                              
cchDomain   =   DNLEN   +   1;                              
if(!LookupAccountName(NULL,                                    
Username,                                  
pSid,                                
&cbSid,                          
RefDomain,                                        
&cchDomain,                              
&peUse                                   ))    
{                                            
AfxMessageBox(_T( "LookupAccountName   error!   (rc=%lu)/n "),   GetLastError());                                            
}                      
}    
else    
{                            
AfxMessageBox(_T( "LookupAccountName   error!   (rc=%lu)/n "),   GetLastError());                            
}            
}      

//   计算权限控制列表所需长度                    
dwAclSize   =   sizeof(ACL)   +1   *   (   sizeof(ACCESS_ALLOWED_ACE)   -   sizeof(DWORD)   )   +   GetLengthSid(pSid)   ;          

//     为pDacl分配所需的空间                        
pDacl   =   (PACL)HeapAlloc(GetProcessHeap(),   0,   dwAclSize);            
if(pDacl   ==   NULL)   return;    

//   将pDacl初始化  
InitializeAcl(pDacl,   dwAclSize,   ACL_REVISION);        
           
//     授予GENERIC_ALL   权限授于得到的SID,并加入到列表中              
AddAccessAllowedAce(pDacl,ACL_REVISION,GENERIC_ALL,pSid);    

//     初始化   SECURITY_DESCRIPTOR       结构的实例sd      
InitializeSecurityDescriptor(&sd,   SECURITY_DESCRIPTOR_REVISION)   ;                

//   将pDacl加入到sd中  
SetSecurityDescriptorDacl(&sd,   TRUE,   pDacl,   FALSE);    

//   填充SHARE_INFO_502   结构  
si502.shi502_netname   =(char*)   szShareName;   //   共享名  
si502.shi502_type   =   STYPE_DISKTREE;                   //     资源类型--文件      
si502.shi502_remark   =   NULL;   //   别名      
si502.shi502_max_uses   =   SHI_USES_UNLIMITED;//   最大连接数  
si502.shi502_permissions   =   ACCESS_ALL;   //   访问权限  
si502.shi502_current_uses   =   0;   //   当前连接  
si502.shi502_path   =   (char*)   szSharePath;   //   共享的目录  
si502.shi502_passwd   =   NULL;   //   访问密码  
si502.shi502_reserved   =   0;   //   保留字段  
si502.shi502_security_descriptor   =   &sd;   //   安全描述符  

//   将资源共享  
res   =   NetShareAdd(  
NULL,   //   NULL表示共享资源在本地系统上  
502,                         //   表示共享信息描述用SHARE_INFO_502结构  
(LPBYTE)&si502,   //   存放SHARE_INFO_502结构的缓冲区  
NULL                         //   存放错误信息,可以不用  
);  

//   根据返回信息,判断共享操作是否成功  
if(res==NERR_Success)  
AfxMessageBox( "Share   created ");  
else   if(res==ERROR_ACCESS_DENIED)  
AfxMessageBox( "The   user   does   not   have   access   to   the   requested   information ");  
else   if(res==ERROR_INVALID_LEVEL)  
AfxMessageBox( "The   value   specified   for   the   level   parameter   is   invalid.   ");  
else   if(res==ERROR_INVALID_NAME)  
AfxMessageBox( "The   character   or   file   system   name   is   invalid. ");  
else   if(res==ERROR_INVALID_PARAMETER)  
AfxMessageBox( "The   specified   parameter   is   invalid. ");  
else   if(NERR_DuplicateShare==res)  
AfxMessageBox( "The   share   name   is   already   in   use   on   this   server. ");  
else   if(NERR_RedirectedPath==res)  
AfxMessageBox( "The   operation   is   invalid   for   a   redirected   resource.   The   specified   device   name   is   assigned   to   a   shared   resource. ");  
else   if(NERR_UnknownDevDir==res)  
AfxMessageBox( "The   device   or   directory   does   not   exist.   ");  
else  
AfxMessageBox( "dfdfd ");  

抱歉!评论已关闭.