哪位热心人能帮我解决这问题,200分答谢

tccsdn 2005-02-28 11:23:23
加精
下面这段代码在BCB里面死活编译不过

void pfree(void *p)
{
if(p)
free(p);
}
//加用户到HANDLE
bool AddUserPrivToHandle(HANDLE Hhandle,char *s,ACCESS_MODE mode)
{
PSECURITY_DESCRIPTOR pSecurityDescriptor1,pSD = NULL;
DWORD size,size1,len,ErrorCode,DaclPresent,DaclDefaulted,dwAbsoluteSDSize,
dwDaclSize,dwSaclSize,dwOwnerSize,dwPrimaryGroupSize;
ACL OldAcl;
PACL POldAcl,PNewAcl,pDacl,pSacl;
PSID pOwner,pPrimaryGroup;
EXPLICIT_ACCESS ExplicitAccess1;
SECURITY_INFORMATION sinfo;

dwAbsoluteSDSize = dwDaclSize = dwSaclSize = dwOwnerSize = dwPrimaryGroupSize = 0;

size = 0;
sinfo = DACL_SECURITY_INFORMATION;
//获得SECURITY_DESCRIPTOR
GetUserObjectSecurity(Hhandle,&sinfo,pSD,size,&len);
ErrorCode = GetLastError();
if(ErrorCode == ERROR_INSUFFICIENT_BUFFER) //122
{
pSD = (PSECURITY_DESCRIPTOR) malloc(len + 1);
if(pSD == NULL)
{
printf("Malloc failed:%d\n",GetLastError());
return FALSE;
}
memset(pSD,0,len + 1);
size = len;
}
else
{
printf("GetUserObjectSecurity in AddUserPrivToHandle(\"%s\") Failed:%d\n",s,ErrorCode);
return FALSE;
}
if(!GetUserObjectSecurity(Hhandle,&sinfo,pSD,size,&len))
{
printf("GetUserObjectSecurity in AddUserPrivToHandle(\"%s\") Failed:%d\n",s,ErrorCode);
return FALSE;
}
//获得DACL
POldAcl = NULL;
LPBOOL a;
if(!GetSecurityDescriptorDacl( pSD, (int *)&DaclPresent, &POldAcl, (int*)&DaclDefaulted ) )
{
printf("GetSecurityDescriptorDacl Error:%d\n",GetLastError());
return FALSE;
}
//重新生成一个ACL,然后在后面合并进去,给administrators组全部的权限.
memset(&ExplicitAccess1,0,sizeof(ExplicitAccess1));
BuildExplicitAccessWithName(&ExplicitAccess1,s,mode,GRANT_ACCESS,NO_INHERITANCE);
//合并权限
ErrorCode = SetEntriesInAcl(1,&ExplicitAccess1,POldAcl,&PNewAcl);
if(ErrorCode != ERROR_SUCCESS)
{
printf("SetEntriesInAcl Error:%d\n",ErrorCode);
return FALSE;
}

dwAbsoluteSDSize = 0x400;
pSecurityDescriptor1 = (PSECURITY_DESCRIPTOR) malloc(dwAbsoluteSDSize+1);
if(pSecurityDescriptor1 == NULL)
{
printf("Malloc for MakeAbsoluteSD failed:%d\n",GetLastError());
return FALSE;
}
memset(pSecurityDescriptor1,0,dwAbsoluteSDSize+1);

MakeAbsoluteSD( pSD,
pSecurityDescriptor1,
&dwAbsoluteSDSize,
NULL,
&dwDaclSize,
NULL,
&dwSaclSize,
NULL,
&dwOwnerSize,
NULL,
&dwPrimaryGroupSize);
//申请内存先.
ErrorCode = GetLastError();

if(ErrorCode == ERROR_INSUFFICIENT_BUFFER)
{
pDacl = (PACL) malloc(dwDaclSize+1);
pSacl = (PACL) malloc(dwSaclSize+1);
pOwner = (PSID) malloc(dwOwnerSize+1);
pPrimaryGroup = (PSID) malloc(dwPrimaryGroupSize+1);

if( (pDacl == NULL) ||
(pSacl == NULL) ||
(pOwner == NULL) ||
(pPrimaryGroup == NULL))
{
printf("Malloc for MakeAbsoluteSD failed:%d\n",GetLastError());
return FALSE;
}
}
else
{
printf("MakeAbsoluteSD Error:%d\n",GetLastError());
return FALSE;
}
//申请后就可以接受了
if(!MakeAbsoluteSD(pSD,
pSecurityDescriptor1,
&dwAbsoluteSDSize,
pDacl,
&dwDaclSize,
pSacl,
&dwSaclSize,
pOwner,
&dwOwnerSize,
pPrimaryGroup,
&dwPrimaryGroupSize))
{
printf("MakeAbsoluteSD After Malloc Error:%d\n",GetLastError());
return FALSE;
}

//设置新的DACL
if(!SetSecurityDescriptorDacl(pSecurityDescriptor1,DaclPresent,PNewAcl,DaclDefaulted))
{
printf("SetSecurityDescriptorDacl Error:%d\n",GetLastError());
return FALSE;
}
//检查新的SecurityDescriptor是否合法
if(!IsValidSecurityDescriptor(pSecurityDescriptor1))
{
printf("pSecurityDescriptor1 is not a valid SD:%d\n",GetLastError());
return FALSE;
}

//给句柄设置新的ACL
if(!SetUserObjectSecurity(Hhandle,&sinfo,pSecurityDescriptor1))
{
printf("SetKernelObjectSecurity Error:%d\n",GetLastError());
return FALSE;
}
if(POldAcl)
LocalFree(POldAcl);
if(PNewAcl)
LocalFree(PNewAcl);
pfree(pSD);
pfree(pSecurityDescriptor1);
pfree(pDacl);
pfree(pSacl);
pfree(pOwner);
pfree(pPrimaryGroup);
return TRUE;
}

其中要用到的头文件
#include <Accctrl.h>
#include <Aclapi.h>

#include <Accctrl.hpp>
#include <Aclapi.hpp>
实在搞不清楚是用.H 还是.HPP的头文件,哪位高手能解释下当一个头文件的.h文件和.hpp文件同时存在的时候,什么情况下用哪种头文件?怎么避免他们的冲突问题
...全文
301 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
僵哥 2005-03-01
  • 打赏
  • 举报
回复
我使用的是
#include <Accctrl.h>
#include <Aclapi.h>
编译过了,是控制台的,但是没使用该函数。
僵哥 2005-03-01
  • 打赏
  • 举报
回复
#include <stdio.h>
Maconel 2005-03-01
  • 打赏
  • 举报
回复
还是用.h文件,把Aclapi.h里边所有的EXPLICIT_ACCESS都该为_WINNT_::EXPLICIT_ACCESS,即可通过编译。
Maconel 2005-03-01
  • 打赏
  • 举报
回复
直接加aclapi.h编译通不过是因为PACCESS_MASK冲突了。
在windows.hpp中和winnt.h中都有定义。
在aclapi.hpp中能通过是因为它给你定义了一个namespace。

把头文件都用hpp后,我这里编译有3个错误,一个还是PACCESS_MASK的定义错误,在accctrl.h中也有他的定义,只要改为Accctrl::EXPLICIT_ACCESS就可以了,剩下2个错误是有2个函数没定义,
BuildExplicitAccessWithName和SetEntriesInAcl,不知道是哪里来的文件,也许是你自己定义的?
constantine 2005-03-01
  • 打赏
  • 举报
回复
#include <Accctrl.h>
#include <Aclapi.h>
是可以的,函数我没有办法试,bcb用不了,没有时间装

hpp好象是delphi的东西转换过来的,象控件很多就是这样,我也不是很清楚,照理应该只有一个吧
tccsdn 2005-03-01
  • 打赏
  • 举报
回复
在我这里加了#include <Aclapi.h>的话,不管是控制台过窗口程序都通不过(没写一行代码,就加了一个头文件,在2台电脑上的BCB里面测试都是一样的结果),用aclapi.hpp可以通过,但加了函数就通不过了,

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧