Solved

how can i set a file locked?

Posted on 2002-06-25
5
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 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