Article ID: 163253
Article Last Modified on 11/21/2006
DWORD dwLength = 0;
PSECURITY_DESCRIPTOR pSecurityDescriptor = NULL;
GetFileSecurity(lpszFileName, DACL_SECURITY_INFORMATION,
NULL, dwLength, &dwLength);
pSecurityDescriptor = (PSECURITY_DESCRIPTOR) new BYTE[dwLength];
In the case where a file is located on a non-NT share, the GetFileSecurity
call fails. It may return a large value and this can cause the new
statement to fail with an "out of memory" error. You need to select the
return value for GetFileSecurity.
**********
#include <afxpriv.h>
#include "c:\msdev\mfc\src\afximpl.h"
class CMyMirrorFile: public CMirrorFile
{
virtual BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags,
CFileException* pError = NULL);
};
BOOL CMyMirrorFile::Open(LPCTSTR lpszFileName, UINT nOpenFlags,
CFileException* pError)
{
ASSERT(lpszFileName != NULL);
m_strMirrorName.Empty();
#ifndef _MAC
CFileStatus status;
if (nOpenFlags & CFile::modeCreate) //opened for writing
{
if (CFile::GetStatus(lpszFileName, status))
{
CString strRoot;
AfxGetRoot(lpszFileName, strRoot);
DWORD dwSecPerClus, dwBytesPerSec, dwFreeClus, dwTotalClus;
int nBytes = 0;
if (GetDiskFreeSpace(strRoot, &dwSecPerClus, &dwBytesPerSec,
&dwFreeClus,
&dwTotalClus))
{
nBytes = dwFreeClus*dwSecPerClus*dwBytesPerSec;
}
if (nBytes > 2*status.m_size) // at least 2x free space avail
{
// get the directory for the file
TCHAR szPath[_MAX_PATH];
LPTSTR lpszName;
GetFullPathName(lpszFileName, _MAX_PATH, szPath, &lpszName);
*lpszName = NULL;
//let's create a temporary file name
GetTempFileName(szPath, _T("MFC"), 0,
m_strMirrorName.GetBuffer(_MAX_PATH+1));
m_strMirrorName.ReleaseBuffer();
}
}
}
#endif
if (!m_strMirrorName.IsEmpty() &&
CFile::Open(m_strMirrorName, nOpenFlags, pError))
{
m_strFileName = lpszFileName;
#ifndef _MAC
FILETIME ftCreate, ftAccess, ftModify;
if (::GetFileTime((HANDLE)m_hFile, &ftCreate, &ftAccess, &ftModify))
{
AfxTimeToFileTime(status.m_ctime, &ftCreate);
SetFileTime((HANDLE)m_hFile, &ftCreate, &ftAccess, &ftModify);
}
// If GetFileSecurity returns an error, don't do anything with
// file securities.
DWORD dwLength = 0;
PSECURITY_DESCRIPTOR pSecurityDescriptor = NULL;
if (GetFileSecurity(lpszFileName, DACL_SECURITY_INFORMATION,
NULL, dwLength, &dwLength))
{
pSecurityDescriptor = (PSECURITY_DESCRIPTOR) new
BYTE[dwLength];
if (::GetFileSecurity(lpszFileName, DACL_SECURITY_INFORMATION,
pSecurityDescriptor, dwLength, &dwLength))
{
SetFileSecurity(m_strMirrorName, DACL_SECURITY_INFORMATION,
pSecurityDescriptor);
}
delete[] (BYTE*)pSecurityDescriptor;
}
#endif
return TRUE;
}
m_strMirrorName.Empty();
return CFile::Open(lpszFileName, nOpenFlags, pError);
}
*************
Then you need to override CDocument::GetFile() to return an object of your
new derived class.Keywords: kbbug kbcode kbdocview kbfileio KB163253