1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| bool CanAccessFolder( LPCTSTR folderName, DWORD genericAccessRights ) { bool bRet = false; DWORD length = 0; if (!::GetFileSecurity(folderName, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, NULL, NULL, &length ) && ERROR_INSUFFICIENT_BUFFER == ::GetLastError()) { PSECURITY_DESCRIPTOR security = static_cast<PSECURITY_DESCRIPTOR >( ::malloc( length ) ); if (security && ::GetFileSecurity( folderName, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, security, length, &length )) { HANDLE hToken = NULL; if (::OpenProcessToken(::GetCurrentProcess(), TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_DUPLICATE | STANDARD_RIGHTS_READ, &hToken )) { HANDLE hImpersonatedToken = NULL; if (::DuplicateToken( hToken, SecurityImpersonation, &hImpersonatedToken )) { GENERIC_MAPPING mapping = { 0xFFFFFFFF }; PRIVILEGE_SET privileges = { 0 }; DWORD grantedAccess = 0, privilegesLength = sizeof( privileges ); BOOL result = FALSE; mapping.GenericRead = FILE_GENERIC_READ; mapping.GenericWrite = FILE_GENERIC_WRITE; mapping.GenericExecute = FILE_GENERIC_EXECUTE; mapping.GenericAll = FILE_ALL_ACCESS; ::MapGenericMask( &genericAccessRights, &mapping ); if (::AccessCheck( security, hImpersonatedToken, genericAccessRights, &mapping, &privileges, &privilegesLength, &grantedAccess, &result )) { bRet = (result == TRUE); } ::CloseHandle( hImpersonatedToken ); } ::CloseHandle( hToken ); } ::free( security ); } } return bRet; }
|