创建 everyone full control 的全局事件对象

#include <windows.h>
#include <stdio.h>

LPVOID BuildRestrictedSD(PSECURITY_DESCRIPTOR pSD) 
{
	
	DWORD  dwAclLength;
	PSID   psidEveryone = NULL;
	PACL   pDACL = NULL;
	BOOL   bResult = FALSE;
	PACCESS_ALLOWED_ACE pACE = NULL;
	SID_IDENTIFIER_AUTHORITY siaWorld = SECURITY_WORLD_SID_AUTHORITY  ;
	SECURITY_INFORMATION si = DACL_SECURITY_INFORMATION;
	
	__try {
		
		// initialize the security descriptor
		if (!InitializeSecurityDescriptor(pSD, 
			SECURITY_DESCRIPTOR_REVISION)) {
			__leave;
		}
		
		// obtain a sid for the Authenticated Users Group
		if (!AllocateAndInitializeSid(&siaWorld, 1, 
			SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, 
			&psidEveryone)) {
			__leave;
		}
		
		// NOTE:
		// 
		// The Authenticated Users group includes all user accounts that
		// have been successfully authenticated by the system. If access
		// must be restricted to a specific user or group other than 
		// Authenticated Users, the SID can be constructed using the
		// LookupAccountSid() API based on a user or group name.
		
		// calculate the DACL length
		dwAclLength = sizeof(ACL)
			// add space for Authenticated Users group ACE
			+ sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)
			+ GetLengthSid(psidEveryone);
		
		// allocate memory for the DACL
		pDACL = (PACL) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
			dwAclLength);
		if (!pDACL) {
			__leave;
		}
		
		// initialize the DACL
		if (!InitializeAcl(pDACL, dwAclLength, ACL_REVISION)) {
			__leave;
		}
		
		// add the Authenticated Users group ACE to the DACL with
		// GENERIC_READ, GENERIC_WRITE, and GENERIC_EXECUTE access
		if (!AddAccessAllowedAce(pDACL, ACL_REVISION,
			GENERIC_ALL,
			psidEveryone)) {
			__leave;
		}
		// set the DACL in the security descriptor
		if (!SetSecurityDescriptorDacl(pSD, TRUE, pDACL, FALSE)) {
			__leave;
		}
		
		bResult = TRUE;
		
	} 
	__finally {
		if (psidEveryone) FreeSid(psidEveryone);
	}
	
	if (bResult == FALSE) {
		if (pDACL) HeapFree(GetProcessHeap(), 0, pDACL);
		pDACL = NULL;
	}
	
	return (LPVOID) pDACL;
}

// The following function frees memory allocated in the
// BuildRestrictedSD() function
VOID FreeRestrictedSD(LPVOID ptr) 
{
	if (ptr) 
		HeapFree(GetProcessHeap(), 0, ptr);
}

int main(int argc,char **argv)
{
	LPVOID ptr;
	HANDLE hEvent;
	SECURITY_ATTRIBUTES sa;
	SECURITY_DESCRIPTOR sd;
	sa.nLength = sizeof(sa);
	sa.lpSecurityDescriptor = &sd;
	sa.bInheritHandle = FALSE;
	// build a restricted security descriptor
	ptr = BuildRestrictedSD(&sd);
	hEvent = CreateEvent(&sa,FALSE,FALSE,"Global\\xxxx");
	FreeRestrictedSD(ptr);
	if(hEvent)
	{
		printf("Create event OK \n");
	}
	else
		printf("error :%d \n",GetLastError());
	getchar();
	return 0;
}

转载于:https://my.oschina.net/sincoder/blog/119267

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值