Solved

how can i set a file locked?

Posted on 2002-06-25
5
263 Views
Last Modified: 2013-11-20
when i open a file in my program, i could delete the file when it opened.I want to lock it. what can I do?
0
Comment
Question by:quace
  • 2
  • 2
5 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 7107013
With MFC's CFile class you lock a file with the constructor or the Open member function:

CFile x(lpszFilename, CFile::modeWrite | CFile::shareExclusive);

the CFile::shareExclusive flag locks the file for EXCLUSIVE access to this opening.
0
 

Author Comment

by:quace
ID: 7109473
Could you tell me how to do in mfc framework?
I use CDocument::OnOpenDocument()
0
 
LVL 32

Expert Comment

by:jhance
ID: 7110137
Believe it or not, CFile _IS_INDEED_ an MFC class.

Beyond that, I'm really don't know what you mean by "do in mfc framework", please explain.
0
 

Author Comment

by:quace
ID: 7112504
doc/view architecture
0
 
LVL 1

Accepted Solution

by:
jianliuer earned 60 total points
ID: 7116174
hi,quace


pls step into BOOL CDocument::OnOpenDocument(...)

and u can find following lines:

....
          CWaitCursor wait;
          if (pFile->GetLength() != 0)
               Serialize(loadArchive);     // load me
          loadArchive.Close();
          ReleaseFile(pFile, FALSE);

....


the file will be closed after u call the default implement CDocument::OnOpentDocument(...).So u can try this:


CYOURDocument::OnOpenDocument(...)
{
if(m_pFile != NULL)
{
 ReleaseFile(pFile, FALSE);
}
 CDocument::OnOpenDocument(...);
 m_pFile = GetFile(lpszPathName,
 CFile::modeRead|CFile::shareDenyWrite, &fe);
 if (m_pFile == NULL)
     {
          ReportSaveLoadException(lpszPathName, &fe,
               FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
          return FALSE;
     }
 ....
}

CYOURDocument::CYOURDocument()
{
m_pFile = NULL;
}

CYOURDocument::~CYOURDocument()
{
if(m_pFile != NULL)
{
 ReleaseFile(pFile, FALSE);
}
}

where m_pFile is a class member of CYOURDocument CFile* m_pFile.

GOOD LUCK!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mixString challenge 36 112
Error on moodle after upgrade 3 123
post4 challenge 28 93
Updating statistics with error notification email in SQL server 4 74
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now