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
}
获取本地共享资源详细信息的源码
最新推荐文章于 2021-11-22 15:52:41 发布