获取本地共享资源详细信息的源码


void GetLocalShareResource(TiXmlElement *pData)
{
	if (pData == NULL)
		return;

	USES_CONVERSION;
	PSHARE_INFO_502 BufPtr,p;
	NET_API_STATUS res;
	LPTSTR lpszServer = NULL;
	DWORD er = 0,tr = 0,resume = 0, i;
	const DWORD INITIAL_SIZE = 260;

	// Call the NetShareEnum function; specify level 502.
	do // begin do
	{
		res = NetShareEnum(lpszServer, 502, (LPBYTE *) &BufPtr, MAX_PREFERRED_LENGTH, &er, &tr, &resume);
		// If the call succeeds,
		if(res != ERROR_SUCCESS && res != ERROR_MORE_DATA)
		{
			break;
		}

		p=BufPtr;
		for( i = 0;i < er; i++, p++)
		{
			PEXPLICIT_ACCESS pEA = NULL;
			PACL pDacl = NULL;
			DWORD dwCount = 0;
			DWORD dwRet = 0;

			DWORD dwType = p->shi502_type & 0xF;
			if (dwType == STYPE_DEVICE)
			{
				continue;
			}
			else if (dwType == STYPE_IPC)
			{
				dwType--;
			}

			TiXmlElement *pShare = new TiXmlElement("Shareresource");
			pShare->SetAttribute("type", dwType);
			pShare->SetAttribute("sharename", T2UTF8(p->shi502_netname));
			pShare->SetAttribute("desc", T2UTF8(p->shi502_remark));
			pShare->SetAttribute("fullpath", T2UTF8(p->shi502_path));
			pData->LinkEndChild(pShare);

			// Validate the value of the shi502_security_descriptor member.
			BOOL bDaclPresent  = FALSE;
			BOOL bDaclDefaulted  = TRUE;
			if (!IsValidSecurityDescriptor(p->shi502_security_descriptor) ||
				!GetSecurityDescriptorDacl(p->shi502_security_descriptor, &bDaclPresent, &pDacl, &bDaclDefaulted))
			{
				continue;
			}

			if (!bDaclPresent || pDacl == NULL)
			{
				continue;
			}

			if (GetExplicitEntriesFromAcl(pDacl, &dwCount, &pEA) != ERROR_SUCCESS)
			{
				continue;
			}

			TCHAR szName[INITIAL_SIZE];
			TCHAR szDomainName[INITIAL_SIZE];
			for (DWORD nIndex = 0; nIndex < dwCount; nIndex++)
			{
				DWORD dwPermission, dwAccMode; 
				LPTSTR lpSid = NULL;
				DWORD dwNameLen = INITIAL_SIZE, dwDomainLen = INITIAL_SIZE;
				SID_NAME_USE eSidType;

				ZeroMemory(szName, sizeof(szName));
				ZeroMemory(szDomainName, sizeof(szDomainName));
				dwPermission = pEA[nIndex].grfAccessPermissions;
				dwAccMode = pEA[nIndex].grfAccessMode;

				if (LookupAccountSid(NULL, (PSID)pEA[nIndex].Trustee.ptstrName, szName, &dwNameLen, szDomainName, &dwDomainLen, &eSidType))
				{
					TiXmlElement *pPermission = NULL;
					if (eSidType == SidTypeUser || eSidType == SidTypeDeletedAccount)//单个用户
					{
						pPermission = new TiXmlElement("Useraccount");
					}
					else//用户组
					{
						pPermission = new TiXmlElement("Usergroup");
					}
					if (pPermission == NULL)
						continue;

					pPermission->SetAttribute("name", T2A(szName));
					if (dwPermission == 0x1200A9)//读
						dwPermission = 1;
					else if (dwPermission == 0x1301BF)//更改
						dwPermission = 2;
					else if (dwPermission == 0x1F01FF)//完全控制
						dwPermission = 3;
					else
						dwPermission = 0;

					if (dwAccMode == DENY_ACCESS)
						dwPermission |= 0x10;
					pPermission->SetAttribute("permission", dwPermission);

					pShare->LinkEndChild(pPermission);
				}
				else
				{
					DWORD dwErrorCode = GetLastError();
					continue;
				}
			}
		}
		// Free the allocated buffer.
		NetApiBufferFree(BufPtr);

	}
	// Continue to call NetShareEnum while there are more entries. 
	while (res==ERROR_MORE_DATA); // end do
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值